问题
I'm trying to show rowa and rowb (right below it) if rowa is true in the WHERE statement. I do have it ORDER BY Date, but that wouldn't affect anything. Are there any quick ways of showing the rowb if rowa is shown?
My code is...
SELECT Roster.`Complete Name`, RS1018to1024.`Day of Week`, RS1018to1024.`Date`, RS1018to1024.`Time Type`, RS1018to1024.`Attribute`, RS1018to1024.`Value`, RS1018to1024.`Hourly value (in decimals)`
FROM Roster
INNER JOIN RS1018to1024 ON Roster.GIN = RS1018to1024.GIN
WHERE (RS1018to1024.`Hourly value (in decimals)` >15 AND RS1018to1024.`Time Type`='Wellsite/Job/Vessel' OR RS1018to1024.Attribute='Job ID' )
OR (RS1018to1024.`Time Type`='On Office-Base-Lab' AND RS1018to1024.Attribute='Regular Work Day' AND RS1018to1024.`Hourly value (in decimals)`>8)
ORDER BY RS1018to1024.`Complete Name`, RS1018to1024.`Date`, RS1018to1024.`Attribute` DESC;
Day of Week| Date | Time Type | Attribute | Value | Hourly value
Tues |20-Oct-2015| Wellsite | Reg Work Day | RGWD | 16.8
Tues |20-Oct-2015| Wellsite | Job ID | 2213 |
Friday |23-Oct-2015| Wellsite | Job ID | 2251 |
Wed |21-Oct-2015| Wellsite | Reg Work Day | RGWD | 24
Tues |21-Oct-2015| Wellsite | Job ID | 2317 |
Sunday |18-Oct-2015| On Office | Reg Work Day | RGWD | 12.2
CREATE TABLE mytable(
Day_of_Week VARCHAR(8) NOT NULL PRIMARY KEY
,Date VARCHAR(9) NOT NULL
,Time_Type VARCHAR(19) NOT NULL
,Attribute VARCHAR(16) NOT NULL
,Value VARCHAR(28) NOT NULL
,Hourly_value_in_decimals NUMERIC(5,2)
);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Tuesday','20-Oct-15','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',16.75);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Tuesday','20-Oct-15','Wellsite/Job/Vessel','Job ID','2213840',NULL);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Friday','23-Oct-15','Wellsite/Job/Vessel','Job ID','2212599',NULL);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Friday','23-Oct-15','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',23.87);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Saturday','24-Oct-15','Wellsite/Job/Vessel','Job ID','2212599',NULL);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Sunday','18-Oct-15','On Office-Base-Lab','Regular Work Day','RGWD - Regular Work Day (BR)',19.87);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Monday','19-Oct-15','On Office-Base-Lab','Regular Work Day','RGWD - Regular Work Day (BR)',11.17);
What I would want to see is...
Day of Week| Date | Time Type | Attribute | Value | Hourly value
Tues |20-Oct-2015| Wellsite | Reg Work Day | RGWD | 16.8
Tues |20-Oct-2015| Wellsite | Job ID | 2213 |
Wed |21-Oct-2015| Wellsite | Reg Work Day | RGWD | 24
Tues |21-Oct-2015| Wellsite | Job ID | 2317 |
Sunday |18-Oct-2015| On Office | Reg Work Day | RGWD | 12.2
CREATE TABLE mytable(
Day_of_Week VARCHAR(8) NOT NULL PRIMARY KEY
,Date VARCHAR(9) NOT NULL
,Time_Type VARCHAR(19) NOT NULL
,Attribute VARCHAR(16) NOT NULL
,Value VARCHAR(28) NOT NULL
,Hourly_value_in_decimals NUMERIC(5,2)
);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Tuesday','20-Oct-15','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',16.75);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Tuesday','20-Oct-15','Wellsite/Job/Vessel','Job ID','2213840',NULL);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Friday','23-Oct-15','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',23.87);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Saturday','24-Oct-15','Wellsite/Job/Vessel','Job ID','2212599',NULL);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Sunday','18-Oct-15','On Office-Base-Lab','Regular Work Day','RGWD - Regular Work Day (BR)',19.87);
INSERT INTO mytable(Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('Monday','19-Oct-15','On Office-Base-Lab','Regular Work Day','RGWD - Regular Work Day (BR)',11.17);
So, every Time Type that says Wellsite needs two rows. One for the Reg Work Day and the next row showing the Job ID. My code is showing all rows that have Job ID which is what I need to get rid of unless they have the Reg Work Day above it. Also, I have a row of On Office Time Type that is fine and I'm not having any issues with.
回答1:
The problem with your requirements is you assume that the data is ordered and you this row and the next row. However in SQL data is not ordered you need a column to use on an ORDER BY clause to get an order.
Looking at your example data it seems that the two rows you want both have the same date. IF THIS IS THE CASE then the solution is not hard. You do it like this:
First you get a list of dates that meet the criteria.
SELECT `Date`
FROM ROSTER
WHERE `Hourly Value` > 15 AND Attribute = 'Reg Work Day'
Then you take this list as the criteria for selecting the rest of the table
SELECT *
FROM ROSTER
WHERE `Date` IN (
SELECT `Date`
FROM ROSTER
WHERE `Hourly Value` > 15 AND Attribute = 'Reg Work Day'
) sub
The takeaway here is that you can't think ordered and sequential in SQL -- you have to think in set -- SQL deals in sets.
来源:https://stackoverflow.com/questions/33440088/sql-microsoft-access-show-immediate-next-row