我想为每个WHERE语句获取一列,现在我必须为每个语句手动进行操作以获取结果,但是我如何立即执行呢?
SELECT
COUNT([ParcelEventID]) as WEEK
FROM
[Data].[CDB].[ParcelEvent]
WHERE
[EventDate] BETWEEN 20180820 AND 20180826
--[EventDate] BETWEEN 20180827 AND 20180902
--[EventDate] BETWEEN 20180902 AND 20180909
--[EventDate] BETWEEN 20180909 AND 20180916
--[EventDate] BETWEEN 20180916 AND 20180923
--[EventDate] BETWEEN 20180923 AND 20180930
--[EventDate] BETWEEN 20181001 AND 20181007
--[EventDate] BETWEEN 20181008 AND 20181014
--[EventDate] BETWEEN 20181014 AND 20181021
--[EventDate] BETWEEN 20181022 AND 20181028 )
AND [Depot] LIKE '05[12]%'
AND [ParcelEventTypeID] = 09
编辑: 感谢您的回答,他们的工作。但是,查询运行非常慢。是否可以以某种方式查询以下内容作为结果?
Eventdate ParcelEventTypeID Count Week
01/12 6 10 50
01/12 8 100 50
02/12 6 25 50
02/12 8 15 50
类似这样:
SELECT week
SUM(Count)
[ParcelEventTypeID]
,datepart(weekday, [EventDateTime]) As [WeekDay]
FROM [Data].[CDB].[ParcelEvent]
GROUP BY [week], [ParcelEventTypeID]
然后得到如下结果:
Week ParcelEventTypeID Count
1 6 35
1 8 115
答案 0 :(得分:0)
我想为每个WHERE语句获取一列
您似乎正在寻找条件聚合:
SELECT
SUM(CASE WHEN [EventDate] BETWEEN 20180820 AND 20180826 THEN 1 ELSE O END) AS 20180820_TO_20180826
SUM(CASE WHEN [EventDate] BETWEEN 20180827 AND 20180902 THEN 1 ELSE 0 END) AS 20180827_TO_20180902
...
FROM
[Data].[CDB].[ParcelEvent]
WHERE
[Depot] LIKE '05[12]%'
AND [ParcelEventTypeID] = 09
答案 1 :(得分:0)
您可能想要条件聚合
SELECT
Count(CASE WHEN [EventDate] BETWEEN 20180820 AND 20180826 THEN [ParcelEventID] end) AS WEEK1
,Count(CASE WHEN [EventDate] BETWEEN 20180827 AND 20180902 THEN [ParcelEventID] end) AS WEEK2
,Count(CASE WHEN [EventDate] BETWEEN 20180902 AND 20180909 THEN [ParcelEventID] end) AS WEEK3
,...
FROM
[DATA].[CDB].[ParcelEvent]
WHERE [EventDate] BETWEEN 20180820 AND 20181028
AND [Depot] LIKE '05[12]%'
AND [ParcelEventTypeID] = 09
答案 2 :(得分:0)
SELECT
case
when [EventDate] BETWEEN 20180820 AND 20180826 then count(ParcelEventID)
else 0
end as 20180820_20180826,
case
when [EventDate] BETWEEN 20180827 AND 20180902 then count(ParcelEventID)
else 0
end as 20180827_20180902,
case
when [EventDate] BETWEEN 20180902 AND 20180909 then count(ParcelEventID)
else 0
end as 20180827_20180902
FROM [Data].[CDB].[ParcelEvent]
WHERE [Depot] LIKE '05[12]%' AND [ParcelEventTypeID] = 09
答案 3 :(得分:0)
感谢您的回答,他们的工作。但是,查询运行非常慢。是否可以以某种方式查询以下内容作为结果?
Eventdate ParcelEventTypeID Count Week
01/12 6 10 50
01/12 8 100 50
02/12 6 25 50
02/12 8 15 50
类似这样:
SELECT week
SUM(Count)
[ParcelEventTypeID]
,datepart(weekday, [EventDateTime]) As [WeekDay]
FROM [Data].[CDB].[ParcelEvent]
GROUP BY [week], [ParcelEventTypeID]
然后得到如下结果:
Week ParcelEventTypeID Count
1 6 35
1 8 115