Hi I am trying to create the second table below:
To summarise the number of days it took to complete a SN_TYPE by SN_STATUS
Hope someone can help, I tried with a sub query but didn't work. Any help/suggestions would be greatly appreciated.
HAVE
SERVICE_NUMBER IDENTIFYER CREATED_DATE COMPLETED_DATE SN_TYPE SN_STATUS
318403853 61030203647 12/05/2017 18/05/2017 De-Activated COMP
318616723 61030203647 13/06/2017 13/06/2017 Re-Activattion N-CO
318637013 61030203647 15/06/2017 15/06/2017 Re-Activattion REJE
318639191 61030203647 15/06/2017 16/06/2017 Re-Activattion COMP
318637791 61030203666 15/06/2017 16/06/2017 Re-Activattion Rejected
318637792 61030203666 14/06/2017 15/06/2017 Re-Activattion N-CO
WANT
IDENTIFYER CREATED_DATE REQUESTED_DATE SN_TYPE SN_STATUS DAYS TO COMPLETE
61030203647 12/05/2017 18/05/2017 De-Activated COMP 6
61030203647 13/06/2017 16/06/2017 Re-Activattion COMP 3
61030203666 14/06/2017 16/06/2017 Re-Activattion Rejected 2
Code
SELECT DISTINCT D.SERVICE_NUMBER
,D.IDENTIFYER
,D.CREATED_DATE
,D.REQUESTED_DATE
,D.SN_TYPE
,D.SN_STATUS
,DATEDIFF(DAY,DATB.FIRSTS_ON_BP,DATB.LASTS_ON_BP) AS '#DAYS_Re-energisation'
FROM [SAP_Reporting_Users].[dbo].[A139075_DAN] D
LEFT JOIN
(
SELECT
FB.SN_TYPE
,FB.IDENTIFYER
,MIN(FB.CREATED_DATE) AS FIRSTS_ON_BP
,MAX(FB.REQUESTED_DATE) AS LASTS_ON_BP
FROM [SAP_Reporting_Users].[dbo].[A139075_DAN] FB
--WHERE FB.SN_STATUS = 'COMP'
GROUP By
FB.SN_TYPE
,FB.IDENTIFYER
) as DATB
ON DATB.SN_TYPE = D.SN_TYPE
AND DATB.IDENTIFYER = D.IDENTIFYER
Tried the query above but with not much success. Hope someone can help me expand.
Thanks
Danii