Query for summation up to certain rows in a table

天涯浪子 提交于 2019-12-12 05:43:30

问题


I have a query like:

select
    CONVERT(VARCHAR(7),[startdatetime],111) AS [year-month],
    nm.nameLine1,
    sum(datediff(hour, startdatetime, enddatetime)) as total 
from  srl
inner join  sr on srl= sr.ServiceRequestId
inner join Name nm on(sr.clientCustomerId = nm.customerId and nm.nameTypeId = 'OFICE')
where (startdatetime >= '08-01-2011 00:00:00.000' and enddatetime <= '10-31-2011 00:00:00.000')
group by nm.nameLine1, [year-month]
order by nm.nameLine1, [year-month]

output of the above query is::

year-month nameLine1       total       
---------- ---------       ----------- 
2011/08    B               4 
2011/09    B               7 
2011/10    B               0 
2011/08    E               167 
2011/09    E               212 
2011/10    E               131 
2011/08    L               14 
2011/09    L               23 
2011/10    L               3 
2011/08    O               18 
2011/09    O               8 
2011/10    O               7 
2011/08    S               43 
2011/09    S               60 
2011/10    S               60 

Now my question is, what should I do in the query to get the summation in a different column called nameLine1total for a single nameLine1. The output should be like this:

year-month nameLine1       total            nameLine1total
---------- ---------       -----------      ---------------
2011/08    B               4         
2011/09    B               7                 
2011/10    B               0                    11 
2011/08    E               167 
2011/09    E               212 
2011/10    E               131                  510 
2011/08    L               14 
2011/09    L               23 
2011/10    L               3                    40 
2011/08    O               18 
2011/09    O               8 
2011/10    O               7                    33
2011/08    S               43 
2011/09    S               60 
2011/10    S               60                   163

回答1:


what variety of sql are you using - sql server, mysql, etc. Also, it doesn't matter for your answer but do you really want <= 10-31-2011 00:00:00, so that data from october 31 isn't included in your query? Also, how are you going to use that query - if it's for a reporting tool, quite likely the reporting tool can calculate it more easily than sql can. If it would be acceptable to have the nameline total included on every row, that would be easier - e.g. if all three rows for "S" said "163" in the last column, is that okay?




回答2:


Because of convert(.., .., 111) I assume that this is a SQL Server, if it's 2005+ I would use CTE:

with cte (ym, n, total, row) as (
    select *, row_number() over(partition by nameLine1 order by nameLine1, [year-month])  from (
        select
            convert(varchar(7),[startdatetime],111) as [year-month],
            nm.nameLine1,
            sum(datediff(hour, startdatetime, enddatetime)) as total 
        from  srl
        inner join sr on srl= sr.ServiceRequestId
        inner join Name nm on 
            sr.clientCustomerId = nm.customerId and 
            nm.nameTypeId = 'OFICE'
        where startdatetime >= '08-01-2011 00:00:00.000' and enddatetime <= '10-31-2011 00:00:00.000'
        group by nm.nameLine1, [year-month]
        order by nm.nameLine1, [year-month]
    ) t
) 
select c.ym as [year-month], c.n as [nameLine1], c.total as [total], g.total as [nameLine1total]
from cte c
left join (
    select max(row) as row, sum(total) as total, n 
    from cte group by n
) g on c.n = g.n and c.row = g.row


来源:https://stackoverflow.com/questions/8469917/query-for-summation-up-to-certain-rows-in-a-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!