I have a complex scenario which I will try to explain.
I have a Column Named "Phase" from ProjectPhases table which holds the description about the project phases. Then there is table called DailyReport Contains fields Such as Desription ,QTY1,QTY2,PHASEID (Foreign key from ProjectPhases table ) etc.
Now the USER in the front end will create Daily Report records for Each day in a week . Now I wrote query to summarise the daily report containing the Data from ProjectPhases as well the data from Daily report.
select P.projectname ,PP.Description as Phase,DD.description, DD.QTYSunday,DD.QTYSMonday,DD.QTYTues,
from
Document_DailyReport DD LEFT OUTER JOIN
ProjectPhases PP on PP.Id=DD.PhaseId LEFT OUTER JOIN
Projects P on P.Id=DD.ProjectId
The output of the query is in the below format
ProjectName Phase Description QTYSunday QTYMonday QtyTues
Project1 Phase 1 Qty-SUNDAy 10
Project1 Phase 1 Qty-Monday 10
Project1 Phase 1 Qty-Monday 10
Now I want the output to be in the below format
ProjectName Phase QTYSunday QTYMonday QtyTues
Project1 Phase 1 10 10 10
I want all the records of the daily report for a particular phase in a single line like above.
Thanks !
EDITED :
My full query below.
SELECT P.projectname ,
PP.Description AS Phase,
DD.Description,
DD.DocNumber,
(SELECT CompanyName
FROM Companies
WHERE Id=dbo.GetCompanyIdByUser(DD.InsertedBy)) AS CreatedBy,
DD.ReportDate,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, -(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantitySun,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, 1-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityMon,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, 2-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityTues,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, 3-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityWed,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, 4-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityThur,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, 5-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantityFri,
(SELECT sum(quantity)+sum(cast(field9 AS INT))
FROM Document_DailyReportOnSite
WHERE DailyReportId=DD.Id
AND ClassificationId=1
AND DATEDIFF(DAY,reportdate,DATEADD(dd, 6-(DATEPART(dw, (@Date)-1)), @Date)) =0) AS StaffQuantitySat ,
FROM Document_DailyReport DD
LEFT OUTER JOIN ProjectPhases PP ON PP.Id=DD.PhaseId
LEFT OUTER JOIN Projects P ON P.Id=DD.ProjectId
WHERE DD.ReportDate BETWEEN DATEADD(dd, -(DATEPART(dw,@Date)-1), @Date) AND DATEADD(dd, 6-(DATEPART(dw, @Date)-1), @Date)
OUTPUT
projectname Phase description CreatedBy 0StaffQuantityMon StaffQuantityTues StaffQuantityWed StaffQuantityThur
Bollywood Park (MCC-ARCO) Bollywood Theatre Main Contractor Package Daily Report as on 16-Jun-2014 ARCO Contracting 22 NULL NULL NULL
Bollywood Park (MCC-ARCO) Bollywood Theatre Main Contractor Package Daily Report as om 17-Jun-2014 ARCO Contracting NULL 23 NULl NULL
Bollywood Park (MCC-ARCO) Bollywood Theatre Main Contractor Package Daily Report as on 18.06.2014 ARCO Contracting NULL NULL NULL 23