基于日期的总和记录落在查找表的范围内-SQL Server

时间:2019-03-13 23:19:31

标签: sql-server sum date-range

我的意图是根据其发生日期所在的日期范围来查询总计记录。通过下表创建日期范围

IF OBJECT_ID('tempdb..#DateRanges') IS NOT NULL DROP TABLE #DateRanges
Create Table #DateRanges
 (Start_num int, End_Num int, Day_Period varchar(10), Day_Bin varchar(8))

 Insert Into #DateRanges
 Values (0,-6,'7Day', 'Current'),
        (-7,-13,'7Day','1'),
        (-14,-20,'7Day','2'),
        (-21,-27,'7Day','3'),
        (-28,-34,'7Day','4'),
        (0,-27,'28Day','Current'),
        (-28,-55,'28Day','1'),
        (-56,-83,'28Day','2'),
        (-84,-111,'28Day','3'),
        (-112,-139,'28Day','4'),
        (0,-83,'84Day','Current'),
        (-84,-167,'84Day','1'),
        (-168,-251,'84Day','2'),
        (-252,-335,'84Day','3'),
        (-336,-419,'84Day','4'),
        (0,-363,'364Day','Current'),
        (-364,-727,'364Day','1'),
        (-728,-1091,'364Day','2'),
        (-1092,-1455,'364Day','3'),
        (-1456,-1819,'364Day','4')

以下查询正在尝试查询我的RMS并使用上表对记录求和

select 
  'category' as n_Group, 
   ab2.ItemCategory as Item,
   DLU.Day_Period, 
   DLU.Day_Bin, 
   sum(g.occ_date Between DateAdd("d",DLU.end_num,@Date1) AND DateAdd   ("d",DLU.start_Num,@Date1)) as n_Count

FROM g_data g
     left join LUab ab2
        on g.ext= ab2.ext,
     #DateRanges DLU

Where g.occ_date Between DateAdd("d", -1819, @Date1) and  @Date1

Group By
    ab2.ItemCategory,
    DLU.Day_Period,
    DLU.Day_Bin

另一个用户协助上述查询的创建,但这是针对MS Access的。我已经换了档,现在正在尝试在SQL Server中完成项目的这一部分,并且以下行与SQL Server不兼容  sum(g.occ_date Between DateAdd("d",DLU.end_num,@Date1) AND DateAdd("d",DLU.start_Num,@Date1)) as n_Count

不正确的语法错误显示了求和行中的最后一个括号。在修补过程中,我尝试了Count(iif(g.occ_date Between DateAdd("d",DLU.end_num,@Date1) AND DateAdd("d",DLU.start_Num,@Date1)), g.rin) as n_Count并且查询运行了,但返回的n_count总数似乎可能是在整个有效值中对类别进行计数。 Case When g.occ_date Between DateAdd("d",DLU.end_num,@Date1) AND DateAdd("d",DLU.start_Num,@Date1) then 1 else 0 End as n_Count在Count iif返回了相同的结果

下面是示例查询产生的示例数据(经过消毒)...我为格式表示歉意,搜索了帮助部分,但找不到更好的方法。 ItemType和ItemDesc是子类别,并且将以与带有联合查询的ItemCategory相同的方式求和

occ_date ItemCategory Itemetype ItemDesc randnum 11/10/2011 A AS AAS 1.01E-07 1/21/2015 V VV VVA 1.02E-07 3/6/2001 L LP LLP 3.85E-07 8/25/2017 L LS LLS 4.41E-07 12/7/2008 C CC CCC 5.62E-07 2/28/2009 V VV VVA 6.54E-07 6/24/2012 M MM MMM 6.76E-07 6/26/2006 A AA AAA 9.86E-07 3/1/1994 Q QQ QQP 1.11E-06 1/9/2018 M MM MMM 1.55E-06 11/6/1992 T TA TTA 2.00E-06 6/3/2009 V VV VVV 2.18E-06 10/26/1996 M MM MMM 2.28E-06 1/21/2017 M MM MMM 2.34E-06 3/28/2012 N NN NN 2.35E-06 5/1/1999 M MM MMM 2.52E-06 6/1/2014 S SR SSR 2.87E-06 10/2/2016 V VV VVV 2.91E-06 8/5/1988 C CC CCC 3.33E-06 4/6/2018 N NN NNN 3.34E-06

1 个答案:

答案 0 :(得分:0)

感谢#DateRanges。请运行以下命令并将结果数据文件粘贴到上面。这将为我们提供一些可用于您的解决方案的测试数据。

Select TOP 20 *  From (
    Select Top 5000
       g.occ_date
      ,g.ext
      ,ab2.ItemCategory
      , RAND(convert(varbinary, newid())) as randnum
    FROM      g_data g
    left join LUab ab2
            on g.ext= ab2.ext
    order by randnum
    ) zz
  
    

当天晚些时候...尝试一下。我只是将SUM更改为COUNT(*),然后将“在DLU之间...”移动到Where..AND ...

  
select 
  'category' as n_Group, 
   ab2.ItemCategory as Item,
   DLU.Day_Period, 
   DLU.Day_Bin, 
   count(*)  as n_Count

FROM g_data g
     left join LUab ab2
        on g.ext= ab2.ext,
     #DateRanges DLU

Where g.occ_date Between DateAdd("d", -1819, @Date1) and  @Date1
and (g.occ_date Between DateAdd("d",DLU.end_num,@Date1) AND DateAdd   ("d",DLU.start_Num,@Date1))

Group By
    ab2.ItemCategory,
    DLU.Day_Period,
        DLU.Day_Bin
  
    

3/14 13:45这是我使用'3/1/2019'的@ Date1的结果。     所有其他日期都太古老了。你得到了什么?结果应该是什么样?

  
n_Group Item    Day_Period  Day_Bin n_Count
category    L   364Day         1    1
category    M   364Day         1    1
category    M   364Day         2    1
category    M   84Day          4    1
category    N   364Day     Current  1
category    N   84Day          3    1
category    S   364Day         4    1
category    V   364Day         2    1
category    V   364Day         4    1