【问题标题】:Pulling out monthly daypart data提取每月时段数据
【发布时间】:2021-10-02 21:31:08
【问题描述】:

我正在处理看起来像这样的销售数据

Date Time Net Amount Store No Item Category Code
2021-03-21 00:00:00.000 1754-01-01 08:01:14.627 100 001 FOOD
2021-01-31 00:00:00.000 1754-01-01 15:42:21.670 120 002 FOOD
2021-04-24 00:00:00.000 1754-01-01 17:35:30.070 160 002 FOOD
2021-03-14 00:00:00.000 1754-01-01 13:08:02.073 150 003 FOOD
2021-02-27 00:00:00.000 1754-01-01 18:33:53.847 95 001 FOOD
2021-02-20 00:00:00.000 1754-01-01 21:30:24.007 180 002 DRINK
2021-04-09 00:00:00.000 1754-01-01 07:36:33.930 110 003 FOOD
2021-01-29 00:00:00.000 1754-01-01 08:02:49.703 180 002 FOOD
2021-01-24 00:00:00.000 1754-01-01 11:01:00.953 110 003 FOOD

我需要的是按时段提取每月数据,这会导致这种情况

Jan 2021 Feb 2021 Mar 2021 Apr 2021
4am to 10:59am 180 0 100 110
11am to 1:59pm 110 0 150 0
2pm to 3:59am 120 95 0 160

这是我当前使用的查询:

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


;

我也试过

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

但是当两者都以另一个间隔尝试时,销售额只是一样。

【问题讨论】:

  • 为什么一月,你把时间部分?为什么不是销售额?
  • @VenkataramanR 抱歉,忘记添加空白列标题。谢谢你帮我解决这个问题!
  • 不客气。我已经添加了答案。请检查。
  • 当您的数据跳到 2022 年及以后会发生什么。您可能需要动态支点。这些数据是如何消耗的?您可以使用报告工具或将其输入 Excel 吗?
  • @Harry 我只需要 2021 年 1 月到 2021 年 6 月的数据

标签: sql sql-server tsql


【解决方案1】:

我建议您将时间存储为 datetime 列本身的一部分。如果您想单独记录时间,请将它们存储在时间数据类型中。考虑到日期和时间都存储在日期列中,我将示例放在下面。

您可以使用 DATEPART 划分时间范围,然后使用数据透视 生成结果集如下:

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 Apr 21
4am to 10:59am 100 160

【讨论】:

    猜你喜欢
    • 2021-07-15
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    • 2014-08-17
    • 2021-01-17
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    相关资源
    最近更新 更多