我有一個表作爲下如何設置不同時間間隔的值?
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
的最終輸出將是
手段,集電極1已經在8.00小時和8.20小時了兩個配置。因此,對於8小時的專欄,他的處置代碼是可用的。他還於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
而且我以後丟失。
需要幫助。
您需要使用數據透視功能這是能夠從行改爲列 – Darka