【问题标题】:How to set values at different time intervals?如何设置不同时间间隔的值?
【发布时间】:2017-08-22 05:02:30
【问题描述】:

我有一张桌子如下

AGREEMENTID         FeedbackDate               DispositionCode  CollectorId
0003SBML00151   2017-08-22 08:00:00.000                PTP        1
0004SBHL00705   2017-08-22 08:20:00.000                BPTP       1
0007SBML01987   2017-08-22 09:10:00.000                SET        1
0026MSS00108    2017-08-22 07:50:00.000                PTP        2
0026MSS00108    2017-08-22 10:30:00.000                NC         2
0026MSS00108    2017-08-22 10:30:10.000                DL         2
0026MSS00108    2017-08-22 15:47:00.000                CB         2
0026MSS00108    2017-08-22 19:52:00.000                BPTP       2

最终的输出将是

Collector 1 在 8.00 hrs 和 8.20 hrs 进行了两次处置。因此,针对 8hrs 列,他的处置代码可用。此外,他在 9.10 小时做出了一项处置,针对 9 小时列,他的处置代码可用。收集器 2 也是如此。

到目前为止我已经尝试过什么

Declare @T table
(AGREEMENTID varchar(50),
FeedbackDate varchar(50),
DispositionCode varchar(10),
[CollectorId] varchar(100)
)
Insert into @T 
SELECT '0003SBML00151','2017-08-22 08:00:00.000','PTP','1' union all
SELECT '0004SBHL00705','2017-08-22 08:20:00.000','BPTP','1' union all
SELECT '0007SBML01987','2017-08-22 09:10:00.000','BPTP','1' union all
SELECT '0026MSS00108','2017-08-21 07:50:00.000','PTP','2' union all
SELECT '0026MSS00108','2017-08-21 10:30:00.000','NC','2' union all
SELECT '0026MSS00108','2017-08-21 10:30:10.000','DL' ,'2' union all
SELECT '0026MSS00108','2017-08-21 15:47:00.000','CB' ,'2' union all
SELECT '0026MSS00108','2017-08-21 19:52:00.000','BPTP','2'

;with   HoursCte as
        (
        select  7 as Hrs
        union all
        select  Hrs + 1
        from    HoursCte
        where   Hrs < 22
        ),
 cte1 as(
SELECT 
    CollectorId
    ,AGREEMENTID
    ,FeedbackDate
    ,FeedbackTime = CONVERT(nvarchar,CAST(FeedbackDate as time),108)
    ,DispositionCode
FROM @T t1),
cte2 as(
Select *, FeedbackHrs = case when FeedbackTime between cast('07:00:00' as time) and cast('07:59:59' as time) then '7Hrs'
               when FeedbackTime between cast('08:00:00' as time) and cast('08:59:59' as time) then '8Hrs'
               when FeedbackTime between cast('09:00:00' as time) and cast('09:59:59' as time) then '9Hrs'
               when FeedbackTime between cast('10:00:00' as time) and cast('10:59:59' as time) then '10Hrs'
               when FeedbackTime between cast('11:00:00' as time) and cast('11:59:59' as time) then '11Hrs'
               when FeedbackTime between cast('12:00:00' as time) and cast('12:59:59' as time) then '12Hrs'
               when FeedbackTime between cast('13:00:00' as time) and cast('13:59:59' as time) then '13Hrs'
               when FeedbackTime between cast('14:00:00' as time) and cast('14:59:59' as time) then '14Hrs'
               when FeedbackTime between cast('15:00:00' as time) and cast('15:59:59' as time) then '15Hrs'
               when FeedbackTime between cast('16:00:00' as time) and cast('16:59:59' as time) then '16Hrs'
               when FeedbackTime between cast('17:00:00' as time) and cast('17:59:59' as time) then '17Hrs'
               when FeedbackTime between cast('18:00:00' as time) and cast('18:59:59' as time) then '18Hrs'
               when FeedbackTime between cast('19:00:00' as time) and cast('19:59:59' as time) then '19Hrs'
               when FeedbackTime between cast('20:00:00' as time) and cast('20:59:59' as time) then '20Hrs'
               when FeedbackTime between cast('21:00:00' as time) and cast('21:59:59' as time) then '21Hrs'
               when FeedbackTime between cast('22:00:00' as time) and cast('22:59:59' as time) then '22Hrs'
          end
From cte1)
Select 
    CollectorId
    ,Hrs = CAST(Hrs as Varchar(4)) + 'Hrs'
    ,FeedbackHrs
    ,DispositionCode
From HoursCte h
Left join cte2 c2 on CAST(h.Hrs as Varchar(4)) + 'Hrs' = c2.FeedbackHrs

然后我就迷路了。

【问题讨论】:

  • 您需要使用 Pivot 函数才能从行更改为列

标签: sql sql-server tsql sql-server-2012


【解决方案1】:

小修改的相同方法:

Declare @T table
(AGREEMENTID varchar(50),
FeedbackDate varchar(50),
DispositionCode varchar(10),
[CollectorId] varchar(100)
)
Insert into @T 
SELECT '0003SBML00151','2017-08-22 08:00:00.000','PTP','1' union all
SELECT '0004SBHL00705','2017-08-22 08:20:00.000','BPTP','1' union all
SELECT '0007SBML01987','2017-08-22 09:10:00.000','BPTP','1' union all
SELECT '0026MSS00108','2017-08-21 07:50:00.000','PTP','2' union all
SELECT '0026MSS00108','2017-08-21 10:30:00.000','NC','2' union all
SELECT '0026MSS00108','2017-08-21 10:30:10.000','DL' ,'2' union all
SELECT '0026MSS00108','2017-08-21 15:47:00.000','CB' ,'2' union all
SELECT '0026MSS00108','2017-08-21 19:52:00.000','BPTP','2' 

    ;with cte1 as(
SELECT 
    CollectorId
    ,AGREEMENTID
    ,FeedbackDate
    ,FeedbackTime = CONVERT(nvarchar,CAST(FeedbackDate as time),108)
    ,DispositionCode
FROM @T t1)

Select *, case when FeedbackTime between cast('07:00:00' as time) and cast('07:59:59' as time) then '7Hrs'
               when FeedbackTime between cast('08:00:00' as time) and cast('08:59:59' as time) then '8Hrs'
               when FeedbackTime between cast('09:00:00' as time) and cast('09:59:59' as time) then '9Hrs'
               when FeedbackTime between cast('10:00:00' as time) and cast('10:59:59' as time) then '10Hrs'
               when FeedbackTime between cast('11:00:00' as time) and cast('11:59:59' as time) then '11Hrs'
               when FeedbackTime between cast('12:00:00' as time) and cast('12:59:59' as time) then '12Hrs'
               when FeedbackTime between cast('13:00:00' as time) and cast('13:59:59' as time) then '13Hrs'
               when FeedbackTime between cast('14:00:00' as time) and cast('14:59:59' as time) then '14Hrs'
               when FeedbackTime between cast('15:00:00' as time) and cast('15:59:59' as time) then '15Hrs'
               when FeedbackTime between cast('16:00:00' as time) and cast('16:59:59' as time) then '16Hrs'
               when FeedbackTime between cast('17:00:00' as time) and cast('17:59:59' as time) then '17Hrs'
               when FeedbackTime between cast('18:00:00' as time) and cast('18:59:59' as time) then '18Hrs'
               when FeedbackTime between cast('19:00:00' as time) and cast('19:59:59' as time) then '19Hrs'
               when FeedbackTime between cast('20:00:00' as time) and cast('20:59:59' as time) then '20Hrs'
               when FeedbackTime between cast('21:00:00' as time) and cast('21:59:59' as time) then '21Hrs'
               when FeedbackTime between cast('22:00:00' as time) and cast('22:59:59' as time) then '22Hrs'
          end collectedhours into #test
From cte1
select * from #test

select CollectorId,  isnull([7Hrs], '')as[7Hrs], 
 isnull([8Hrs], '')as[8Hrs], 
 isnull([9Hrs], '')as[9Hrs], 
 isnull([10Hrs],'')as[10Hrs], 
 isnull([11Hrs],'')as[11Hrs], 
 isnull([12Hrs],'')as[12Hrs], 
 isnull([13Hrs],'')as[13Hrs], 
 isnull([14Hrs],'')as[14Hrs], 
 isnull([15Hrs],'')as[15Hrs], 
 isnull([16Hrs],'')as[16Hrs], 
 isnull([17Hrs],'')as[17Hrs],
 isnull([18Hrs],'')as[18Hrs], 
 isnull([19Hrs],'')as[19Hrs], 
 isnull([20Hrs],'')as[20Hrs], 
 isnull([21Hrs],'')as[21Hrs], 
 isnull([22Hrs] ,'')as[22Hrs]
 from (
select distinct CollectorId,collectedhours,stuff((select ','+DispositionCode from #test a where a.CollectorId=b.CollectorId
and a.collectedhours=b.collectedhours
 group by CollectorId,DispositionCode for xml path('')), 1,1,'')DispositionCode
 from #test b)z
 pivot
 (max(DispositionCode) for  collectedhours in([7Hrs], [8Hrs], [9Hrs], [10Hrs], [11Hrs], [12Hrs], [13Hrs], [14Hrs], [15Hrs], [16Hrs], [17Hrs],
 [18Hrs], [19Hrs], [20Hrs], [21Hrs], [22Hrs]))as pvt  

【讨论】:

【解决方案2】:

使用PIVOT 运算符可以实现相同的输出。我正在考虑反馈日期可以包括当天的所有记录。 Demo Example

SELECT *
FROM 
(
    SELECT    CollectorId, 
              DispositionCode = (STUFF((
              SELECT ', ' + CONVERT(VARCHAR(10), t2.[DispositionCode], 103)
              FROM   @T as t2
              WHERE  t2.[CollectorId] = t.[CollectorId]
              AND   CAST(DATEPART(HOUR, t2.FeedbackDate) as VARCHAR(10)) = CAST(DATEPART(HOUR, t.FeedbackDate) as VARCHAR(10))
              FOR XML PATH(''), TYPE).value('.', 'VARCHAR(MAX)'), 1, 2, '')), 
              CAST(DATEPART(HOUR, FeedbackDate) as VARCHAR(10)) + 'Hrs' AS SearchHour
              FROM @T as t          
              GROUP BY  CollectorId, CAST(DATEPART(HOUR, FeedbackDate) as VARCHAR(10)) 
) PivotSource
 PIVOT (MAX([DispositionCode]) FOR SearchHour IN 
         ( [0Hrs],  [1Hrs],  [2Hrs],  [3Hrs],  [4Hrs],  [5Hrs], 
           [6Hrs],  [7Hrs],  [8Hrs],  [9Hrs], [10Hrs], [11Hrs], 
           [12Hrs], [13Hrs], [14Hrs], [15Hrs], [16Hrs], [17Hrs], 
           [18Hrs], [19Hrs], [20Hrs], [21Hrs], [22Hrs], [23Hrs])) as pvt

【讨论】:

猜你喜欢
  • 2020-06-21
  • 2023-03-11
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
相关资源
最近更新 更多