使用日期参数“运行总和”

时间:2014-06-17 18:39:16

标签: sql sql-server ssms

我目前正在管理Studio 2012中编写SQL查询。我需要合并一个Date参数来收集TotalContributorsTotalCharts等的总和。直到给出的日期。

DECLARE @DateEnd DATETIME = '06/13/2014'
DECLARE @GeneralStats TABLE (
       TotalPapers INT,
   SiteID BIGINT,
   SiteEventGroupID INT,
   EventCreateDate DATETIME,
   AssignmentType VARCHAR(20),
   AssignmentTypeIndex INT
    );
INSERT INTO @GeneralStats
        ( TotalPapers ,
         SiteID ,
         SiteEventGroupID,
     EventCreateDate,
     AssignmentType,
     AssignmentTypeIndex
        )
SELECT 
   TotalPapers ,
   SiteID,
   SiteEventGroupID,
   EventCreateDate,
CASE
    WHEN TotalPapers BETWEEN 1 AND 4 THEN '1-4 Papers'  
    WHEN TotalPapers BETWEEN 5 AND 9 THEN '5-9 Papers'  
    WHEN TotalPapers BETWEEN 10 AND 24 THEN '10-24 Papers'  
    WHEN TotalPapers BETWEEN 25 AND 49 THEN '25-49 Papers'  
    WHEN TotalPapers BETWEEN 50 AND 99 THEN '50-99 Papers'  
    WHEN TotalPapers BETWEEN 100 AND 249 THEN '100-249 Papers'  
    WHEN TotalPapers BETWEEN 250 AND 499 THEN '250-499 Papers'  
    WHEN TotalPapers BETWEEN 500 AND 1000 THEN '500-1000 Papers'  
    WHEN TotalPapers > 1000 THEN 'Greater 1000'
END AssignmentType,
CASE
    WHEN TotalPapers BETWEEN 1 AND 4 THEN '1'  
    WHEN TotalPapers BETWEEN 5 AND 9 THEN '2'  
    WHEN TotalPapers BETWEEN 10 AND 24 THEN '3'  
    WHEN TotalPapers BETWEEN 25 AND 49 THEN '4'  
    WHEN TotalPapers BETWEEN 50 AND 99 THEN '5'  
    WHEN TotalPapers BETWEEN 100 AND 249 THEN '6'  
    WHEN TotalPapers BETWEEN 250 AND 499 THEN '7'  
    WHEN TotalPapers BETWEEN 500 AND 1000 THEN '8'  
    WHEN TotalPapers > 1000 THEN '9'
END AssignmentTypeIndex
FROM (
SELECT
    COUNT(c.PaperID) TotalPapers,
    c.SiteID, 
    wfseg.SiteEventGroupID,
    wfse.EventCreateDate
FROM
    Sites s 
    INNER JOIN Papers c ON s.SiteID = c.SiteID
    LEFT JOIN WFSiteEvents wfse ON s.SiteID = wfse.SiteID AND EventStatusID IN (1,2)
    LEFT JOIN WFSiteEventTypes wfset ON wfse.EventTypeID = wfset.EventTypeID
    LEFT JOIN WFSiteEventGroups wfseg ON wfset.SiteEventGroupID = wfseg.SiteEventGroupID
WHERE
    c.SiteID <> 110
GROUP BY
    c.SiteID, 
    wfseg.SiteEventGroupID,
    wfse.EventCreateDate
) d
ORDER BY SiteID

SELECT  
 Main.CreateDate,
 Main.AssignmentType,
    Main.Contributors as TotalContributors,
    Main.Papers as TotalPapers,
    CONVERT(DECIMAL(10,2),Main.[%]) [Total%],
    ISNULL(Type1.Contributors, 0) Type1Contributors ,
    ISNULL(Type1.Papers,0) Type1Papers, 
    ISNULL(CONVERT(DECIMAL(10,2),Type1.[%]),0) [Type1%],
    ISNULL(Type2.Contributors, 0) Type2Contributors ,
    ISNULL(Type2.Papers, 0) Type2Papers ,
    ISNULL(CONVERT(DECIMAL(10,2),Type2.[%]),0) [Type2%],
    ISNULL(Type3.Contributors,0) Type3Contributors ,
    ISNULL(Type3.Papers, 0) Type3Papers ,
    ISNULL(CONVERT(DECIMAL(10,2),Type3.[%]), 0) [Type3%],
    ISNULL(Type4.Contributors, 0) Type4Contributors ,
    ISNULL(Type4.Papers, 0) Type4Papers ,
    ISNULL(CONVERT(DECIMAL(10,2),Type4.[%]), 0) [Type4%],
    ISNULL(Type5.Contributors, 0) Type5Contributors ,
    ISNULL(Type5.Papers, 0) Type5Papers ,
    ISNULL(CONVERT(DECIMAL(10,2),Type5.[%]), 0) [Type5%]
FROM 
(
SELECT 
    CONVERT(varchar,EventCreateDate,101) as CreateDate,
    AssignmentTypeIndex,
    AssignmentType, 
    COUNT(DISTINCT SiteID) Contributors, 
    SUM(TotalPapers) Papers, 
    SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers) FROM @GeneralStats)  [%]
FROM @GeneralStats
WHERE 
    CONVERT(VARCHAR,EventCreateDate, 101) <= @DateEnd
GROUP BY AssignmentType, AssignmentTypeIndex, CONVERT(varchar,EventCreateDate,101)
) Main
LEFT JOIN
(
SELECT 
    CONVERT(varchar,EventCreateDate,101) as CreateDate,
    AssignmentType, 
    COUNT(DISTINCT SiteID) Contributors, 
    SUM(TotalPapers) Papers, 
    SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers) FROM @GeneralStats WHERE SiteEventGroupID = 3)  [%]
FROM @GeneralStats
WHERE SiteEventGroupID = 3
    AND CONVERT(VARCHAR,EventCreateDate, 101) <= @DateEnd
GROUP BY AssignmentType, CONVERT(varchar,EventCreateDate,101)
)Type1 ON  Main.AssignmentType = Type1.AssignmentType
LEFT JOIN
(
SELECT 
    CONVERT(varchar,EventCreateDate,101) as CreateDate,
    AssignmentType, 
    COUNT(DISTINCT SiteID) Contributors, 
    SUM(TotalPapers) Papers, 
    SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers) FROM @GeneralStats WHERE SiteEventGroupID = 4)  [%]
FROM @GeneralStats
WHERE SiteEventGroupID = 4
    AND CONVERT(VARCHAR,EventCreateDate, 101) <= @DateEnd
GROUP BY AssignmentType, CONVERT(varchar,EventCreateDate,101)
)Type2 ON  Main.AssignmentType = Type2.AssignmentType
LEFT JOIN
(
SELECT 
    CONVERT(varchar,EventCreateDate,101) as CreateDate,
    AssignmentType, 
    COUNT(DISTINCT SiteID) Contributors, 
    SUM(TotalPapers) Papers, 
    SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers) FROM @GeneralStats WHERE SiteEventGroupID = 2)  [%]
FROM @GeneralStats
WHERE SiteEventGroupID = 2
    AND CONVERT(VARCHAR,EventCreateDate, 101) <= @DateEnd
GROUP BY AssignmentType, CONVERT(varchar,EventCreateDate,101)
)Type3 ON  Main.AssignmentType = Type3.AssignmentType
LEFT JOIN
(
SELECT 
    CONVERT(varchar,EventCreateDate,101) as CreateDate,
    AssignmentType, 
    COUNT(DISTINCT SiteID) Contributors, 
    SUM(TotalPapers) Papers, 
    SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers) FROM @GeneralStats WHERE SiteEventGroupID = 9)  [%]
FROM @GeneralStats
WHERE SiteEventGroupID = 9
    AND CONVERT(VARCHAR,EventCreateDate, 101) <= @DateEnd
GROUP BY AssignmentType, CONVERT(varchar,EventCreateDate,101) 
)Type4 ON  Main.AssignmentType = Type4.AssignmentType
LEFT JOIN
(
SELECT 
    CONVERT(varchar,EventCreateDate,101) as CreateDate,
    AssignmentType, 
    COUNT(DISTINCT SiteID) Contributors, 
    SUM(TotalPapers) Papers, 
    SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers) FROM @GeneralStats WHERE SiteEventGroupID = 8)  [%]
FROM @GeneralStats
WHERE SiteEventGroupID = 8
    AND CONVERT(VARCHAR,EventCreateDate, 101) <= @DateEnd
GROUP BY AssignmentType, CONVERT(varchar,EventCreateDate,101) 
)Type5 ON  Main.AssignmentType = Type5.AssignmentType

ORDER BY AssignmentTypeIndex

我在DECLARE @DateEnd DATETIME = '06/13/2014'添加了@DateEnd并添加CreateDate | AssignmentType | TotalContributors | TotalPapers | Total% | Type1Contributors | Type1Papers | Type1% | Type2Contributors | Type2Papers | Type2% | Type3Contributors | Type3Papers | Type3% | Type4Contributors | Type4Papers | Type4% | Type5Contributors | Type5Papers | Type5% ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 04/28/2014 | 1-4 Papers | 11329 | 17478 | 2.24 | 19 | 42 | 0.02 | 6 | 11 | 0.01 | 4 | 9 | 0.00 | 1 | 1 | 0.00% | 7 | 10 | 0.09 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 04/28/2014 | 1-4 Papers | 11329 | 17478 | 2.24 | 19 | 42 | 0.02 | 6 | 11 | 0.01 | 4 | 9 | 0.00 | 1 | 1 | 0.00% | 3 | 8 | 0.07 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ 04/28/2014 | 1-4 Papers | 11329 | 17478 | 2.24 | 19 | 42 | 0.02 | 6 | 11 | 0.01 | 4 | 9 | 0.00 | 1 | 1 | 0.00% | 2 | 7 | 0.06 -- etc. Running it actually takes FOREVER and I've never seen the end of it ,但它并没有真正给我我想要的结果,也没有正确的格式。

现在如何发布:

CreateDate | AssignmentType | TotalContributors | TotalPapers | Total% | Type1Contributors | Type1Papers | Type1% | Type2Contributors | Type2Papers | Type2% | Type3Contributors | Type3Papers | Type3% | Type4Contributors | Type4Papers | Type4% |  Type5Contributors | Type5Papers | Type5%
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
06/13/2014 | 1-4 Papers     | --Total#upto6/13  -> for all rows 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
06/13/2014 | 5-9 Papers     | --Total#upto6/13  -> for all rows 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
06/13/2014 | 10-24 Papers   | --Total#upto6/13  -> for all rows 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
06/13/2014 | 25-49 Papers   | --Total#upto6/13  -> for all rows 
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
06/13/2014 | 50-99 Papers   | --Total#upto6/13  -> for all rows 

我希望它看起来如何:

{{1}}

2 个答案:

答案 0 :(得分:0)

这可以通过以下方式实现,这是一种有助于理解的简单方法。

假设您有一个如下表,名为tblLedger

ID     | Date        | Amount
1      | 2014-01-01  | 100.00
2      | 2014-01-15  | 75.00
3      | 2014-01-15  | -25.00

查询:

SELECT T1.ID, T1.Date, T1.Amount, SUM(T2.Amount) AS 'Balance'
FROM tblLedger T1
JOIN tblLedger T2 ON T1.ID>=T2.ID
GROUP BY T1.ID, T1.Date, T1.Amount

Here is a fiddle demo

答案 1 :(得分:0)

所以我在SQLFiddle做了一点。这应该相对较快。记下创建的额外表,但主要是我们对主数据集进行了几次子类型查询(很像你的方法),但它在6个查询和6个聚合中进行了15列工作,而不是6个查询和15个聚合。之后,重型击球手应该是Pivot操作员。这是明确的...架构:

CREATE TABLE Sites 
(
  SiteID INT
 );

INSERT INTO Sites (SiteID)
VALUES (12345),(67890),(09876),(54321),(13579),(24680),(12457),(23679),(09126),(12094);

CREATE TABLE Papers
(
  SiteID INT,
  PaperID INT
 );

INSERT INTO Papers (SiteID,PaperID)
VALUES (12345,123456),(12345,789012),(12345,165655),(12345,165656),
(67890,165676),(67890,165677),(67890,165657),(67890,165658),(67890,165659),(67890,165660),
(09876,165661),(09876,165662),(09876,165663),(09876,165664),(09876,165665),
(54321,165666),(54321,165667),(54321,165668),
(13579,165669),
(24680,165670),
(12457,165671),
(23679,165672),(23679,165673),(23679,165674),(23679,165675),(23679,165702),(23679,165703),(23679,165704),(23679,165705),(23679,165706),(23679,165707),(23679,165708),
(09126,165709),(09126,165710),(09126,165711),(09126,165712),(09126,165713),(09126,165714),(09126,165715),(09126,165716),(09126,165717),(09126,165718),(09126,165719),
(12094,165720),(12094,165721),(12094,165722),(12094,165723);

CREATE TABLE WFSiteEvents
(
  SiteEventID INT,
  SiteID INT,
  EventStatusID INT,
  EventTypeID INT,
  EventCreateDate DATETIME 
 );

INSERT INTO WFSiteEvents (SiteEventID,SiteID,EventStatusID,EventTypeID,EventCreateDate)
VALUES (21487,12345,1,1220,'2014-06-04 15:35:19.027'),
(21483,12345,3,1000,'2014-06-04 15:30:36.000'),
(28000,12345,8,1000,'2014-04-28 08:00:00.000'),
(16660,67890,1,1210,'2014-04-28 08:00:00.000'),
(18742,09876,1,1103,'2014-05-28 13:47:51.033'),
(28002,09876,3,1000,'2014-04-28 08:00:00.000'),
(25575,54321,1,1103,'2014-06-16 10:14:54.030'),
(28007,54321,3,1000,'2014-04-28 08:00:00.000'),
(25572,54321,3,1000,'2014-06-16 10:11:04.000'),
(11345,54321,8,1305,'2014-04-29 16:44:56.000'),
(25082,13579,1,1103,'2014-06-13 09:59:04.690'),
(28013,13579,3,1000,'2014-04-28 08:00:00.000'),
(17716,13579,3,1101,'2014-05-23 10:49:14.000'),
(11330,24680,1,1305,'2014-04-29 15:55:24.440'),
(28006,24680,3,1000,'2014-04-28 08:00:00.000'),
(25097,12457,1,1220,'2014-06-13 10:15:31.047'),
(18664,12457,3,1210,'2014-05-28 12:08:05.000'),
(18507,12457,3,1000,'2014-05-28 09:03:44.000'),
(28408,12457,8,1000,'2014-04-28 08:00:00.000'),
(49182,23679,1,1405,'2014-05-23 10:49:14.000'),
(59283,23679,3,1000,'2014-04-28 08:00:00.000'),
(17589,09126,1,1211,'2014-05-22 16:36:54.870'),
(17587,09126,3,1211,'2014-05-22 16:36:42.000'),
(17203,09126,3,1220,'2014-05-22 08:51:14.000'),
(82253,09126,8,1000,'2014-04-28 08:00:00.000'),
(17186,12094,1,1101,'2014-05-22 08:28:59.500'),
(82254,12094,3,1000,'2014-04-28 08:00:00.000');

CREATE TABLE WFSiteEventTypes
(
  EventTypeID INT,
  SiteEventGroupID INT
 );

INSERT INTO WFSiteEventTypes(EventTypeID,SiteEventGroupID)
VALUES (1220,4),
(1000,1),
(1103,2),
(1210,3),
(1305,8),
(1405,9),
(1211,3);

CREATE TABLE WFSiteEventGroups
(
  SiteEventGroupID INT
 );

INSERT INTO WFSiteEventGroups (SiteEventGroupID)
VALUES (1),(2),(3),(4),(6),(7),(8),(9)

CREATE TABLE PaperAssignmentDivisions(
    ID INT PRIMARY KEY IDENTITY(1,1)
  , MinAssign INT NOT NULL
  , MaxAssign INT
);
INSERT INTO PaperAssignmentDivisions (MinAssign, MaxAssign) VALUES
    (1,4)
  , (5,9)
  , (10,24)
  , (25,49)
  , (50,99)
  , (100,249)
  , (250,499)
  , (500,1000)
  , (1001,NULL);

CREATE TABLE SiteEventGroupIDToType (
    SiteEventGroupID INT
  , Type INT
  );
INSERT INTO SiteEventGroupIDToType VALUES
    (3,1)
  , (4,2)
  , (2,3)
  , (9,4)
  , (8,5);

CREATE TABLE GeneralStats (
    TotalPapers INT,
    SiteID BIGINT,
    Type INT,
    EventCreateDate DATETIME,
    AssignmentType VARCHAR(20),
    AssignmentTypeIndex INT
);

INSERT INTO GeneralStats
  (
  TotalPapers ,
  SiteID ,
  Type,
  EventCreateDate,
  AssignmentType,
  AssignmentTypeIndex
  )
SELECT 
    TotalPapers ,
    SiteID,
    stt.Type,
    EventCreateDate,
    CASE
        WHEN MaxAssign IS NULL THEN 'Greater Than ' + (SELECT CAST(MAX(MaxAssign) AS VARCHAR(6)) FROM PaperAssignmentDivisions)
        ELSE CAST(MinAssign AS VARCHAR(6)) + '-' + CAST(MaxAssign AS VARCHAR(6)) + ' Papers'  
    END AssignmentType,
    pad.ID AssignmentTypeIndex
FROM (
    SELECT
        COUNT(c.PaperID) TotalPapers,
        c.SiteID, 
        wfseg.SiteEventGroupID,
        wfse.EventCreateDate
    FROM
        Sites s 
        INNER JOIN Papers c ON s.SiteID = c.SiteID
        LEFT JOIN WFSiteEvents wfse ON s.SiteID = wfse.SiteID AND EventStatusID IN (1,2)
        LEFT JOIN WFSiteEventTypes wfset ON wfse.EventTypeID = wfset.EventTypeID
        LEFT JOIN WFSiteEventGroups wfseg ON wfset.SiteEventGroupID = wfseg.SiteEventGroupID
    WHERE
        c.SiteID <> 110
    GROUP BY
        c.SiteID, 
        wfseg.SiteEventGroupID,
        wfse.EventCreateDate
    ) d
  INNER JOIN PaperAssignmentDivisions pad ON TotalPapers BETWEEN pad.MinAssign AND ISNULL(pad.MaxAssign,2147483647)
  INNER JOIN SiteEventGroupIDToType stt ON stt.SiteEventGroupID = d.SiteEventGroupID
ORDER BY SiteID

查询:

SELECT
    AssignmentType
  , ISNULL(TotalContributers,0) AS TotalContributers
  , ISNULL(TotalPapers,0) AS TotalPapers
  , ISNULL([Total%],0) AS [Total%]
  , ISNULL(Type1Contributers,0) AS Type1Contributers
  , ISNULL(Type1Papers,0) AS Type1Papers
  , ISNULL([Type1%],0) AS [Type1%]
  , ISNULL(Type2Contributers,0) AS Type2Contributers
  , ISNULL(Type2Papers,0) AS Type2Papers
  , ISNULL([Type2%],0) AS [Type2%]
  , ISNULL(Type3Contributers,0) AS Type3Contributers
  , ISNULL(Type3Papers,0) AS Type3Papers
  , ISNULL([Type3%],0) AS [Type3%]
  , ISNULL(Type4Contributers,0) AS Type4Contributers
  , ISNULL(Type4Papers,0) AS Type4Papers
  , ISNULL([Type4%],0) AS [Type4%]
  , ISNULL(Type5Contributers,0) AS Type5Contributers
  , ISNULL(Type5Papers,0) AS Type5Papers
  , ISNULL([Type5%],0) AS [Type5%]

FROM (
  SELECT
    'TotalContributers' AS AttributeName
    , COUNT(DISTINCT SiteID) AS AttributeValue
    , ID AS AssignmentTypeIndex
    , CASE
        WHEN MaxAssign IS NULL THEN 'Greater Than ' + (SELECT CAST(MAX(MaxAssign) AS VARCHAR(6)) FROM PaperAssignmentDivisions)
        ELSE CAST(MinAssign AS VARCHAR(6)) + '-' + CAST(MaxAssign AS VARCHAR(6)) + ' Papers'  
    END AS AssignmentType
  FROM PaperAssignmentDivisions
    LEFT JOIN GeneralStats ON ID = Type
  WHERE EventCreateDate <= (SELECT MAX(EventCreateDate-1) FROM GeneralStats)
  GROUP BY MinAssign, MaxAssign, ID
  UNION ALL
  SELECT
    'TotalPapers' AS AttributeName
    , SUM(TotalPapers) AS AttributeValue
    , AssignmentTypeIndex
    , AssignmentType
  FROM GeneralStats A
  WHERE EventCreateDate <= (SELECT MAX(EventCreateDate-1) FROM GeneralStats)
  GROUP BY AssignmentTypeIndex, AssignmentType
  UNION ALL
  SELECT
    'Total%' AS AttributeName
    , SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers)*1.0 FROM GeneralStats B) AS AttributeValue
    , AssignmentTypeIndex
    , AssignmentType
  FROM GeneralStats A
  WHERE EventCreateDate <= (SELECT MAX(EventCreateDate-1) FROM GeneralStats)
  GROUP BY AssignmentTypeIndex, AssignmentType
  UNION ALL
  SELECT
    'Type' +
    CAST(Type AS VARCHAR(2))
    + 'Contributers' AS AttributeName
    , COUNT(DISTINCT SiteID) AS AttributeValue
    , AssignmentTypeIndex
    , AssignmentType
  FROM GeneralStats A
  WHERE EventCreateDate <= (SELECT MAX(EventCreateDate-1) FROM GeneralStats)
  GROUP BY Type, AssignmentTypeIndex, AssignmentType
  UNION ALL
  SELECT
    'Type' +
    CAST(Type AS VARCHAR(2))
    + 'Papers' AS AttributeName
    , SUM(TotalPapers) AS AttributeValue
    , AssignmentTypeIndex
    , AssignmentType
  FROM GeneralStats A
  WHERE EventCreateDate <= (SELECT MAX(EventCreateDate-1) FROM GeneralStats)
  GROUP BY Type, AssignmentTypeIndex, AssignmentType
  UNION ALL
  SELECT
    'Type' +
    CAST(Type AS VARCHAR(2))
    + '%' AS AttributeName
    , SUM(TotalPapers)*100.0/(SELECT SUM(TotalPapers)*1.0 FROM GeneralStats B WHERE A.Type = B.Type) AS AttributeValue
    , AssignmentTypeIndex
    , AssignmentType
  FROM GeneralStats A
  WHERE EventCreateDate <= (SELECT MAX(EventCreateDate-1) FROM GeneralStats)
  GROUP BY Type, AssignmentTypeIndex, AssignmentType
  ) P
  PIVOT (
    SUM(AttributeValue) FOR AttributeName IN
    (TotalContributers,Type1Contributers,Type2Contributers,Type3Contributers,Type4Contributers,Type5Contributers
    ,TotalPapers,Type1Papers,Type2Papers,Type3Papers,Type4Papers,Type5Papers
    ,[Total%],[Type1%],[Type2%],[Type3%],[Type4%],[Type5%]
    )
    ) PVT
ORDER BY AssignmentTypeIndex;