包括2个SUM函数和2个条件

时间:2019-01-15 10:05:40

标签: sql sql-server tsql

如何在SELECT语句中获取两个不同的where条件,以便可以按月对SUM分组?

SELECT
[tpdb].[tLedgerEntry].CalendarMonth AS Måned,
(select SUM(Amount) FROM [tpdb].[tLedgerEntry] where CompanyId = 'Nordlux A/S' and CalendarYear = 2018 and LedgerAccount>=6001 and LedgerAccount <= 6090) AS Varelager,
(select SUM(Amount) FROM [tpdb].[tLedgerEntry] where CompanyId = 'Nordlux A/S' and CalendarYear = 2018 and LedgerAccount>=6100 and LedgerAccount <= 6156) AS Debitorer
FROM [tpdb].[tLedgerEntry]
GROUP BY [tpdb].[tLedgerEntry].CalendarMonth

这是我现在得到的结果,这是错误的。

NULL    32582633.07 13901648.50
1   32582633.07 13901648.50
2   32582633.07 13901648.50
3   32582633.07 13901648.50
4   32582633.07 13901648.50
5   32582633.07 13901648.50
6   32582633.07 13901648.50
7   32582633.07 13901648.50
8   32582633.07 13901648.50
9   32582633.07 13901648.50
10  32582633.07 13901648.50
11  32582633.07 13901648.50
12  32582633.07 13901648.50

4 个答案:

答案 0 :(得分:2)

尝试一下:

SELECT
[tpdb].[tLedgerEntry].CalendarMonth AS Måned,
SUM(IIF(LedgerAccount>=6001 and LedgerAccount <= 6090, Amount, 0))  AS Varelager,
SUM(IIF(LedgerAccount>=6100 and LedgerAccount <= 6156, Amount, 0))  AS Debitorer
FROM [tpdb].[tLedgerEntry]
 where CompanyId = 'Nordlux A/S' and CalendarYear = 2018
GROUP BY [tpdb].[tLedgerEntry].CalendarMonth

答案 1 :(得分:1)

尝试将情况用作条件聚合

SELECT
[tpdb].[tLedgerEntry].CalendarMonth AS Måned,
SUM(case when CompanyId = 'Nordlux A/S' and CalendarYear = 2018 and LedgerAccount>=6001 and LedgerAccount <= 6090 then Amount end) AS Varelager,
SUM(case when CompanyId = 'Nordlux A/S' and CalendarYear = 2018 and LedgerAccount>=6100 and LedgerAccount <= 6156 then Amount end) AS Debitorer
FROM [tpdb].[tLedgerEntry]
GROUP BY [tpdb].[tLedgerEntry].CalendarMonth

答案 2 :(得分:0)

用例何时

 select [tpdb].[tLedgerEntry].CalendarMonth AS Måned,
                         sum(case when CompanyId = 'Nordlux A/S' 
                              and CalendarYear = 2018
                              and LedgerAccount>=6001 
                              and LedgerAccount <= 6090 
                              then Amount else 0 end) Varelager,
                        sum(case when CompanyId = 'Nordlux A/S' 
                             and CalendarYear = 2018 
                             and LedgerAccount>=6100 
                             and LedgerAccount <= 6156 
                             then amount else 0 end) Debitorer
    from [tpdb].[tLedgerEntry]
    group by [tpdb].[tLedgerEntry].CalendarMonth 

答案 3 :(得分:0)

使用相关子查询。

SELECT
    t1.CalendarMonth AS Måned,
    (
      select SUM(Amount) 
      FROM [tpdb].[tLedgerEntry] t2
      where t2.CompanyId = 'Nordlux A/S' and 
            t2.CalendarYear = 2018 and 
            t2.LedgerAccount>=6001 and 
            t2.LedgerAccount <= 6090 and
            t2.CalendarMonth = t1.CalendarMonth
    ) AS Varelager,
    (
      select SUM(Amount) 
      FROM [tpdb].[tLedgerEntry] t2
      where t2.CompanyId = 'Nordlux A/S' and 
            t2.CalendarYear = 2018 and 
            t2.LedgerAccount>=6100 and 
            t2.LedgerAccount <= 6156 and
            t2.CalendarMonth = t1.CalendarMonth
    ) AS Debitorer, 
FROM (
   SELECT distinct CalendarMonth  FROM [tpdb].[tLedgerEntry]
) t1

如果您在CalendarMonth, CalendarYear, LedgerAccount上具有索引并且选择性很高,那么相关子查询可能具有更好的performance

此外,pushing the distinct into a subquery也可能对您的查询有益。