使用group by查找所有记录的总和

时间:2018-05-15 03:06:25

标签: sql-server select group-by aggregate

我正在构建SQL Query 此查询必须显示每个供应商的最高付费供应商和每月付款的详细信息,同时显示供应商的总付款

VendorTable
Vendor varchar(100)
PayDate Date
Amt int

-

Vendor   PayDate    Amt
Vendor1  01-Jan-15  $70
Vendor1  13-Jan-15  $30
Vendor2  04-Mar-16  $180
Vendor2  21-Mar-16  $70
Vendor3  17-Sep-16  $300

最终结果的示例

Vendor   Month   MonthlyAmt   TotalAmt
Vendor1  Jan-15  $100         $870
Vendor2  Mar-16  $250         $900
Vendor3  Sep-16  $300         $720
Vendor1  Dec-16  $140         $870
Vendor2  Feb-17  $210         $900
Vendor1  Apr-17  $400         $870
Vendor3  Aug-17  $420         $900
Vendor2  Nov-17  $330         $900
Vendor2  Mar-18  $110         $900
Vendor1  May-18  $230         $870

我的SELECT看起来像这样

SELECT TOP 10 Vendor, Month(PayDate) + '-' + Year(PayDate), SUM(Amt), ????????
FROM VendorsTable
GROUP BY Vendor, Month(PayDate) + '-' + Year(PayDate)
ORDER BY ???????? DESC

不确定如何查找供应商的所有付款总额

4 个答案:

答案 0 :(得分:1)

似乎ROLLUP选项就是您所需要的:

SELECT TOP 10 Vendor, Month(PayDate) + '-' + Year(PayDate), SUM(Amt), ????????
FROM VendorsTable
GROUP BY Vendor, Month(PayDate) + '-' + Year(PayDate)
WITH ROLLUP
ORDER BY ???????? DESC

每个供应商和日期,您将获得一个额外的行,每个供应商和每个供应商的总数,加上总数与所有商品的总和

https://docs.microsoft.com/en-us/sql/t-sql/queries/select-group-by-transact-sql?view=sql-server-2017

答案 1 :(得分:1)

好的,放弃我之前的回答,我没有意识到你在帖子中显示的表是结果而不是原始数据。这意味着您必须使用窗口函数来解决您的问题:

with samplevalues as
(
select
    *
from
    (values 
        ('Vendor1', 'Jan-15', 100, 870),
        ('Vendor2', 'Mar-16', 250,  900),
        ('Vendor3', 'Sep-16',   300, 720),
        ('Vendor1', 'Dec-16',   140, 870),
        ('Vendor2', 'Feb-17',   210, 900),
        ('Vendor1', 'Apr-17',   400, 870),
        ('Vendor3', 'Aug-17',   420, 900),
        ('Vendor2', 'Nov-17',   330, 900),
        ('Vendor2', 'Mar-18',   110, 900),
        ('Vendor1', 'May-18 ',  230, $870)
    ) T(Vendor, [Month], MonthlyAmt, TotalAmt)
)
select
    Vendor, [Month], SUM(MonthlyAmt) OVER (PARTITION BY Vendor)
from
    samplevalues

应该完全符合您的需要。

此处有更多信息或OVER条款:

https://docs.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-2017

答案 2 :(得分:1)

试试这个......

SELECT t1.vendor, 
       Replace(RIGHT(CONVERT(NVARCHAR(9), t1.paydate, 6), 6), ' ', '-') AS PayDate, 
       Sum(t1.amt)                                                      AS MonthlyAmt, 
       Max(t2.totalamt)                                                 AS TotalAmt 
FROM   tablename t1 
       INNER JOIN (SELECT vendor, Sum(amt) AS TotalAmt 
                   FROM   tablename 
                   GROUP  BY vendor) t2 
               ON t1.vendor = t2.vendor 
GROUP  BY t1.vendor, Replace(RIGHT(CONVERT(NVARCHAR(9), t1.paydate, 6), 6), ' ', '-') 

输出
请注意,这些样本数据与您的数据不同

+---------+---------+------------+----------+
| vendor  | PayDate | MonthlyAmt | TotalAmt |
+---------+---------+------------+----------+
| Vendor1 | Jan-15  |        100 |      110 |
| Vendor1 | Jan-16  |         10 |      110 |
| Vendor2 | Mar-16  |        250 |      250 |
| Vendor3 | Sep-16  |        300 |      300 |
+---------+---------+------------+----------+

十大供应商&个月

SELECT TOP 10 t1.vendor, 
              Replace(RIGHT(CONVERT(NVARCHAR(9), t1.paydate, 6), 6), ' ', '-') AS PayDate, 
              Sum(t1.amt) AS MonthlyAmt, 
              Max(t2.totalamt) AS TotalAmt 
FROM   tablename t1 
       INNER JOIN (SELECT vendor, Sum(amt) AS TotalAmt 
                   FROM   tablename 
                   GROUP  BY vendor) t2 
               ON t1.vendor = t2.vendor 
GROUP  BY t1.vendor, Replace(RIGHT(CONVERT(NVARCHAR(9), t1.paydate, 6), 6), ' ', '-') 
ORDER  BY monthlyamt DESC 

输出

+---------+---------+------------+----------+
| vendor  | PayDate | MonthlyAmt | TotalAmt |
+---------+---------+------------+----------+
| Vendor3 | Sep-16  |        300 |      300 |
| Vendor2 | Mar-16  |        250 |      250 |
| Vendor1 | Jan-15  |        100 |      110 |
| Vendor1 | Jan-16  |         10 |      110 |
+---------+---------+------------+----------+

参考
- http://www.sql-server-helper.com/tips/date-formats.aspx

答案 3 :(得分:1)

这有用吗?

SELECT DISTINCT Vendor
      ,Replace(RIGHT(CONVERT(NVARCHAR(9), paydate, 6), 6), ' ', '-') AS Month
      ,SUM(Amt) OVER(Partition by Vendor,Replace(RIGHT(CONVERT(NVARCHAR(9), paydate, 6), 6), ' ', '-')) As MonthlyAmt,
       SUM(Amt) OVER(Partition by Vendor) AS TotalAmt
FROM VendorTable  

使用order by子句:

;with cte
 AS
 (
   SELECT DISTINCT Vendor,Replace(RIGHT(CONVERT(NVARCHAR(9), paydate, 6), 6), ' ', '-') AS Month
     ,SUM(Amt) OVER(Partition by Vendor,Replace(RIGHT(CONVERT(NVARCHAR(9), paydate, 6), 6), ' ', '-')) As MonthlyAmt,
     SUM(Amt) OVER(Partition by Vendor) AS TotalAmt
  FROM VendorTable   
 )

 select Vendor,Month,MonthlyAmt,TotalAmt
 FROM cte 
 ORDER BY CAST('20'+SUBSTRING(Month,5,2) + '-' +SUBSTRING(Month,0,4) + '-' + '01' AS DATE)

SQL小提琴链接: http://sqlfiddle.com/#!18/26ef4/99/0