英文:
Error Code: 3588. Window '<unnamed window>' with RANGE frame has ORDER BY expression of datetime type. Only INTERVAL bound value allowed
问题
I need to find the customers who ordered for 3 consecutive days with every order amount greater than $50 for the data in table sales.mytable. This is the SQL query i wrote for this in MySQL workbench.
SELECT T.Customer_ID, T.Customer_Name, T.Order_Date
FROM (SELECT T.*, COUNT(*) OVER(ORDER BY Order_Date RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) cnt_days
FROM sales.mytable T
WHERE Sales > 50) T
WHERE cnt_days = 3;
My sample table looks like this.
# Row_ID, Order_ID, Order_Date, Ship_Date, Ship_Mode, Customer_ID, Customer_Name, Segment, Country, City, State, Postal_Code, Region, Product_ID, Category, Sub_Category, Product_Name, Sales
'1', 'CA-2017-152156', '2017-11-08', '2017-11-11', 'Second Class', 'CG-12520', 'Claire Gute', 'Consumer', 'United States', 'Henderson', 'Kentucky', '42420.0', 'South', 'FUR-BO-10001798', 'Furniture', 'Bookcases', 'Bush Somerset Collection Bookcase', '261.9600'
And i receive this error. Error Code: 3588. Window '
I am sure OVER(ORDER BY Order_Date RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) is a reason but couldnt find any issue. What could be the mistake i made writing this query.
My Table structure is
英文:
I need to find the customers who ordered for 3 consecutive days with every order amount greater than $50 for the data in table sales.mytable. This is the SQL query i wrote for this in MySQL workbench.
SELECT T.Customer_ID, T.Customer_Name, T.Order_Date
FROM (SELECT T.*, COUNT(*) OVER(ORDER BY Order_Date RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) cnt_days
FROM sales.mytable T
WHERE Sales>'50')T
WHERE cnt_days = 3;
My sample table looks like this.
# Row_ID, Order_ID, Order_Date, Ship_Date, Ship_Mode, Customer_ID, Customer_Name, Segment, Country, City, State, Postal_Code, Region, Product_ID, Category, Sub_Category, Product_Name, Sales
'1', 'CA-2017-152156', '2017-11-08', '2017-11-11', 'Second Class', 'CG-12520', 'Claire Gute', 'Consumer', 'United States', 'Henderson', 'Kentucky', '42420.0', 'South', 'FUR-BO-10001798', 'Furniture', 'Bookcases', 'Bush Somerset Collection Bookcase', '261.9600'
And i receive this error. Error Code: 3588. Window '<unnamed window>' with RANGE frame has ORDER BY expression of datetime type. Only INTERVAL bound value allowed.
I am sure OVER(ORDER BY Order_Date RANGE BETWEEN 1 PRECEDING AND 1 FOLLOWING) is a reason but couldnt find any issue. What could be the mistake i made writing this query.
My Table structure is
答案1
得分: 1
ORDER BY表达式数据类型为DATE。您指定RANGE偏移 - 即按值偏移。因此,偏移的数据类型必须与DATE兼容。
...
COUNT(*) OVER ( ORDER BY Order_Date
RANGE BETWEEN INTERVAL 1 DAY PRECEDING
AND INTERVAL 1 DAY FOLLOWING ) cnt_days
...
但如果您的BETWEEN 1 PRECEDING AND 1 FOLLOWING
表示行数,则必须使用ROWS偏移,而不是RANGE。
...
COUNT(*) OVER ( ORDER BY Order_Date
ROWS BETWEEN 1 PRECEDING
AND 1 FOLLOWING ) cnt_days
...
英文:
ORDER BY expression datatype is DATE. You specify RANGE shift - i.e. a shift by the value. So the datatype of the shift must be compatible with DATE.
...
COUNT(*) OVER ( ORDER BY Order_Date
RANGE BETWEEN INTERVAL 1 DAY PRECEDING
AND INTERVAL 1 DAY FOLLOWING ) cnt_days
...
But if your BETWEEN 1 PRECEDING AND 1 FOLLOWING
means the rows amount then you must use ROWS shift, not RANGE.
...
COUNT(*) OVER ( ORDER BY Order_Date
ROWS BETWEEN 1 PRECEDING
AND 1 FOLLOWING ) cnt_days
...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论