简体   繁体   English

提取每月时段数据

[英]Pulling out monthly daypart data

I'm dealing with sales data that looks like this我正在处理看起来像这样的销售数据

Date日期 Time时间 Net Amount净额 Store No店铺编号 Item Category Code项目类别代码
2021-03-21 00:00:00.000 2021-03-21 00:00:00.000 1754-01-01 08:01:14.627 1754-01-01 08:01:14.627 100 100 001 001 FOOD食物
2021-01-31 00:00:00.000 2021-01-31 00:00:00.000 1754-01-01 15:42:21.670 1754-01-01 15:42:21.670 120 120 002 002 FOOD食物
2021-04-24 00:00:00.000 2021-04-24 00:00:00.000 1754-01-01 17:35:30.070 1754-01-01 17:35:30.070 160 160 002 002 FOOD食物
2021-03-14 00:00:00.000 2021-03-14 00:00:00.000 1754-01-01 13:08:02.073 1754-01-01 13:08:02.073 150 150 003 003 FOOD食物
2021-02-27 00:00:00.000 2021-02-27 00:00:00.000 1754-01-01 18:33:53.847 1754-01-01 18:33:53.847 95 95 001 001 FOOD食物
2021-02-20 00:00:00.000 2021-02-20 00:00:00.000 1754-01-01 21:30:24.007 1754-01-01 21:30:24.007 180 180 002 002 DRINK
2021-04-09 00:00:00.000 2021-04-09 00:00:00.000 1754-01-01 07:36:33.930 1754-01-01 07:36:33.930 110 110 003 003 FOOD食物
2021-01-29 00:00:00.000 2021-01-29 00:00:00.000 1754-01-01 08:02:49.703 1754-01-01 08:02:49.703 180 180 002 002 FOOD食物
2021-01-24 00:00:00.000 2021-01-24 00:00:00.000 1754-01-01 11:01:00.953 1754-01-01 11:01:00.953 110 110 003 003 FOOD食物

What I need is to pull out monthly data by daypart that would result to this我需要的是按时段提取月度数据,这将导致此

Jan 2021 2021 年 1 月 Feb 2021 2021 年 2 月 Mar 2021 2021 年 3 月 Apr 2021 2021 年 4 月
4am to 10:59am凌晨 4 点至 10 点 59 分 180 180 0 0 100 100 110 110
11am to 1:59pm上午 11 点至下午 1:59 110 110 0 0 150 150 0 0
2pm to 3:59am下午 2 点至凌晨 3:59 120 120 95 95 0 0 160 160

Here's the query I'm currently using:这是我目前使用的查询:

with FoodSales as
(
    select distinct
           concat(year(s.[Date]),'',datename(month,s.[Date])) as TransMonth
           ,sum( s.[Net Amount]*-1) as Sales

    from [dbo].[vw_Sales Entry] as s

    left join [dbo].[vw_Discount Entry] as d
          on s.[Receipt No_]=d.[Receipt No_] 


    where s.[Date] >= '2021-01-01 04:00:00' and s.[Date] <= '2021-04-30 10:59:59'
          and s.[Item Category Code]='FOOD'
          and (d.[Discount Name] is null or d.[Discount Name]='')
         

    group by 
           concat(year(s.[Date]),'',datename(month,s.[Date]))

)

    select * 
    from FoodSales
    pivot( sum(Sales)
    for TransMonth in ([2021January]
                       ,[2021February]
                       ,[2021March]
                       ,[2021April]

      )
      ) as p


;

I've also tried using我也试过使用

[Date] between '2021-01-01 04:00:00' and '2021-04-30 10:59:59'

but when both are tried with another interval, the sales amount just comes out the same.但是当两者都以另一个间隔进行尝试时,销售额就会相同。

I would suggest to you to store time as part of datetime column itself.我建议您将时间存储为日期时间列本身的一部分。 If you want to keep time separately, store them in the time datatype.如果要单独保留时间,请将它们存储在时间数据类型中。 I have put sample below considering both date and time being stored in the date column.考虑到日期和时间存储在日期列中,我在下面放置了示例。

You can use DATEPART to demarcate the time ranges and then use pivot to generate result set as given below:您可以使用 DATEPART 来划分时间范围,然后使用 pivot 生成如下所示的结果集:

declare @TABLE TABLE(dateval datetime, Netamount int, storeno char(3), itemcategorycode char(4))

insert into @table
values
('2021-03-21 04:00:00.000', 100,'001','FOOD'), 
('2021-04-24 10:00:00.000', 160,'002','FOOD')   


select * from
(
SELECT case when datepart(hh,dateval) >= 4 and datepart(hh,dateval) < 11 then '4am to 10:59am' 
            when datepart(hh,dateval) >= 11 and datepart(hh,dateval) < 14 then '11am to 1:59pm' 
            when datepart(hh,dateval) >= 14 and datepart(hh,dateval) < 16 then '2pm to 3:59am' end as timerange,
            
            format(dateval,'MMM yy') AS Month_Year,netamount FROM @table 
            ) as t
            pivot (sum(NetAmount) for month_year in ([Mar 21],[Apr 21])) as pvt
timerange时间范围 Mar 21 3月21日 Apr 21 4月21日
4am to 10:59am凌晨 4 点至 10 点 59 分 100 100 160 160

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

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