Error Code: 3588. Window '<unnamed window>' with RANGE frame has ORDER BY expression of datetime type. Only INTERVAL bound value allowed

huangapple go评论142阅读模式
英文:

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 '' 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

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&gt;&#39;50&#39;)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
&#39;1&#39;, &#39;CA-2017-152156&#39;, &#39;2017-11-08&#39;, &#39;2017-11-11&#39;, &#39;Second Class&#39;, &#39;CG-12520&#39;, &#39;Claire Gute&#39;, &#39;Consumer&#39;, &#39;United States&#39;, &#39;Henderson&#39;, &#39;Kentucky&#39;, &#39;42420.0&#39;, &#39;South&#39;, &#39;FUR-BO-10001798&#39;, &#39;Furniture&#39;, &#39;Bookcases&#39;, &#39;Bush Somerset Collection Bookcase&#39;, &#39;261.9600&#39;

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

Error Code: 3588. Window '<unnamed window>' with RANGE frame has ORDER BY expression of datetime type. Only INTERVAL bound value allowed

答案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
...

huangapple
  • 本文由 发表于 2023年3月7日 11:18:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75657742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定