SQL Group By和其他列的总和

时间:2019-09-12 10:30:11

标签: sql sql-server

以下是原始表

+--------------+---------------+----------+---------+-----------+
| CustomerName | InvoiceNumber | ItemRate | TaxRate | TaxAmount |
+--------------+---------------+----------+---------+-----------+
| A            |             1 |      500 | 4%      | 20        |
| B            |             2 |      300 | 9%      | 27        |
| B            |             2 |      100 | 9%      | 9         |
| B            |             2 |      400 | 4%      | 16        |
| C            |             3 |      250 | 9%      | 22.5      |
| C            |             3 |      650 | 4%      | 26        |
| D            |             4 |      200 | 4%      | 8         |
| D            |             4 |      100 | 4%      | 4         |
+--------------+---------------+----------+---------+-----------+

我想要这样的输出:

+--------------+---------------+--------------+---------+-----------+
| CustomerName | InvoiceNumber | InvoiceTotal | TaxRate | TaxAmount |
+--------------+---------------+--------------+---------+-----------+
| A            |             1 |          500 | 4%      | 20        |
| B            |             2 |          800 | 9%      | 36        |
| B            |             2 |          800 | 4%      | 16        |
| C            |             3 |          900 | 9%      | 22.5      |
| C            |             3 |          900 | 4%      | 26        |
| D            |             4 |          300 | 4%      | 12        |
+--------------+---------------+--------------+---------+-----------+

进行分组依据时,发票值总和有问题。 我必须使用“ TaxRate”进行分组,以组合类似的“ Taxsate”,并且我希望在结果中包括新列,以显示该特定发票的总计。

“发票总计”列应包含整个发票的总和,因为我当前的查询是按税收组而不是整个发票对“总计”求和。

我的查询是:

Select CustomerName, InvoiceNumber, sum(itemrate) as InvoiceTotal, TaxRate, sum(TaxAmount) 
from salestable group by customername,invoicenumber, Taxrate

请帮助解决此问题。

谢谢

3 个答案:

答案 0 :(得分:3)

如果我理解正确,您希望对每个客户在发票上加itemrate

select CustomerName, InvoiceNumber,
       sum(sum(itemrate)) over (partition by CustomerName, InvoiceNumber) as InvoiceTotal,
       TaxRate,
       sum(TaxAmount) as TaxAmount
from salestable
group by customername, invoicenumber, Taxrate

答案 1 :(得分:1)

试试这个-

WITH CTE AS
(
    SELECT CustomerName,SUM(ItemRate)  InvoiceTotal
    FROM your_table
    GROUP BY CustomerName
)

SELECT A.CustomerName,A.InvoiceNumber,B.InvoiceTotal,A.TaxRate,SUM(A.TaxAmount)
FROM your_table A
LEFT JOIN CTE B ON A.CustomerName = B.CustomerName
GROUP BY A.CustomerName,A.InvoiceNumber,B.InvoiceTotal,A.TaxRate
ORDER BY A.CustomerName,A.TaxRate DESC

答案 2 :(得分:0)

类似的东西:

SELECT  tab.CustomerName,
        tab.InvoiceNumber,
        it.InvoiceTotal,
        tab.TaxRate,
        SUM(tab.TaxAmount) AS TaxAmount
  FROM tab
    INNER JOIN (
                SELECT InvoiceNumber, SUM(ItemRate) AS InvoiceTotal FROM tab GROUP BY InvoiceNumber
               ) it ON it.InvoiceNumber = tab.InvoiceNumber
  GROUP BY tab.CustomerName, tab.InvoiceNumber, it.InvoiceTotal, tab.TaxRate
  ORDER BY tab.CustomerName, tab.TaxRate DESC

请参阅相关的fiddle

结果:

A   1   500 4   20
B   2   800 9   36
B   2   800 4   16
C   3   900 9   22.5
C   3   900 4   26
D   4   300 4   12