我正在尝试编写查询以从表数据中获得以下结果:
由于我是初学者,因此我可以编写查询以获取该数据的数据,但不能将销售分成每小时数据。另外,我想比较同一时间范围内的销售额与YOY和WOW数据。
这里是查询以提取当天的数据。
select COUNT(*) Order_Count_For_The_Day
FROM
purchase_Mobile
WHERE
CREATE_DATE between to_date('01/02/2018 00:00:00','mm/dd/yyyy hh24:mi:ss') and to_date('01/02/2018 23:59:59','mm/dd/yyyy hh24:mi:ss')
AND status_code >= 99;
有人可以更改查询内容,并帮助我获得上述要点中提到的所需输出吗,我将一生为之感激?
我已经尝试从给定表中提取数据,并且可以在特定时间段内提取数据。
select COUNT(*) Order_Count_For_The_Day
FROM
purchase_Mobile
WHERE
CREATE_DATE between to_date('01/02/2018 00:00:00','mm/dd/yyyy hh24:mi:ss') and to_date('01/02/2018 23:59:59','mm/dd/yyyy hh24:mi:ss')
AND status_code >= 99;
答案 0 :(得分:0)
为了获取每小时的信息,可以使用datepart(HH,Create_Date)。然后使用该小时信息的GROUP BY。
SELECT COUNT(CREATE_DATE) Order_Count_For_The_Day
,datepart(HH,CREATE_DATE) Order_Hour
,MAX(CREATE_DATE) CurrentDate
FROM purchase_Mobile
CREATE_DATE between to_date('01/02/2018 00:00:00','mm/dd/yyyy hh24:mi:ss') and to_date('01/02/2018 23:59:59','mm/dd/yyyy hh24:mi:ss')
AND status_code >= 99;
GROUP BY datepart(HH,CREATE_DATE)
为了执行“今年,去年”和其他时间范围-我将它们分为自己的临时表,并在最后加入它们。您需要为此调整一些问题(没有销售的小时数,关闭的小时数等,我做不到的方式),但想为您提供一些开始的选择:
DECLARE @StartDate DateTime = '2018-1-2 00:00:00'
DECLARE @EndDate DateTime = '2018-1-2 23:59:00'
DECLARE @CurrentResults Table
(OrderCount INT
,OrderHour INT
,CurrentDate DATETIME)
INSERT INTO @CurrentResults
SELECT COUNT(CREATE_DATE) Order_Count_For_The_Day
,datepart(HH,CREATE_DATE) Order_Hour
,MAX(CREATE_DATE) CurrentDate
FROM purchase_Mobile
WHERE CREATE_DATE BETWEEN @StartDate AND @EndDate
AND status_code >= 99
GROUP BY datepart(HH,CREATE_DATE)
OPTION (RECOMPILE);
DECLARE @LastYearResults Table
(LastYearOrderCount INT
,LastYearOrderHour INT
,LastYearDate DATETIME)
INSERT INTO @LYResults
SELECT COUNT(CREATE_DATE) LastYearOrderCount
,datepart(HH,CREATE_DATE) LastYearOrderHour
,MAX(CREATE_DATE) LastYearDate
FROM purchase_Mobile
WHERE CREATE_DATE BETWEEN @StartDate-365 AND @EndDate-365
AND status_code >= 99
GROUP BY datepart(HH,CREATE_DATE)
OPTION (RECOMPILE);
DECLARE @LastWeekResults Table
(LastWeekOrderCount INT
,LastWeekOrderHour INT
,LastWeekDate DATETIME)
INSERT INTO @LastWeekResults
SELECT COUNT(CREATE_DATE) Order_Count
,datepart(HH,CREATE_DATE) Order_Hour
,MAX(CREATE_DATE) LastWeekDate
FROM purchase_Mobile
WHERE CREATE_DATE BETWEEN @StartDate-7 AND @EndDate-7
AND status_code >= 99
GROUP BY datepart(HH,CREATE_DATE)
OPTION (RECOMPILE);
Select CR.OrderHour
,CR.OrderCount
,LY.LastYearOrderCount
,LW.LastWeekOrderCount
,CR.CurrentDate
,LY.LastYearDate
,LW.LastWeekDate
FROM @CurrentResults As CR
LEFT JOIN @LastYearResults AS LY ON LY.LastYearOrderHour = CR.OrderHour
LEFT JOIN @LastWeekResults AS LW ON LW.LastWeekOrderHour = CR.OrderHour
ORDER BY CR.OrderHour