一个选择中有两个不同的分组级别

时间:2015-12-03 03:17:38

标签: sql sql-server

我有这个:

SELECT 
invoice_number, invoice_year, invoice_month, invoice_amount,
    payment_year, payment_month, payment_amount
FROM payments_table

结果:

Result

所以我有4张发票。 2015/01年度两张发票的发票金额合计为900件,2015/02年度的两张发票合计为950件

我想要这个结果:

Grouped

所以我想只使用invoice_amount invoice_yearinvoice_monthinvoice_number求和一次。我想按payment_amountinvoice_yearinvoice_monthpayment_yearpayment_month求和。

如果我使用GROUP BY invoice_year, invoice_month, payment_year, payment_month,我会获得SUM(payment_amount)的正确金额,但我得到SUM(invoice_amount)的错误金额。

有什么建议吗?

4 个答案:

答案 0 :(得分:1)

您需要的查询是:

select a.invoice_year, a.invoice_month, a.payment_year, a.payment_month,
       SUM(payment_amount), b.sumup
  from payments_table a
        inner join
         (select invoice_year, invoice_month, sum(payment_amount) sumup
            from payments_table
           group by invoice_year, invoice_month) b
        ON (a.invoice_year = b.invoice_year
            and a.invoice_month = b.invoice_month )
 GROUP BY a.invoice_year, a.invoice_month, a.payment_year, a.payment_month  

但请允许我说,对于您提供的示例数据,invoice_year and invoice_month的总和为900而不是950.

在这里查看小提琴:http://sqlfiddle.com/#!9/46249/4

请注意,我在MySql中做了小提琴,但对于SQLServer应该是相同的,因为没有特定的函数或语法,只有普通的SQL。我之所以在Mysql中这样做是因为有时SQLFiddle和SQLServer会变得不稳定。

修改

原来我总结了错误的字段并错过了一列,所以正确的查询应该是:

select a.invoice_year, a.invoice_month, 
       b.incount,
       SUM(payment_amount) invoice_amount,
       a.payment_year, 
       a.payment_month, 
       b.payment__amount
  from payments_table a
       inner join
       (select invoice_year, invoice_month, 
               count(distinct invoice_amount) incount, 
               sum(distinct invoice_amount) payment__amount
          from payments_table
        group by invoice_year, invoice_month) b
       ON (     a.invoice_year = b.invoice_year
           and  a.invoice_month = b.invoice_month )
 GROUP BY a.invoice_year, a.invoice_month, a.payment_year, a.payment_month  

这将根据您的需要为您提供结果。请在此处查看:http://sqlfiddle.com/#!9/46249/10

答案 1 :(得分:1)

在这个sql小提琴,http://sqlfiddle.com/#!6/7d789/27中,我打破了你的查询的组件,并从小部分中提出了一个完整的查询来获得你想要的东西。

答案 2 :(得分:0)

您可以找到此查询。

SELECT Invoice_Year,
Invoice_Month,
CASE WHEN (SUM(Invoice_amount) <= 900 AND Invoice_Month=1 ) THEN 900 
WHEN (SUM(Invoice_amount) <= 950 AND Invoice_Month=2) THEN 950 END AS InvoiceAmount,
Payment_year,
Payment_Month,
SUM(Payment_Amount) as PaymentAmount
FROM @table 
GROUP BY 
Payment_Month,
Invoice_Month,
Invoice_Year,
Payment_year
ORDER BY Invoice_Month

请找附件。

SQL Fiddle

谢谢

答案 3 :(得分:0)

我自己找到了解决方案。谢谢大家一起思考:

select b.invoice_year
, b.invoice_month
, b.invoice_amount
, c.payment_year
, c.payment_month
, c.payment_amount

from (
    select a.invoice_year
    , a.invoice_month
    , sum(a.invoice_amount) as invoice_amount

    from (
        select distinct invoice_number
        , invoice_year
        , invoice_month
        , invoice_amount 
        from payments
        ) a

    group by a.invoice_year
    , a.invoice_month
    ) b
inner join (
    select a.invoice_year
    , a.invoice_month
    , a.payment_year
    , a.payment_month
    , sum(a.payment_amount) as payment_amount

    from (
        select invoice_year
        , invoice_month
        , payment_year
        , payment_month
        , payment_amount 
        from payments
        ) a

    group by a.invoice_year
    , a.invoice_month
    , a.payment_year
    , a.payment_month
    ) as c
    on c.invoice_year = b.invoice_year
    and c.invoice_month = b.invoice_month

这给了我正在寻找的结果。