0

我有一个表spending,其中包含一些基本字段,这些字段定义了购买项目、购买时间和购买价值,以及它们是从哪个帐户购买的:spItemspAmount和。spDatespAccount

我已经进行了查询,以每月对它们进行分组,一次一个帐户,效果很好:

SELECT YEAR(spDate) as spYear,
       MONTH(spDate) as spMonth,
       SUM(spAmount) AS TotalAmount
FROM spending
WHERE spAccount like 'Bank1'
GROUP BY YEAR(spDate), MONTH(spDate)
ORDER BY YEAR(spDate), MONTH(spDate)

SELECT YEAR(spDate) as spYear,
       MONTH(spDate) as spMonth,
       SUM(spAmount) AS TotalAmount
FROM spending
WHERE spAccount like 'Bank2'
GROUP BY YEAR(spDate), MONTH(spDate)
ORDER BY YEAR(spDate), MONTH(spDate)

我正在尝试获取一个可以组合两个查询并显示Bank1到一列和Bank2下一列的表。可能有几个月其中一个账户没有支出。在这种情况下,它应该显示一个零值,而不仅仅是跳过它。

任何能以正确方式指出我的人都将不胜感激。

4

4 回答 4

0

您可以使用带有条件聚合的分组来在一个查询中获取两个帐户的总计。这是它的样子:

SELECT YEAR(spDate) as spYear,
       MONTH(spDate) as spMonth,
       SUM(CASE spAccount WHEN 'Bank1' THEN spAmount ELSE 0 END) AS Bank1,
       SUM(CASE spAccount WHEN 'Bank2' THEN spAmount ELSE 0 END) AS Bank2
FROM spending
WHERE spAccount IN ('Bank1', 'Bank2')
GROUP BY YEAR(spDate), MONTH(spDate)
ORDER BY YEAR(spDate), MONTH(spDate)
;
于 2012-10-03T10:26:08.563 回答
0

我希望我正确理解了你的问题:-)

对于每个可用的月份和年份(数据存在于至少一个帐户中)显示每个帐户的总和(即使其中一个为 0)。为此,我加入了 2 个查询并使用 ISNULL 函数从正确的源获取数据。

这是我使用虚拟数据的解决方案:

--Preparing Dummy Data
;WITH spending
 AS (SELECT '01-01-2010' spDate, 
            100          spamount, 
            'Bank1'      spaccount 
     UNION 
     SELECT '01-01-2010' spDate, 
            200          spamount, 
            'Bank1'      spaccount 
     UNION 
     SELECT '03-01-2010' spDate, 
            150          spamount, 
            'Bank1'      spaccount 
     UNION 
     SELECT '04-01-2010' spDate, 
            500          spamount, 
            'Bank1'      spaccount 
     UNION 
     SELECT '01-01-2010' spDate, 
            200          spamount, 
            'Bank1'      spaccount 
     UNION 
     SELECT '03-01-2010' spDate, 
            10           spamount, 
            'Bank2'      spaccount 
     UNION 
     SELECT '05-01-2010' spDate, 
            800          spamount, 
            'Bank2'      spaccount 
     UNION 
     SELECT '03-01-2010' spDate, 
            110          spamount, 
            'Bank2'      spaccount 
     UNION 
     SELECT '01-01-2010' spDate, 
            100          spamount, 
            'Bank2'      spaccount) 
--End of Dummy Data

--Start Query on Dummy Data
SELECT ISNULL(Bank1.SPMONTH, Bank2.SPMONTH) AS spMonth, 
       ISNULL(Bank1.SPYEAR, Bank2.SPYEAR)   AS spYear, 
       ISNULL(Bank1.TOTALAMOUNT, 0)         AS TotalAmount_Bank1, 
       ISNULL(Bank2.TOTALAMOUNT, 0)         AS TotalAmount_Bank2 
FROM   (SELECT YEAR(SPDATE)  AS spYear, 
               MONTH(SPDATE) AS spMonth, 
               SUM(SPAMOUNT) AS TotalAmount 
        FROM   spending 
        WHERE  SPACCOUNT LIKE 'Bank1' 
        GROUP  BY YEAR(SPDATE), 
                  MONTH(SPDATE)) AS Bank1 
       FULL JOIN (SELECT YEAR(SPDATE)  AS spYear, 
                         MONTH(SPDATE) AS spMonth, 
                         SUM(SPAMOUNT) AS TotalAmount 
                  FROM   spending 
                  WHERE  SPACCOUNT LIKE 'Bank2' 
                  GROUP  BY YEAR(SPDATE), 
                            MONTH(SPDATE)) AS Bank2 
              ON Bank1.SPMONTH = Bank2.SPMONTH 
                 AND Bank2.SPYEAR = Bank2.SPYEAR 
ORDER  BY SPYEAR, 
          SPMONTH 
于 2012-10-03T09:05:57.073 回答
0

在日期上进行选择,将年月组合为一个字段,该字段将相应地进行排序和分组。还要确保首先按帐户分组,因为 SQL 中的分组顺序很重要。

于 2012-10-03T00:34:35.830 回答
0
SELECT YEAR(spDate) as spYear, MONTH(spDate) as spMonth, SUM(spAmount) AS TotalAmount,(SELECT SUM(spAmount) AS TotalAmount2 FROM spending WHERE spAccount like 'Bank2' GROUP BY YEAR(spDate), MONTH(spDate) ORDER BY YEAR(spDate), MONTH(spDate)) FROM spending WHERE spAccount like 'Bank1' GROUP BY YEAR(spDate), MONTH(spDate) ORDER BY YEAR(spDate), MONTH(spDate)
于 2012-10-03T00:48:20.190 回答