简体   繁体   English

SQL查询按月求和

[英]SQL Query to sum by month

i have the two tables, Payment Table and Person Table, a person por month can have more than one payment, so i want so sum all "amount" fields from a parson per month and per year, if there is no payment the result should be 0 and ID of the person should appear in the month. 我有两个表,“付款表”和“人表”,一个人一个月可以有多个付款,所以我想将一个牧师每月和每年的所有“金额”字段加起来,如果没有付款,结果应该为0,并且该人的ID应该出现在月份中。

i am almost there, but in my curreny query the data displayed is all payments per person and not the sum. 我快到了,但是在我的货币查询中,显示的数据是每人所有付款,而不是总和。 how can o het this? 怎么可能呢?

current results are like this (see october) i need to sum below 3 payments and olny show one line of october 2013: 当前结果是这样的(请参见10月),我需要对3次付款进行总计,而olny则显示2013年10月的一行:

My Table 我的桌子

MonthNr---MonthAbr---Amount---PersonID---YearAmount MonthNr --- MonthAbr --- ---量---是PersonID YearAmount

1---JAN---0---2---2013 1 --- --- JAN 0 --- 2 --- 2013

2---FEB---0---2---2013 2 --- --- FEB 0 --- 2 --- 2013

3---MAR---0---2---2013 3 --- --- MAR 0 --- 2 --- 2013

4---APR---0---2---2013 4 --- --- APR 0 --- 2 --- 2013

5---MAY---0---2---2013 5 --- --- MAY 0 --- 2 --- 2013

6---JUN---0---2---2013 6 --- --- JUN 0 --- 2 --- 2013

7---JUL---0---2---2013 7 --- --- JUL 0 --- 2 --- 2013

8---AUG---0---2---2013 8 --- --- AUG 0 --- 2 --- 2013

9---SEP---0---2---2013 9 --- --- SEP 0 --- 2 --- 2013

10---OCT---64,74---2---2013 10 --- --- OCT --- 64,74 2 --- 2013

10---OCT---73,66---2---2013 10 --- --- OCT --- 73,66 2 --- 2013

10---OCT---24,3---2---2013 10 --- --- OCT --- 24.3 --- 2 2013

11---NOV---24,3---2---2013 11 --- --- NOV --- 24.3 --- 2 2013

12----DEC----0---2----2013 12 ---- ---- DEC 0 --- 2 ---- 2013

My query: 我的查询:

SELECT 
months.monthno as MonthNr,
CAST(CASE   WHEN CAST(months.monthno AS int) =1  THEN 'JAN'
            WHEN CAST(months.monthno AS int) =2  THEN 'FEB'
            WHEN CAST(months.monthno AS int) =3  THEN 'MAR'
            WHEN CAST(months.monthno AS int) =4  THEN 'APR'
            WHEN CAST(months.monthno AS int) =5  THEN 'MAY'
            WHEN CAST(months.monthno AS int) =6  THEN 'JUN'
            WHEN CAST(months.monthno AS int) =7  THEN 'JUL'
            WHEN CAST(months.monthno AS int) =8  THEN 'AUG'
            WHEN CAST(months.monthno AS int) =9  THEN 'SEP'
            WHEN CAST(months.monthno AS int) =10  THEN 'OCT'
            WHEN CAST(months.monthno AS int) =11  THEN 'NOV'
            WHEN CAST(months.monthno AS int) =12  THEN 'DEC'
     ELSE 
     '' 
     END AS nvarchar) as MonthAbr,

    Amount = isnull(sum(o.Amount),0),
    c.IDPerson  as PersonID,
    isnull(year(o.Date ),2013) as YearAmount
    FROM
    Person c 
     cross join     
    (select number monthNo from master..spt_values where type='p' and number between 1 and 12) months
     full join Payments o
    ON o.IDPerson   = c.IDPerson
    AND month(o.Date ) = months.monthNo
    where c.IDPerson = 2
    GROUP BY
    months.monthno, c.IDPerson ,o.Date
    ORDER BY
    months.monthno, c.IDPerson

can anyone help me? 谁能帮我? thanks in advance. 提前致谢。

Since you are using the isnull function on o.date I assume this means there are nulls in this column. 由于您在o.date上使用了isull函数,因此我认为这意味着此列中为null。 If so, you need to account for this within your group by clause, eg "group by months.monthno, c.idperson, isnull(year(o.date),2013)". 如果是这样,则需要在group by子句中对此进行说明,例如“ group by months.monthno,c.idperson,isull(year(o.date),2013)”。

您不应该按o.Date ,而只能按日期中的月份o.Date ,该日期已包含为months.monthno

Why dont you simplify it: 您为什么不简化它:

 select
 months.monthno as MonthNr
 ,case when monthno='1' then 'JAN'
       when monthno='2' then 'FEB'
       end as MonthAbr
 isnull(year(o.Date ),2013) as YearAmount
,sum(Amount)
from PERSONS c
left join Payments o ON o.IDPerson = c.IDPerson
left join    
(select number monthNo from master..spt_values where type='p' and number between 1 and    12) months on months.number= select month(o.Date)
group by months.monthno, o.date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM