帮助SQL查询

时间:2011-06-02 15:05:32

标签: sql sql-server

我对SQL查询有疑问,我想知道从哪里开始。到目前为止的想法包括在记忆中创建一个包含一系列日期的表格,并加入其中以获得特定日期输入的小时数。

这里只是为了解一下这个问题的背景知识。

数据库的结构如下: enter image description here

尝试使用当前正在编写的查询实现的结果需要如下所示: enter image description here

到目前为止我写的查询:

DECLARE @Deleted AS BIT
DECLARE @WeekStartDate AS DATETIME
DECLARE @WeekEndDate AS DATETIME

SET @Deleted = 0
SET @WeekStartDate = '2011/05/01 00:00'
SET @WeekEndDate = '2011/05/07 00:00'

SELECT 
[JobSheet].[JobSheetDate],
[JobSheet].[ContractID],
[JobSheet].[ContractCode],
[JobSheet].[ContractTitle],
[JobSheet].[ProjectID],
[JobSheet].[ProjectCode],
[JobSheet].[ProjectTitle],
[JobSheet].[JobID],
[JobSheet].[JobCode],
[JobSheet].[JobTitle],
[JobSheet].[SageDatabaseID],
[JobSheetLineHours].[CostRateCode],
SUM([JobSheetLineHours].[Hours]) AS TotalCostRateHours,
'???' AS 'Mon',
'???' AS 'Tue',
'???' AS 'Wed',
'???' AS 'Thu',
'???' AS 'Fri',
'???' AS 'Sat',
'???' AS 'Sun'

FROM [JobSheet]

INNER JOIN [JobSheetLine]
ON  [JobSheetLine].[JobSheetID]
=   [JobSheet].[JobSheetID]

INNER JOIN [JobSheetLineHours]
ON  [JobSheetLineHours].[JobSheetLineID]
=   [JobSheetLine].[JobSheetLineID]

WHERE 
    [JobSheet].[Deleted]=@Deleted
AND [JobSheet].[JobSheetDate] >= @WeekStartDate
AND [JobSheet].[JobSheetDate] <= @WeekEndDate
AND [JobSheetLine].[Deleted]=@Deleted
AND [JobSheetLineHours].[Deleted]=@Deleted

GROUP BY 
    [JobSheet].[JobSheetDate],
    [JobSheet].[ContractID],
    [JobSheet].[ContractCode],
    [JobSheet].[ContractTitle],
    [JobSheet].[ProjectID],
    [JobSheet].[ProjectCode],
    [JobSheet].[ProjectTitle],
    [JobSheet].[JobID],
    [JobSheet].[JobCode],
    [JobSheet].[JobTitle],
    [JobSheet].[SageDatabaseID],
    [JobSheetLineHours].[CostRateCode]

然后输出以下结果集: enter image description here

正如您所看到的那样,结果集几乎就在那里,我只需要计算该特定日期的工作小时数,对ContractID,JobID,ProjectID,CostRateCode进行分组,这样我就可以获得每个成本费率的小时数在每个独特的项目上。

目前的表格设计是否可以实现?或者通过使用临时日历表来确定传递给查询的两者之间的日期?

编辑: 更新的查询:

SET DATEFIRST 1 -- Set the first day of week to monday
GO

DECLARE @Deleted AS BIT
DECLARE @RequestedByID AS BIGINT
DECLARE @WeekStartDate AS DATETIME
DECLARE @WeekEndDate AS DATETIME
DECLARE @WaitingForUserID AS BIGINT
DECLARE @WaitingForUserTypeID AS BIGINT
DECLARE @WaitingForTypeUser AS VARCHAR(50)
DECLARE @WaitingForTypeUserType AS VARCHAR(50)

SET @Deleted = 0
SET @WeekStartDate = '2009/05/01 00:00'
SET @WeekEndDate = '2012/05/07 00:00'

SELECT 
[JobSheet].[JobSheetDate],
[JobSheet].[ContractID],
[JobSheet].[ContractCode],
[JobSheet].[ContractTitle],
[JobSheet].[ProjectID],
[JobSheet].[ProjectCode],
[JobSheet].[ProjectTitle],
[JobSheet].[JobID],
[JobSheet].[JobCode],
[JobSheet].[JobTitle],
[JobSheet].[SageDatabaseID],
[JobSheetLineHours].[CostRateCode],
SUM([JobSheetLineHours].[Hours]) AS TotalCostRateHours,
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 1 
THEN SUM(JobSheetLineHours.Hours )
END 
AS MON,
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 2 
THEN SUM(JobSheetLineHours.Hours )
END 
AS TUE, 
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 3 
THEN SUM(JobSheetLineHours.Hours )
END 
AS WED,
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 4 
THEN SUM(JobSheetLineHours.Hours )
END 
AS THU,
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 5 
THEN SUM(JobSheetLineHours.Hours )
END 
AS FRI,
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 6 
THEN SUM(JobSheetLineHours.Hours )
END 
AS SAT,
CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 7 
THEN SUM(JobSheetLineHours.Hours )
END 
AS SUN

FROM [JobSheet]

INNER JOIN [JobSheetLine]
ON  [JobSheetLine].[JobSheetID]
=   [JobSheet].[JobSheetID]

INNER JOIN [JobSheetLineHours]
ON  [JobSheetLineHours].[JobSheetLineID]
=   [JobSheetLine].[JobSheetLineID]

WHERE 
    [JobSheet].[Deleted]=@Deleted
AND [JobSheet].[JobSheetDate] >= @WeekStartDate
AND [JobSheet].[JobSheetDate] <= @WeekEndDate
AND [JobSheetLine].[Deleted]=@Deleted
AND [JobSheetLineHours].[Deleted]=@Deleted

GROUP BY 
    [JobSheet].[JobSheetDate],
    [JobSheet].[ContractID],
    [JobSheet].[ContractCode],
    [JobSheet].[ContractTitle],
    [JobSheet].[ProjectID],
    [JobSheet].[ProjectCode],
    [JobSheet].[ProjectTitle],
    [JobSheet].[JobID],
    [JobSheet].[JobCode],
    [JobSheet].[JobTitle],
    [JobSheet].[SageDatabaseID],
    [JobSheetLineHours].[CostRateCode],
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
    WHEN 1 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 2 
    THEN     JobSheetLineHours.Hours 
    END, 
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 3 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 4 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 5 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 6 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 7 
    THEN     JobSheetLineHours.Hours 
    END

更新的结果集: enter image description here

我现在要做的是从结果集中删除JobSheetDate,以便每天的总小时数不是全部在单独的行上,例如。同一合同,项目等的值放在同一行,但是在Mon,Tues,Wed等列中,而不是在多行中填充一天的小时数,其余时间为null(参见屏幕截图) )


编辑2: 更新的查询:

SET DATEFIRST 1 -- Set the first day of week to monday
GO

DECLARE @Deleted AS BIT
DECLARE @WeekStartDate AS DATETIME
DECLARE @WeekEndDate AS DATETIME

SET @Deleted = 0
SET @WeekStartDate = '2009/05/01 00:00'
SET @WeekEndDate = '2012/05/07 00:00'

SELECT 
--[JobSheet].[JobSheetDate],
[JobSheet].[ContractID],
[JobSheet].[ContractCode],
[JobSheet].[ContractTitle],
[JobSheet].[ProjectID],
[JobSheet].[ProjectCode],
[JobSheet].[ProjectTitle],
[JobSheet].[JobID],
[JobSheet].[JobCode],
[JobSheet].[JobTitle],
[JobSheet].[SageDatabaseID],
[JobSheetLineHours].[CostRateCode],
SUM([JobSheetLineHours].[Hours]) AS TotalCostRateHours,
SUM( CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 1 
THEN JobSheetLineHours.Hours 
END )
AS MON,
SUM( CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 2 
THEN JobSheetLineHours.Hours 
END )
AS TUE, 
SUM(CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 3 
THEN JobSheetLineHours.Hours 
END)
AS WED,
SUM( CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 4 
THEN JobSheetLineHours.Hours 
END) 
AS THU,
SUM(CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 5 
THEN JobSheetLineHours.Hours 
END )
AS FRI,
SUM( CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 6 
THEN JobSheetLineHours.Hours 
END )
AS SAT,
SUM( CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
WHEN 7 
THEN JobSheetLineHours.Hours 
END )
AS SUN

FROM [JobSheet]

INNER JOIN [JobSheetLine]
ON  [JobSheetLine].[JobSheetID]
=   [JobSheet].[JobSheetID]

INNER JOIN [JobSheetLineHours]
ON  [JobSheetLineHours].[JobSheetLineID]
=   [JobSheetLine].[JobSheetLineID]

WHERE 
    [JobSheet].[Deleted]=@Deleted
AND [JobSheet].[JobSheetDate] >= @WeekStartDate
AND [JobSheet].[JobSheetDate] <= @WeekEndDate
AND [JobSheetLine].[Deleted]=@Deleted
AND [JobSheetLineHours].[Deleted]=@Deleted

GROUP BY 
    --[JobSheet].[JobSheetDate],
    [JobSheet].[ContractID],
    [JobSheet].[ContractCode],
    [JobSheet].[ContractTitle],
    [JobSheet].[ProjectID],
    [JobSheet].[ProjectCode],
    [JobSheet].[ProjectTitle],
    [JobSheet].[JobID],
    [JobSheet].[JobCode],
    [JobSheet].[JobTitle],
    [JobSheet].[SageDatabaseID],
    [JobSheetLineHours].[CostRateCode],
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) 
    WHEN 1 
    THEN     JobSheetLineHours.Hours
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 2 
    THEN     JobSheetLineHours.Hours 
    END, 
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 3 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 4 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 5 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 6 
    THEN     JobSheetLineHours.Hours 
    END,
    CASE DATEPART(WEEKDAY, JobSheet.JobSheetDate) WHEN 7 
    THEN     JobSheetLineHours.Hours 
    END

结果: enter image description here


完成查询的最终更新:

SET DATEFIRST 1 -- Set the first day of week to monday
GO

DECLARE @Deleted AS BIT
DECLARE @WeekStartDate AS DATETIME
DECLARE @WeekEndDate AS DATETIME

SET @Deleted = 0
SET @WeekStartDate = '2009/05/01 00:00'
SET @WeekEndDate = '2012/05/07 00:00'

SELECT 
[JobSheet].[ContractID],
[JobSheet].[ContractCode],
[JobSheet].[ContractTitle],
[JobSheet].[ProjectID],
[JobSheet].[ProjectCode],
[JobSheet].[ProjectTitle],
[JobSheet].[JobID],
[JobSheet].[JobCode],
[JobSheet].[JobTitle],
[JobSheet].[SageDatabaseID],
[JobSheetLineHours].[CostRateCode],
SUM([JobSheetLineHours].[Hours]) AS TotalCostRateHours,
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Monday
     WHEN 1 
     THEN [JobSheetLineHours].[Hours]
     END )
AS MON,
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Tuesday
     WHEN 2 
     THEN [JobSheetLineHours].[Hours]
     END )
AS TUE, 
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Wednesday
     WHEN 3 
     THEN [JobSheetLineHours].[Hours]
     END )
AS WED,
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Thursday
     WHEN 4 
     THEN [JobSheetLineHours].[Hours]
     END ) 
AS THU,
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Friday
     WHEN 5 
     THEN [JobSheetLineHours].[Hours]
     END )
AS FRI,
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Saturday
     WHEN 6 
     THEN [JobSheetLineHours].[Hours]
     END )
AS SAT,
SUM( CASE DATEPART(WEEKDAY, [JobSheet].[JobSheetDate]) -- Get Total Value for Sunday
     WHEN 7 
     THEN [JobSheetLineHours].[Hours]
     END )
AS SUN

FROM [JobSheet]

INNER JOIN [JobSheetLine]
ON  [JobSheetLine].[JobSheetID]
=   [JobSheet].[JobSheetID]

INNER JOIN [JobSheetLineHours]
ON  [JobSheetLineHours].[JobSheetLineID]
=   [JobSheetLine].[JobSheetLineID]

WHERE 
    [JobSheet].[Deleted]=@Deleted
AND [JobSheet].[JobSheetDate] >= @WeekStartDate
AND [JobSheet].[JobSheetDate] <= @WeekEndDate
AND [JobSheetLine].[Deleted]=@Deleted
AND [JobSheetLineHours].[Deleted]=@Deleted

GROUP BY 
    [JobSheet].[ContractID],
    [JobSheet].[ContractCode],
    [JobSheet].[ContractTitle],
    [JobSheet].[ProjectID],
    [JobSheet].[ProjectCode],
    [JobSheet].[ProjectTitle],
    [JobSheet].[JobID],
    [JobSheet].[JobCode],
    [JobSheet].[JobTitle],
    [JobSheet].[SageDatabaseID],
    [JobSheetLineHours].[CostRateCode]

4 个答案:

答案 0 :(得分:7)

如果我理解正确,您需要将其添加到SELECT

编辑由于你需要总和,你需要围绕案例添加和解

SUM(CASE WHEN DATEPART(WEEKDAY, JobSheet.JobSheetDate) = 1 THEN 
    JobSheetLineHours.Hours 
END) AS MON,
SUM(CASE WHEN DATEPART(WEEKDAY, JobSheet.JobSheetDate) = 2 THEN 
    JobSheetLineHours.Hours 
END) AS TUE, 
SUM(CASE WHEN DATEPART(WEEKDAY, JobSheet.JobSheetDate) = 3 THEN 
    JobSheetLineHours.Hours 
END) AS WED,
SUM(CASE WHEN DATEPART(WEEKDAY, JobSheet.JobSheetDate) = 4 THEN 
    JobSheetLineHours.Hours 
END) AS THU,
SUM(CASE WHEN DATEPART(WEEKDAY, JobSheet.JobSheetDate) = 5 THEN 
    JobSheetLineHours.Hours 
END) AS FRI,

您还需要从SELECTGROUP BY 删除任何[JobSheet]。[JobSheetDate],以便在一行中获取所有日期

如评论中所述,这取决于SET DATEFIRST的设置方式

还应该注意的是,只要你有SUM(CASE,肯定有PIVOT替代

答案 1 :(得分:4)

创建具体的日历表是​​完全可以接受的(并且通常是最佳的)。

桌子小而轻,您可以JOIN轻松地按照您想要的任何日期组合分组。如果您愿意,也可以添加一列来表示工作日和非工作日。

否则获得“零小时”日是很尴尬的,因为你要求从不存在的数据进行聚合。

答案 2 :(得分:2)

我使用示例表方案调查它。希望它会对你有所帮助。从未经历过这种情况。很高兴今天学到新东西。感谢帖子 WraithNath

create table #temp
(
    projectId int,
    ContractID int,
    CostRateCode varchar(10),
    JobSheetDate datetime,
    Hours Int   
)

我插入了以下几条记录

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 1, 'A', '2011-06-02 22:00:35.337', 1)

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 1, 'A', '2011-06-01 22:00:35.337', 2)

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 1, 'A', '2011-05-31 22:00:35.337', 3)

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 1, 'A', '2011-06-03 22:00:35.337', 6)

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 1, 'A', '2011-05-30 22:00:35.337', 5)

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 2, 'B', '2011-05-31 22:00:35.337', 4)

Insert into #temp(projectId, ContractID, CostRateCode, JobSheetDate, Hours)
Values(1, 2, 'B', '2011-06-01 22:00:35.337', 7)

以下是插入后的情况

enter image description here

最后是select语句

SET DATEFIRST 1

Select projectId ,ContractID ,CostRateCode,Sum(Hours) Total,
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 1 Then Hours Else 0 End) as 'Mon',
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 2 Then Hours Else 0 End) as 'Tue',
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 3 Then Hours Else 0 End) as 'Wed',
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 4 Then Hours Else 0 End) as 'Thu',
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 5 Then Hours Else 0 End) as 'Fri',
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 6 Then Hours Else 0 End) as 'Sat',
SUM(Case When DATEPART(WEEKDAY, JobSheetDate) = 7 Then Hours Else 0 End) as 'Sun'
From #temp
Group By projectId ,ContractID ,CostRateCode

Drop table #temp

结果集

enter image description here

答案 3 :(得分:0)

尝试使用子查询,其中包含日期的问号,一周中每天的一个子查询;当天的日期是根据传入的日期计算的。星期一的日期是:

( SELECT SUM([JobSheetLineHours].[Hours]) 
  FROM [JobSheet]              
  INNER JOIN [JobSheetLine] ON [JobSheetLine].[JobSheetID] = 
    [JobSheet].[JobSheetID]
  INNER JOIN [JobSheetLineHours] ON [JobSheetLineHours].[JobSheetLineID] =   
    [JobSheetLine].[JobSheetLineID]
  WHERE[JobSheet].[JobSheetDate] = @WeekStartDate + 1 AND
   [JobSheet].[Deleted]=@Deleted AND 
   [JobSheetLine].[Deleted]=@Deleted AND 
   [JobSheetLineHours].[Deleted]=@Deleted )

星期二你会加2等等。

注意:这只有在@WeekStartDate确实是星期天的情况下才有效,尽管你似乎已经假设了这样。