This is the report which I need to create in SQL
The 3 columns Swing
, Turnback
and Inday
are types of volume requests made by accounts. They are stored in VolumeRequest
table which stores request by account and the start and end dates for the requests as below
VolumeRequestTypeId
2 stands for Swing
, 3 for Turnback
and 4 for Inday
The 3 columns FL Gas Zn1 (GDA)
, FL Gas Zn2 (GDA)
and FL Gas Zn3 (GDA)
and indexes which have daily prices (Midpoint
) stored in the IndexHistory
table as below
I need to display the various volume requests related to accounts with accountids 202,203,205 for a given month. The report must have a row for every day in the month hence I need to sum up the requests for a day for each type of volume requests
In order to achieve this I started to apply the solution I found twice for both tables like below
DECLARE @startdate DATE
SET @startdate = '03/01/2013'
SELECT DISTINCT IH.Flowdate AS [Date],
CASE WHEN VR.VolumeRequestTypeId=2 AND IH.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) END AS Swing,
CASE WHEN VR.VolumeRequestTypeId=3 AND IH.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) END AS Turnback,
CASE WHEN VR.VolumeRequestTypeId=4 AND IH.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) END AS Inday,
CASE WHEN IH.Indexid=412 THEN IH.Midpoint END AS [FL Gas Zn1 (GDA)],
CASE WHEN IH.Indexid=420 THEN IH.Midpoint END AS [FL Gas Zn2 (GDA)],
CASE WHEN IH.Indexid=463 THEN IH.Midpoint END AS [FL Gas Zn3 (GDA)]
FROM IndexHistory IH
LEFT JOIN VolumeRequest VR
ON IH.FlowDate BETWEEN @startdate AND EOMONTH(@startdate,0) AND indexid in (412,463,420) AND VR.EndDate>= @startdate AND VR.StartDate <= EOMONTH(@startdate,0)
INNER JOIN Account A
ON A.Id=VR.accountId
INNER JOIN VolumeRequestType VRT
ON VR.VolumeRequestTypeId=VRT.Id
WHERE VRT.Id in (2,3,4) AND ISNULL(VR.Deprecated,0)<>1 AND A.Id in (202,203,205) AND ISNULL(A.Status,'open')<>'closed'
GROUP BY VR.VolumeRequestTypeId, IH.Flowdate, VR.Volume, IH.Indexid, IH.Midpoint,VR.StartDate,VR.EndDate
ORDER BY [Date]
but it returned the below table
As it was not the result required I did 3 joins to the IndexHistory
for the three indexes as below
DECLARE @startdate DATE
SET @startdate = '03/01/2013'
SELECT DISTINCT IH1.Flowdate AS [Date],
CASE WHEN VR.VolumeRequestTypeId=2 AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) END AS Swing,
CASE WHEN VR.VolumeRequestTypeId=3 AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) END AS Turnback,
CASE WHEN VR.VolumeRequestTypeId=4 AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) END AS Inday,
IH1.Midpoint AS [FL Gas Zn1 (GDA)],
IH2.Midpoint AS [FL Gas Zn2 (GDA)],
IH3.Midpoint AS [FL Gas Zn3 (GDA)]
FROM IndexHistory IH1
LEFT JOIN VolumeRequest VR ON IH1.FlowDate BETWEEN @startdate AND EOMONTH(@startdate,0) AND IH1.indexid = 412 AND VR.EndDate>= @startdate AND VR.StartDate <= EOMONTH(@startdate,0)
INNER JOIN IndexHistory IH2 ON IH2.FlowDate = IH1.FlowDate AND IH2.indexid = 420
INNER JOIN IndexHistory IH3 ON IH3.FlowDate = IH1.FlowDate AND IH3.indexid = 463
INNER JOIN Account A ON A.Id=VR.accountId
INNER JOIN VolumeRequestType VRT ON VR.VolumeRequestTypeId=VRT.Id
WHERE VRT.Id in (2,3,4) AND ISNULL(VR.Deprecated,0)<>1 AND A.Id in (202,203,205) AND ISNULL(A.Status,'open')<>'closed'
GROUP BY VR.VolumeRequestTypeId, IH1.Flowdate, VR.Volume, IH1.Midpoint,IH2.Midpoint, IH3.Midpoint, VR.StartDate,VR.EndDate
ORDER BY [Date]
This returned the following result
As I was the sum of the volume was broken down more than required because of the GROUP BY
. I wanted to try `OVER' and tried it like below
DECLARE @startdate DATE
SET @startdate = '03/01/2013'
SELECT DISTINCT IH1.Flowdate AS [Date],
CASE WHEN VR.VolumeRequestTypeId=2 AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) OVER (PARTITION BY IH1.Flowdate) END AS Swing,
CASE WHEN VR.VolumeRequestTypeId=3 AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) OVER (PARTITION BY IH1.Flowdate) END AS Turnback,
CASE WHEN VR.VolumeRequestTypeId=4 AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate THEN SUM(VR.Volume) OVER (PARTITION BY IH1.Flowdate) END AS Inday,
IH1.Midpoint AS [FL Gas Zn1 (GDA)],
IH2.Midpoint AS [FL Gas Zn2 (GDA)],
IH3.Midpoint AS [FL Gas Zn3 (GDA)]
FROM IndexHistory IH1
LEFT JOIN VolumeRequest VR ON IH1.FlowDate BETWEEN @startdate AND EOMONTH(@startdate,0) AND IH1.indexid = 412 AND VR.EndDate>= @startdate AND VR.StartDate <= EOMONTH(@startdate,0)
INNER JOIN IndexHistory IH2 ON IH2.FlowDate = IH1.FlowDate AND IH2.indexid = 420
INNER JOIN IndexHistory IH3 ON IH3.FlowDate = IH1.FlowDate AND IH3.indexid = 463
INNER JOIN Account A ON A.Id=VR.accountId
INNER JOIN VolumeRequestType VRT ON VR.VolumeRequestTypeId=VRT.Id
WHERE VRT.Id in (2,3,4) AND ISNULL(VR.Deprecated,0)<>1 AND A.Id in (202,203,205) AND ISNULL(A.Status,'open')<>'closed' AND IH1.Flowdate BETWEEN VR.StartDate AND VR.EndDate
ORDER BY [Date]
This time everything was right expect that that days with out requests are not displayed as below I don't know where I am going wrong. Can someone help me with this?