从具有多个要求的表中拉出对

时间:2015-11-02 19:49:53

标签: sql ms-access-2013

我有一个包含多名员工的数据库以及他们工作了多少小时。请参阅下文。
因此,每个说明Wellsite的Time.Type需要两行。一个用于属性:注册工作日,下一行显示属性:作业ID。我的代码显示了所有具有作业ID的行,这是我需要摆脱的,除非他们在其上方的Reg Work Day符合我的条件(见下文)。另外,我有一排办公时间类型很好,我没有任何问题。

我已经得到了帮助,但是对于一名员工。现在我需要我的数据来为多个员工完成这个过程。

我需要成立的条件是当一行的Time.Type为" Wellsite / Job / Vessel"和一个表示"正常工作日" "每小时价值"需要> 15。此外,我需要在它下面的行,它具有"作业ID"工作号码在"值"标签也是。据我所知,Date列可用于将行保持在一起,但我只需要它来为某些员工/天而不是一切工作。

下表显示了我的内容以及我希望它做的是在此表下面。

CREATE TABLE mytable(
   Complete_Name              VARCHAR(9) NOT NULL PRIMARY KEY
  ,Day_of_Week                VARCHAR(7) NOT NULL
  ,Date                       VARCHAR(11) 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(Complete_Name,Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('EmployeeA','Tuesday','20-Oct-2015','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',16.75);
INSERT INTO mytable(Complete_Name,Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('EmployeeA','Tuesday','20-Oct-2015','Wellsite/Job/Vessel','Job ID','2213840',NULL);
INSERT INTO mytable(Complete_Name,Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('EmployeeB','Tuesday','20-Oct-15','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',2.25);
INSERT INTO mytable(Complete_Name,Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('EmployeeB','Tuesday','20-Oct-15','Wellsite/Job/Vessel','Job ID','2213840',NULL);

表:

Complete_Name Day_of_Week Date        Time_Type           Attribute        Value                        Hourly_value_in_decimals
------------- ----------- ----------- ------------------- ---------------- ---------------------------- ---------------------------------------
EmployeeA     Tuesday     20-Oct-2015 Wellsite/Job/Vessel Regular Work Day RGWD - Regular Work Day (BR) 16.75
EmployeeA     Tuesday     20-Oct-2015 Wellsite/Job/Vessel Job ID           2213840                      NULL
EmployeeB     Tuesday     20-Oct-15   Wellsite/Job/Vessel Regular Work Day RGWD - Regular Work Day (BR) 2.25
EmployeeB     Tuesday     20-Oct-15   Wellsite/Job/Vessel Job ID           2213840                      NULL

我想看到的是......

CREATE TABLE mytable(
   Complete_Name              VARCHAR(9) NOT NULL PRIMARY KEY
  ,Day_of_Week                VARCHAR(7) NOT NULL
  ,Date                       VARCHAR(11) 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(Complete_Name,Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('EmployeeA','Tuesday','20-Oct-2015','Wellsite/Job/Vessel','Regular Work Day','RGWD - Regular Work Day (BR)',16.75);
INSERT INTO mytable(Complete_Name,Day_of_Week,Date,Time_Type,Attribute,Value,Hourly_value_in_decimals) VALUES ('EmployeeA','Tuesday','20-Oct-2015','Wellsite/Job/Vessel','Job ID','2213840',NULL);

表:

Complete_Name Day_of_Week Date        Time_Type           Attribute        Value                        Hourly_value_in_decimals
------------- ----------- ----------- ------------------- ---------------- ---------------------------- ---------------------------------------
EmployeeA     Tuesday     20-Oct-2015 Wellsite/Job/Vessel Regular Work Day RGWD - Regular Work Day (BR) 16.75
EmployeeA     Tuesday     20-Oct-2015 Wellsite/Job/Vessel Job ID           2213840                      NULL

1 个答案:

答案 0 :(得分:1)

因此,根据我之前的回答,您需要根据不同的标准为不同的员工提供帮助。

例如员工姓名Hogan可能需要几天> 15名名为NotHogan的员工可能需要几天时间> 24。

要执行此操作,请将条件放在表中(在下面的示例中,我动态构建表),然后加入该表以获取所需的日期和名称。

然后我加入到这个结果中以获得最终数据。

SELECT *
FROM ROSTER
JOIN (
  -- A list of dates and users we want to select
  SELECT `Date`, Name 
  FROM ROSTER 
  JOIN (
      -- Innermost table, our criteria, this can be an actual table in your db
      SELECT 'Hogan' as N, 15 as C 
         UNION ALL 
      SELECT 'NotHogan' as N, 24 as C 
  ) Criteria ON
      Criteria.N = ROSTER.Name AND 
      ROSTER.`Hourly Value` > Criteria.C AND  
      Attribute = 'Reg Work Day'
) sub ON ROSTER.`Date` = sub.`Date` and ROSTER.Name = Sub.Name