在SQL中组合两个返回的行

时间:2015-05-18 13:05:18

标签: sql sql-server tsql stored-procedures

我有一个存储过程,我暂时填充了返回在公司记录的作业上花费的时间的值,我将JobTypeID分组到4个不同的列中,Dev工作时间,Dev小时收费,支持工作时间和支持小时数。在运行代码过程时,由于我如何使用GroupBy子句将数据分成多行,我该如何修复它以便将数据输出到一行?

;WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime,
       WorkLogJobTypeID,
       tblJobWorkLog.SystemUserID

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE
       tblJobWorkLog.StartTime >= '20150511'
       AND tblJobWorkLog.EndTime <= '20150515'
       AND SystemUserID = '65405273-6BFD-4482-8A0D-BC6430AC996D'

)

SELECT
       FromDate,
       Case when WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93' then (SUM(JobTime) / 60.0) end as SupportHoursWorked,
       Case when WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93' then SUM(ChargeableTime) end AS SupportHoursCharged,
       Case when WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F' OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' 
       OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD' then (SUM(JobTime) / 60.0) end as DevelopmentHoursWorked,
       Case when WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F' OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' 
       OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD' then SUM(ChargeableTime) end as DevelopmentHoursCharged

FROM
       cte

GROUP BY
       FromDate, WorkLogJobTypeID
ORDER BY
       FromDate

结果:

2015-05-11  NULL        NULL        1.250000    1.250000
2015-05-11  1.166666    0.5         NULL        NULL
2015-05-12  0.000000    0.0         NULL        NULL
2015-05-12  NULL        NULL        3.250000    1.250000
2015-05-12  0.250000    0.000000    NULL        NULL
2015-05-12  NULL        NULL        0.250000    0.000000
2015-05-13  NULL        NULL        0.750000    0.750000
2015-05-13  0.000000    0.000000    NULL        NULL
2015-05-13  NULL        NULL        0.0         0.000000
2015-05-14  NULL        NULL        1.0         1.000000
2015-05-14  4.000000    1.166667    NULL        NULL
2015-05-14  NULL        NULL        1.0         0.750000

^我需要将日期结果合并为一行

3 个答案:

答案 0 :(得分:0)

对带有空值的列使用sum,否则您将丢失结果,然后按日期分组

答案 1 :(得分:0)

道歉这是第一次没有工作,您可以使用以下内容,现在应该为您提供您正在寻找的结果:

WITH cte AS (

SELECT
       DATEPART(Year, StartTime) AS YearNumber,
       DATEPART(Month, StartTime) AS MonthNumber,
       DateName(Month, StartTime) + ' ' + CAST(DatePart(Year, StartTime) AS nvarchar(50)) AS TimePeriod,
       DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
       DateDiff(minute, StartTime, EndTime) AS JobTime,
       tblJobWorkLog.ChargeableTime,
       WorkLogJobTypeID,
       tblJobWorkLog.SystemUserID

FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID

WHERE
       tblJobWorkLog.StartTime >= '20150511'
       AND tblJobWorkLog.EndTime <= '20150515'
       AND SystemUserID = '65405273-6BFD-4482-8A0D-BC6430AC996D'

)

Select FromDate, SUM(SupportHoursWorked) SupportHoursWorked, SUM(SupportHoursCharged) SupportHoursCharged, SUM(DevelopmentHoursWorked) DevelopmentHoursWorked, SUM(DevelopmentHoursCharged) DevelopmentHoursCharged

From

(SELECT
       FromDate,
       Case when WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93' then (SUM(JobTime) / 60.0) end as SupportHoursWorked,
       Case when WorkLogJobTypeID = 'FA5E6979-D228-44B7-A91B-8DDC8DDC709B' OR WorkLogJobTypeID = '3171B295-60E9-4724-95A3-04FA182D7D43' OR WorkLogJobTypeID = '52c2691f-ff0a-4263-a440-8a309f868f93' then SUM(ChargeableTime) end AS SupportHoursCharged,
       Case when WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F' OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' 
       OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD' then (SUM(JobTime) / 60.0) end as DevelopmentHoursWorked,
       Case when WorkLogJobTypeID = 'D0E910B1-B4BD-430C-AD04-EB4E67946806' OR WorkLogJobTypeID = 'B0BBF362-294D-4262-BED8-EDA7EE74745B' OR WorkLogJobTypeID = '1E333ADC-E4F2-4042-8B65-E25F2770D59F' OR WorkLogJobTypeID = 'A445B7CE-E9E4-48E6-B5AA-83C83F045315' 
       OR WorkLogJobTypeID = '1D83F510-87FA-446E-9337-3D0376210D57' OR WorkLogJobTypeID = 'B59C1596-E1D0-4118-A805-65208E27AFB5' OR WorkLogJobTypeID = 'F44A4B3C-B149-45A8-A9F0-5A57883482FD' then SUM(ChargeableTime) end as DevelopmentHoursCharged

FROM
       cte

GROUP BY
       FromDate, WorkLogJobTypeID) a
       Group by FromDate

ORDER BY
       FromDate

答案 2 :(得分:0)

这应该有效并且更具可读性:

;WITH cte AS (

SELECT
  DATEADD(day, DATEDIFF(Day, 0, StartTime), 0) AS FromDate,
  DateDiff(minute, StartTime, EndTime) AS JobTime,
  tblJobWorkLog.ChargeableTime,
  CASE WHEN WorkLogJobTypeID in 
  ('FA5E6979-D228-44B7-A91B-8DDC8DDC709B', '3171B295-60E9-4724-95A3-04FA182D7D43', 
   '52c2691f-ff0a-4263-a440-8a309f868f93') THEN 1 END chk1,
  CASE WHEN WorkLogJobTypeID in 
    ('D0E910B1-B4BD-430C-AD04-EB4E67946806', 'B0BBF362-294D-4262-BED8-EDA7EE74745B', 
     '1E333ADC-E4F2-4042-8B65-E25F2770D59F', 'A445B7CE-E9E4-48E6-B5AA-83C83F045315', 
     '1D83F510-87FA-446E-9337-3D0376210D57', 'B59C1596-E1D0-4118-A805-65208E27AFB5', 
     'F44A4B3C-B149-45A8-A9F0-5A57883482FD') THEN 1 END chk2
FROM
       tblJobWorkLog
       INNER JOIN tblJob ON tblJobWorkLog.JobID = tblJob.JobID
       INNER JOIN tblContact ON tblJob.ContactID = tblContact.ContactID
WHERE
       tblJobWorkLog.StartTime >= '20150511'
       AND tblJobWorkLog.EndTime <= '20150515'
       AND SystemUserID = '65405273-6BFD-4482-8A0D-BC6430AC996D'
)
SELECT
       FromDate,
       SUM(chk1 * JobTime) / 60.0 as SupportHoursWorked,
       SUM(chk1 * ChargeableTime) AS SupportHoursCharged,
       SUM(chk2 * JobTime) / 60.0 as DevelopmentHoursWorked,
       SUM(chk2 * ChargeableTime) as DevelopmentHoursCharged
FROM
       cte
GROUP BY FromDate
ORDER BY FromDate