SQL Microsoft Access立即显示下一行

时间:2015-10-30 15:57:15

标签: sql ms-access-2013

如果在WHERE语句中rowa为true,我试图显示rowa和rowb(在它下面)。我确实有ORDER BY Date,但这不会影响任何事情。如果显示rowa,有没有快速显示rowb的方法?

我的代码是......

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);

我希望看到的是......

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);

因此,每个表示Wellsite的时间类型都需要两行。一个用于Reg工作日,另一个用于显示工作ID。我的代码显示了所有具有Job ID的行,这是我需要除去它们之前的Reg Work Day。另外,我有一排办公时间类型很好,我没有任何问题。

1 个答案:

答案 0 :(得分:1)

您的要求的问题在于您假设数据已订购,而您是此行和下一行。但是,在SQL数据中没有排序,您需要在ORDER BY子句上使用列来获取订单。

查看示例数据,您想要的两行似乎都具有相同的日期。如果这是案例那么解决方案并不难。你这样做:

首先,您会看到符合条件的日期列表。

SELECT `Date` 
FROM ROSTER 
WHERE `Hourly Value` > 15 AND Attribute = 'Reg Work Day'

然后,您将此列表作为选择表格其余部分的标准

SELECT *
FROM ROSTER
WHERE `Date` IN (
  SELECT `Date` 
  FROM ROSTER 
  WHERE `Hourly Value` > 15 AND Attribute = 'Reg Work Day'
) sub

这里要说的是,你无法在SQL中思考有序和顺序 - 你必须在集合中思考 - 集合中的SQL交易。