0

This is the report which I need to create in SQLReport

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 belowVolme Request

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 belowIndex History

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 tableresult1

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 resultresult2

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 result3 I don't know where I am going wrong. Can someone help me with this?

4

1 回答 1

2

I'll just start by saying that you've made this incredibly hard by providing data that doesn't match up - the first image has March 2013 data, the second June 12, the third July 12. As such, it is difficult to figure out exactly where data comes from and how you want it applied.

Anyway, this is what I've come up with. I haven't included the Account table (or restricted it by account) so if you choose to use it, you'll have to make a few modifications

;WITH Gas AS (
  SELECT FlowDate, [412] as [FL Gas Zn1 (GDA)], [463] as [FL Gas Zn2 (GDA)], [420] as [FL Gas Zn3 (GDA)]
  FROM (
    SELECT IndexId, FlowDate, Midpoint
    FROM IndexHistory
  ) p
  PIVOT (max(Midpoint) FOR IndexId IN ([412], [463], [420])) AS pvt
), 
Volume AS (
  SELECT FlowDate, [Swing], [Turnback], [Inlay]
  FROM (
    SELECT DISTINCT IH.Flowdate, VRT.Name, VR.Volume
    FROM IndexHistory IH
    INNER JOIN VolumeRequest VR ON IH.FlowDate BETWEEN @startdate AND '2012-06-30' AND indexid IN (412,463,420) AND VR.EndDate>= @startdate AND VR.StartDate <= '2012-06-30'
    INNER JOIN VolumeRequestType VRT ON VR.VolumeRequestTypeId = VRT.Id
  ) p
  PIVOT (sum(Volume) FOR Name IN ([Swing], [Turnback], [Inlay])) AS pvt
)
SELECT G.Flowdate, Swing, Turnback, Inlay, [FL Gas Zn1 (GDA)], [FL Gas Zn2 (GDA)], [FL Gas Zn3 (GDA)]
FROM Gas G
INNER JOIN Volume V ON G.FlowDate = V.FlowDate

I've made extensive use of PIVOTs and CTEs for this

SQL Fiddle here

于 2013-08-02T00:32:14.693 回答