sql嵌套求和查询

时间:2015-11-20 14:56:12

标签: 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)

任何帮助都会很棒!

3 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

有一个派生表,您可以在其中放置CASE表达式来获取月份名称。

使用CASE表达式执行条件SUM

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

答案 2 :(得分:0)

选择年,月,        sum(Invoice_number喜欢' MSP%'然后invoice_amount结束)为' MSP',        总和(Invoice_number喜欢' PS%'然后invoice_amount结束时的情况)为' PS',        总和(Invoice_number喜欢' APP%'然后invoice_amount结束)的情况为' APP' 从 ( 选择date_invoice,        发票编号        年(date_invoice)为年,        案例月(date_invoice)          当1然后' Jan'          当2然后' 2月'          当3然后'三月'          当4然后'四月'          当5然后'五月'          当6然后'六月'          当7然后' 7月'          当8然后' 8月'          当9然后' 9月'          当10然后' 10月'          什么时候11然后' 11月'          什么时候12,然后'12月'        以月结束,        INVOICE_AMOUNT 从发票 其中convert(nvarchar(50),date_invoice,100)> DATEADD(月,-12,getdate()) )作为dt 按年,月分组 按年,月排序