2015-12-03 24 views
0

我有这样的:两种不同的分组层次选择

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 2张发票加起来950

我想这样的结果:

Grouped

所以我想求和invoice_amountinvoice_yearinvoice_month仅使用invoice_number一次。我想总结payment_amountinvoice_year,invoice_month,payment_yearpayment_month

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

有什么建议吗?

+0

你的结果对我没有意义。为什么第4个月的付款包含这两个月的发票? –

+0

如果您的数据与您显示的样本类似,则可以只执行SUM(DISTINCT invoice_amount)。 – Munir

+0

@munochtractor这是危险的,你假设两张不同的发票不能有相同的发票金额 – Rabbit

回答

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 

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

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中使用它,是因为SQLServer SQLFiddle有时会变得不稳定。

编辑

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

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

+0

因为字段b.sumup不包含在GROUP BY子句中,所以在SQL Server中出现运行此错误。我们有两个invoice_year和invoice_month的组合。 2015/01年的总额应为900(发票#1001 = 500 +发票#1002 = 400),2015/02的总和应为950(发票#1003 = 600 +发票#1004 = 350)。 –

+0

您的编辑现在确实为invoice_year和invoice_month提供了正确的invoice_amount,但您假定在一个月内invoice_amount是唯一的。销售相同的产品将导致许多发票金额相同。在SQL Server中,我仍然遇到错误,因为列不在GROUP BY子句中。 –

1

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

+0

考虑发布并简要解释这里的查询。当SQL FIddle关闭时,您的答案变得不可用 –

+0

它给我的payment_amounts是他们应该是的两倍。例如:2015/01和2015/01的invoice_year,invoice_month,payment_year,payment_month的组合出现两次,payment_amounts为100和50.因此,我应该看到150.但我看到300. –

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

谢谢

+0

请考虑发布并在此处简要解释查询。当SQL FIddle关闭时,您的答案变得不可用 –

+0

这给了我想要的东西,但是已经硬编码了invoice_amounts。这些在现实中会有所不同。 –

+0

其实我没有得到你。你可以说得更详细点吗。其实我已经在这里硬编码发票_价值。但我不知道你到底想要什么。 –