简体   繁体   English

SQL添加每周和每月总计

[英]SQL Add for Weekly & Monthly Totals

I am trying to follow a similar example of getting weekly and monthly totals from: StackOverFlow: SQL Add Sum Row for Week and At the End Add the Grand Total . 我正在尝试遵循一个类似的示例,该示例从以下位置获取每周和每月总计: StackOverFlow:SQL为周添加总计行,然后在末尾添加总计 This level of SQL is a stretch for me, so please be a clear as possible. 对我来说,SQL的这种水平是很困难的,因此请尽可能清楚。

I have a table with over 200K records of ID, Store_ID, Sales_Date, Amount. 我有一张桌子,上面有超过20万条ID,Store_ID,Sales_Date,Amount的记录。

ID  Store_ID    Amount  Sales_Date
1   215            7    1/29/2012
2   215            7    1/30/2012
3   215            7    1/31/2012
4   215            7    2/1/2012
5   215            7    2/2/2012
6   215            7    2/3/2012
7   215            7    2/4/2012
8   215            8    2/5/2012
9   215            8    2/6/2012
10  215            8    2/7/2012
    ***More and More Data***        
162 218            4    10/30/2011
163 218            4    10/31/2011
164 218            4    11/1/2011
165 218            4    11/2/2011
166 218            4    11/3/2011
167 218            4    11/4/2011
168 218            4    11/5/2011
169 218            8    11/6/2011
170 218            8    11/7/2011
171 218            8    11/8/2011
           ******LOTS MORE DATA*****

I need to generate a view that will show the Weekly and Monthly Totals for each Store_ID and the associated date of the total. 我需要生成一个视图,该视图将显示每个Store_ID的每周和每月总计以及总计的关联日期。 The issue that I am having is that the example is providing me with Week totals(with no dates associated), Month totals (with no dates associated) and the daily amounts( which is a side benefit). 我遇到的问题是,该示例向我提供了周总计(没有日期关联),月份总计(没有日期关联)和每日金额(这是附带的好处)。

I need to know how I can add in an additional column to the results that will show me the week ending date and the month ending date. 我需要知道如何在结果中添加其他列,以向我显示周结束日期和月结束日期。

This is what I have so far (it is almost exactly like the example): 这是我到目前为止所拥有的(几乎与示例完全一样):

set datefirst 7

select top 100
    case
        when grouping(cast(datepart(week, [Sales_Date]) as varchar(255)))=1 then '<MonthEnd>'
        when grouping(cast([Sales_Date] as date))=1 then '<weektotal>'
        else cast(cast([Sales_Date] as date) as varchar(255))
    end as Period
    , WkSales = sum(Amount)
    , Store = Store_ID
From KF_Store_Sales_Daily

group by 
    grouping sets(  
    (cast(datepart(month, [Sales_Date]) as varchar(255)), cast(datepart(week, [Sales_Date]) as varchar(255)),cast([Sales_Date] as date)),
    (cast(datepart(month, [Sales_Date]) as varchar(255)), cast(datepart(week, [Sales_Date]) as varchar(255))),
    (cast(datepart(month, [Sales_Date]) as varchar(255)))
    )
    , Store_ID
ORDER BY Store_ID, Sales_Date    

The following query can be used to show daily, weekly, monthly and yearly totals: 以下查询可用于显示每日,每周,每月和每年的总计:

select
    case
        when grouping(d.m)=1 then 'Year ' + cast(max(d.y) as varchar(10))
        when grouping(d.w)=1 then datename(m, max(Sales_Date)) + ' ' + cast(max(d.y) as varchar(10))
        when grouping(Sales_Date)=1 then 'Week ' + datename(m, max(ws)) + ' ' + cast(datepart(d, max(ws)) as varchar(20)) + ' - '
            + datename(m, max(we)) + ' ' + cast(datepart(d, max(we)) as varchar(20))
        else cast(cast([Sales_Date] as date) as varchar(255))
    end as Period
    , Sales = sum(Amount)
    , Store = Store_ID
from KF_Store_Sales_Daily
    cross apply (
        select -- aux. expressions for dates
            datepart(yy, [Sales_Date]), -- year
            datepart(m, [Sales_Date]), -- month
            datepart(wk, [Sales_Date]), -- week
            dateadd(d, 1-datepart(w, Sales_date), Sales_date), -- week start
            dateadd(d, 7-datepart(w, Sales_date), Sales_date) -- week end
    ) d(y, m, w, ws, we)
group by Store_ID, d.y, rollup (d.m, d.w, Sales_Date)
order by d.y desc,
    grouping(d.m), d.m,
    grouping(d.w), d.w,
    grouping(Sales_Date), Sales_Date

I'm not sure how convenient it is to have montly and weekly totals together (since week may belong to two months). 我不确定将每月和每周的总数汇总在一起有多方便(因为一周可能属于两个月)。 Following two queries for the case if you will need them separartely. 如果您需要分开使用,请在以下两个查询情况下进行。

For daily, monthly and yearly totals: 每日,每月和每年总计:

select
    case
        when grouping(d.m)=1 then 'Year ' + cast(max(d.y) as varchar(10))
        when grouping(Sales_Date)=1 then datename(m, max(Sales_Date)) + ' ' + cast(max(d.y) as varchar(10))
        else cast(cast([Sales_Date] as date) as varchar(255))
    end as Period
    , Sales = sum(Amount)
    , Store = Store_ID
from KF_Store_Sales_Daily
    cross apply (
        select
            datepart(yy, [Sales_Date]),
            datepart(m, [Sales_Date])
    ) d(y, m)
group by Store_ID, d.y, rollup (d.m, Sales_Date)
order by d.y desc,
    grouping(d.m), d.m,
    grouping(Sales_Date), Sales_Date

For daily, weekly and yearly totals (in this case a week may belong to two years): 对于每日,每周和每年的总计(在这种情况下,一周可能属于两年):

select
    case
        when grouping(d.w)=1 then 'Year ' + cast(max(d.y) as varchar(10))
        when grouping(Sales_Date)=1 then 'Week ' + datename(m, max(ws)) + ' ' + cast(datepart(d, max(ws)) as varchar(20)) + ' - '
            + datename(m, max(we)) + ' ' + cast(datepart(d, max(we)) as varchar(20))
        else cast(cast([Sales_Date] as date) as varchar(255))
    end as Period
    , Sales = sum(Amount)
    , Store = Store_ID
from KF_Store_Sales_Daily
    cross apply (
        select
            datepart(yy, [Sales_Date]),
            datepart(wk, [Sales_Date]),
            dateadd(d, 1-datepart(w, Sales_date), Sales_date),
            dateadd(d, 7-datepart(w, Sales_date), Sales_date)
    ) d(y, w, ws, we)
group by Store_ID, d.y, rollup (d.w, Sales_Date)
order by d.y desc,
    grouping(d.w), d.w,
    grouping(Sales_Date), Sales_Date

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

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