我的意图是根据发生日期所在的日期范围得到查询和记录。日期范围通过下表创建
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
总计似乎可能计算整个rms中的类别。 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
感谢#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以下是使用@ Date1 of'3/1/2019'的结果。所有其他日期都太古老了。你得到了什么?结果应该是什么样的?
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