2015-11-20 266 views
-1

我有一張表,我需要在發票上創建三個基於月份的總和。SQL嵌套總和查詢

使用「invoice_number」字段的前三個字母計算總和,然後爲該特定發票類型添加所有「Invoice_Amount」。數據

例 INVOICE_NUMBER - MSP-1111 發票金額 - $ 2100.00

我要那麼它格式,例如計算這個基礎關閉幾個月

Year | Month | Total MSP Invoices| Total PS Invoices | Total App Invoice 

我能夠得到的幾個月裏,然而,所有月份的總數都是相同的,而不是特定於那個月。以下是我正在使用的查詢。

我想顯示過去12個月。

select year(date_invoice) as Year, 
    Case month(date_invoice) 
     When 1 Then 'Jan' 
     When 2 Then 'Feb' 
     When 3 Then 'March' 
     When 4 Then 'April' 
     When 5 Then 'May' 
     When 6 Then 'June' 
     When 7 Then 'July' 
     When 8 Then 'Aug' 
     When 9 Then 'Sept' 
     When 10 Then 'Oct' 
     When 11 Then 'Nov' 
     When 12 then 'Dec' 
    End 
as Month, 
(Select sum(invoice_amount) from invoices where Invoice_number like 'MSP%') as 'MSP', 
(Select sum(invoice_amount) from invoices where Invoice_number like 'PS%') as'PS', 
(Select sum(invoice_amount) from invoices where Invoice_number like 'APP%') as 'App' 
from invoices 
Where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate()) 
Group by year(date_invoice), month(date_invoice) 
Order by year(date_invoice), month(date_invoice) 

任何幫助都會很棒!

+1

您正在使用哪些DBMS? –

回答

0

你應該能夠在那裏你把CASE表達拿到一個月的名稱使用的情況下聲明這將組是正確的

select year(date_invoice) as Year, 
Case month(date_invoice) 
    When 1 Then 'Jan' 
    When 2 Then 'Feb' 
    When 3 Then 'March' 
    When 4 Then 'April' 
    When 5 Then 'May' 
    When 6 Then 'June' 
    When 7 Then 'July' 
    When 8 Then 'Aug' 
    When 9 Then 'Sept' 
    When 10 Then 'Oct' 
    When 11 Then 'Nov' 
    When 12 then 'Dec' 
End 
as Month, 
(Select sum(invoice_amount) from invoices where Invoice_number like 'MSP%') as 'MSP', 
(Select sum(invoice_amount) from invoices where Invoice_number like 'PS%') as'PS', 
(Select sum(invoice_amount) from invoices where Invoice_number like 'APP%') as 'App' 
from invoices 
Where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate()) 
Group by year(date_invoice), Case month(date_invoice) 
    When 1 Then 'Jan' 
    When 2 Then 'Feb' 
    When 3 Then 'March' 
    When 4 Then 'April' 
    When 5 Then 'May' 
    When 6 Then 'June' 
    When 7 Then 'July' 
    When 8 Then 'Aug' 
    When 9 Then 'Sept' 
    When 10 Then 'Oct' 
    When 11 Then 'Nov' 
    When 12 then 'Dec' 
End 
Order by year(date_invoice), month(date_invoice) 
+0

爲什麼不使用cte或派生表來節省大量輸入? – jarlh

+0

沒有理由,你不能。只是我的做法 –

+0

當我嘗試這種方法時,我得到以下內容,「invoices.Date_Invoice」在ORDER BY子句中無效,因爲它不包含在聚合函數或GROUP BY子句中。「 – k1pp3r

0

有一個派生表在一組。

使用CASE表達式來做條件SUM's。

select year, month, 
     sum(case when Invoice_number like 'MSP%' then invoice_amount end) as 'MSP', 
     sum(case when Invoice_number like 'PS%' then invoice_amount end) as 'PS', 
     sum(case when Invoice_number like 'APP%' then invoice_amount end) as 'APP' 
from 
(
select date_invoice, 
     Invoice_number 
     year(date_invoice) as Year, 
     case month(date_invoice) 
     When 1 Then 'Jan' 
     When 2 Then 'Feb' 
     When 3 Then 'March' 
     When 4 Then 'April' 
     When 5 Then 'May' 
     When 6 Then 'June' 
     When 7 Then 'July' 
     When 8 Then 'Aug' 
     When 9 Then 'Sept' 
     When 10 Then 'Oct' 
     When 11 Then 'Nov' 
     When 12 then 'Dec' 
     End as Month, 
     invoice_amount 
from invoices 
where convert(nvarchar(50), date_invoice,100) > DATEADD(month, -12, getdate()) 
) as dt 
Group by year, month 
Order by year, month 
+0

您應該將'where '在派生表內,所以可以使用日期索引(我認爲應該是'日期'的一個),並且不帶所有的發票 –

+0

@JuanCarlosOropeza,好點。但是現在我不會編輯 - 因爲如果涉及派生表時可以使用索引,或者不使用,則取決於所使用的dbms,並且此處不標記dbms。 (也許以後,當我們知道它是哪個dbms的時候。)順便說一句,不是至少在某些產品中轉換了破壞索引的使用情況嗎? – jarlh

+0

即使沒有索引,你也在爲所有行而不是僅僅過去的12個月做一些「昂貴」的計算「year(),month()case'。 –

0

選擇年,月, 總和(情況下INVOICE_NUMBER像 'MSP%' 然後INVOICE_AMOUNT結束)爲 'MSP', 總和(情況下INVOICE_NUMBER像 'PS%',那麼INVOICE_AMOUNT結束)爲「PS ' 總和(情況下INVOICE_NUMBER像 'APP%' 然後INVOICE_AMOUNT結束)爲 'APP' 從 ( 選擇date_invoice, INVOICE_NUMBER 年(date_invoice)爲一年, 情況下一個月(date_invoice) 當1然後' Jan' When 2 Then'Feb' When 3 Then'March' 當4接着「四月」 當5接着「月」 當6於是「六月」 當7接着「月」 當8接着「月」 當9於是「九月」 當10然後「月」 當11然後 '月' 當12隨後 '月' 完如月, INVOICE_AMOUNT 從發票 其中轉換(爲nvarchar(50),date_invoice,100)> DATEADD(月,-12,GETDATE()) ) as dt 按年份,月份分組 按年,月排序