我尝试制作一个查询,显示一段时间内每小时的平均总订单数。
我这样试试:
SELECT [day],
[hour],
[dayn],
Avg(totals) AS [Avg]
FROM (SELECT [document no_] AS hallo,
Cast(CONVERT(VARCHAR, [shipment date], 101) AS DATE),
w = Datediff(week, 0, [timestamp]),
[Day] = Datename(weekday, [timestamp]),
[DayN] = Datepart(weekday, [timestamp]),
[Hour] = Datepart(hour, [timestamp]),
Totals = Count(*)
FROM [verploegen poc$sales line]
WHERE [shipment date] BETWEEN '2016-01-01' AND '2016-12-31'
AND ( [document no_] NOT LIKE '%RET%'
AND [document no_] NOT LIKE '%OFF%'
AND [document no_] NOT LIKE '%CV%'
AND [document no_] NOT LIKE '%RC%' )
GROUP BY [document no_],
[shipment date],
Datediff(week, 0, [timestamp]),
Datename(weekday, [timestamp]),
Datepart(weekday, [timestamp]),
Datepart(hour, [timestamp])) AS hallo
GROUP BY [day],
[hour],
[dayn]
ORDER BY dayn;
但我收到错误:
没有为' hallo'。
的第2列指定列名
但如何纠正这个?
谢谢
我现在就这样:
SELECT [Day], [Hour], [DayN], AVG(Totals) AS [Avg]
FROM
(
SELECT [Document No_] as hallo, cast(CONVERT(varchar, [Shipment Date], 101) as date) as number,
w = DATEDIFF(WEEK, 0, [timestamp]),
[Day] = DATENAME(WEEKDAY, [timestamp]),
[DayN] = DATEPART(WEEKDAY, [timestamp]),
[Hour] = DATEPART(HOUR, [timestamp]),
Totals = COUNT(*)
FROM [Verploegen POC$Sales Line]
WHERE [Shipment Date] BETWEEN '2016-01-01' and '2016-12-31'
AND ([Document No_] NOT LIKE '%RET%' AND [Document No_] NOT LIKE '%OFF%'
AND [Document No_] NOT LIKE '%CV%' AND [Document No_] NOT LIKE '%RC%')
GROUP BY
[Document No_], [Shipment Date],
DATEDIFF(WEEK, 0, [timestamp]),
DATENAME(WEEKDAY, [timestamp]),
DATEPART(WEEKDAY, [timestamp]),
DATEPART(HOUR, [timestamp])
) AS hallo
GROUP BY [Day], [Hour], [DayN]
ORDER BY DayN;
但是我收到了这个错误:
Arithmetic overflow error converting expression to data type datetime.
答案 0 :(得分:1)
您必须在子查询中对列进行别名,以便以后可以引用它们。
cast(CONVERT(varchar, [Shipment Date], 101) as date) as whatYouNameIt
上面的行需要有一个列别名,以便您可以使用hallo.whatYouNameIt