协助进行SQL查询(需要添加更多的列和过滤器)

时间:2018-07-03 03:17:21

标签: sql-server

我有下表:

CREATE TABLE [dbo].[ActivityList] 
(
    [ActivityListId]  INT            IDENTITY (1, 1) NOT NULL,
    [ActivityName]    NVARCHAR (100) NOT NULL,
    [IsActive]        NVARCHAR (6)   NULL,
    [CreatedDate]     DATETIME2 (7)  NULL,
    [CreatedBy]       NVARCHAR (50)  NULL,
    [UpdatedDate]     DATETIME2 (7)  NULL,
    [UpdatedBy]       NVARCHAR (50)  NULL,
    [DeactivatedDate] DATETIME2 (7)  NULL,
    [DeactivatedBy]   NVARCHAR (50)  NULL,
    [DepartmentId]    INT            NOT NULL,
    [SiteView]        VARCHAR (20)   DEFAULT ('Warehouse') NOT NULL,

    PRIMARY KEY CLUSTERED ([ActivityListId] ASC),
    CONSTRAINT [FK_ActivityList_DepartmentId] 
        FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId])
);

CREATE TABLE [dbo].[Department] 
(
    [DepartmentId]    INT           IDENTITY (1, 1) NOT NULL,
    [DepartmentName]  VARCHAR (30)  NOT NULL,
    [IsActive]        NVARCHAR (6)  NULL,
    [CreatedDate]     DATETIME2 (7) NULL,
    [CreatedBy]       NVARCHAR (50) NULL,
    [UpdatedDate]     DATETIME2 (7) NULL,
    [UpdatedBy]       NVARCHAR (50) NULL,
    [DeactivatedDate] DATETIME2 (7) NULL,
    [DeactivatedBy]   NVARCHAR (50) NULL,
    [SiteView]        VARCHAR (20)  DEFAULT ('Warehouse') NULL,

    PRIMARY KEY CLUSTERED ([DepartmentId] ASC)
);

CREATE TABLE [dbo].[TimeSheet] 
(
    [TimeSheetId]    INT            IDENTITY (1, 1) NOT NULL,
    [Week]           INT            NOT NULL,
    [Departmentname] NVARCHAR (30)  NOT NULL,
    [WorkDate]       DATE           NOT NULL,
    [Employee]       VARCHAR (50)   NOT NULL,
    [Activity]       NVARCHAR (100) NOT NULL,
    [Time]           TIME (7)       NULL,
    [IsActive]       NVARCHAR (6)   NOT NULL,
    [CreatedDate]    DATETIME2 (7)  NULL,
    [CreatedBy]      NVARCHAR (50)  NULL,
    [UpdatedDate]    DATETIME2 (7)  NULL,
    [UpdatedBy]      NVARCHAR (50)  NULL,
    [DeletedDate]    DATETIME2 (7)  NULL,
    [DeletedBy]      NVARCHAR (50)  NULL,
    [UserId]         INT            NOT NULL,
    [ActivityListId] INT            NOT NULL,
    [DepartmentId]   INT            NOT NULL,
    [Company]        NVARCHAR (16)  NULL,
    [Notes]          NVARCHAR (MAX) NULL,
    [SiteView]       VARCHAR (20)   DEFAULT ('Warehouse') NOT NULL,

    PRIMARY KEY CLUSTERED ([TimeSheetId] ASC),
    CONSTRAINT [FK_UserId] 
        FOREIGN KEY ([UserId]) REFERENCES [dbo].[users] ([UserId]),
    CONSTRAINT [FK_ActivityListId] 
        FOREIGN KEY ([ActivityListId]) REFERENCES [dbo].[ActivityList] ([ActivityListId]),
    CONSTRAINT [FK_DepartmentId] 
        FOREIGN KEY ([DepartmentId]) REFERENCES [dbo].[Department] ([DepartmentId])
);

和类似这样的查询

WITH a AS 
(
    SELECT 
        ts.[week], 
        ts.departmentid, 
        ts.employee, 
        ts.company, 
        ts.activitylistid, 
        minutes_worked = COALESCE(Sum(Datediff(minute, '0:00:00', time)), 0) 
    FROM   
        timesheet ts 
    WHERE  
        createddate BETWEEN '01/01/2018' AND '12/31/2018' 
        AND siteview = 'Warehouse' 
    GROUP BY 
        ts.[week], 
        ts.departmentid, 
        ts.activitylistid, 
        ts.employee, 
        ts.company
) 
SELECT 
    d.departmentname, 
    al1.activityname, 
    COALESCE((SELECT CAST((minutes_worked / 60) AS VARCHAR(8)) + ':' 
                        + RIGHT('0' + CAST((minutes_worked % 60) AS VARCHAR(2)), 2) 
              FROM a 
              WHERE a.activitylistid = al1.activitylistid 
                AND a.departmentid = al1.departmentid 
               AND week = 18), '00:00') Week_18 
FROM   
    activitylist al1 
INNER JOIN 
    department D ON al1.departmentid = d.departmentid 
WHERE  
    d.departmentid = 4 
ORDER BY 
    D.departmentid, 
    al1.activityname 

通过上面的查询,我获得了部门名称,活动名称以及给定一周内每个活动的总工作时间。现在,我的问题是,使用相同的查询,是否可以获取员工,公司和创建日期?如果是这样,有人可以指出正确的方向吗?另外,是否可以为过滤器提供多名员工?

这是我在原始查询中获得的输出。

enter image description here

这是我加入TimeSheet表时得到的输出。

我希望将结果显示为第一个输出,但这是在列employee,company和createddate中添加的。

enter image description here

1 个答案:

答案 0 :(得分:1)

您快到了。只需将JOIN中的TimeSheet表中的activityListSELECT一起使用。假设您在Timesheet表中每个ActivityListId有一条记录。要按员工过滤,只需将其包含在WHERE子句中即可。

WITH a
AS ( SELECT   ts.[week] ,
              ts.departmentid ,
              ts.employee ,
              ts.company ,
              ts.activitylistid ,
              minutes_worked = COALESCE(
                                   SUM(DATEDIFF(MINUTE, '0:00:00', time)), 0)
     FROM     timesheet ts
     WHERE    createddate
              BETWEEN '01/01/2018' AND '12/31/2018'
              AND siteview = 'Warehouse'
     GROUP BY ts.[week] ,
              ts.departmentid ,
              ts.activitylistid ,
              ts.employee ,
              ts.company )
SELECT   D.departmentname ,
         al1.activityname ,
         ts.employee ,
         ts.company ,
         ts.createddate ,
         COALESCE(
         (   SELECT CAST(( minutes_worked / 60 ) AS VARCHAR(8)) + ':'
                    + RIGHT('0' + CAST(( minutes_worked % 60 ) AS VARCHAR(2)), 2)
             FROM   a
             WHERE  a.activitylistid = al1.activitylistid
                    AND a.departmentid = al1.departmentid
                    AND week = 18 ) ,
         '00:00') Week_18
FROM     activitylist al1
         INNER JOIN department D ON al1.departmentid = D.departmentid
         INNER JOIN TimeSheet ts ON al1.ActivityListId = ts.ActivityListId
WHERE    D.departmentid = 4
ORDER BY D.departmentid ,
         al1.activityname;

更新:通过基于company过滤employee来更新查询以提供不同的Timesheetcreateddate

WITH a
AS ( SELECT   ts.[week] ,
              ts.departmentid ,
              ts.employee ,
              ts.company ,
              ts.activitylistid ,
              minutes_worked = COALESCE(
                                   SUM(DATEDIFF(MINUTE, '0:00:00', time)), 0)
     FROM     timesheet ts
     WHERE    createddate
              BETWEEN '01/01/2018' AND '12/31/2018'
              AND siteview = 'Warehouse'
     GROUP BY ts.[week] ,
              ts.departmentid ,
              ts.activitylistid ,
              ts.employee ,
              ts.company )
SELECT   D.departmentname ,
         al1.activityname ,
         ts.Employee ,
         ts.Company ,
         --ts.createdDate ,
         COALESCE(
         (   SELECT CAST(( minutes_worked / 60 ) AS VARCHAR(8)) + ':'
                    + RIGHT('0' + CAST(( minutes_worked % 60 ) AS VARCHAR(2)), 2)
             FROM   a
             WHERE  a.activitylistid = al1.activitylistid
                    AND a.departmentid = al1.departmentid
                    AND week = 18 ) ,
         '00:00') Week_18
FROM     activitylist al1
         INNER JOIN department D ON al1.departmentid = D.departmentid
         INNER JOIN (   SELECT  DISTINCT ActivityListId ,
                                         Company ,
                                         Employee
                        FROM    timesheet
                        WHERE   createdDate
                        BETWEEN '20180601' AND '20180630' ) AS ts ON al1.ActivityListId = ts.ActivityListId
WHERE    D.departmentid = 4
ORDER BY D.departmentid ,
         al1.activityname;