2015-12-03 46 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

其實我沒有得到你。你可以說得更詳細點嗎。其實我已經在這裏硬編碼發票_價值。但我不知道你到底想要什麼。 –