gpt4 book ai didi

sql - 每辆车相对有 Count(For that Day), Count for last 10 days 和 Count of last 20 days

转载 作者:行者123 更新时间:2023-12-02 02:39:37 33 4
gpt4 key购买 nike

我试图查看当天的车辆销量,并创建另外两个列来告诉我过去 10 天的销量和过去 20 天的销量。同一天和同一辆车可能有多个销售。我的目标是获取不同的车辆和日期并查看他们的销售数量。

N 天计数应与该行中出现的日期相关。

我的数据看起来像:

 Vehicle_ID       Sales_Date
X500 01/03/2020 02:00:00 PM
X500 01/11/2020 05:00:00 PM
X500 01/25/2020 06:00:00 PM
X500 01/25/2020 01:00:00 PM
X500 02/13/2020 06:00:00 PM
X500 02/21/2020 02:00:00 PM

我的目标表应该是这样的

 Vehicle_ID       Sales_Date     Sales_Count    Sales_Count_last_10     Sales_Count_last_20
X500 01/03/2020 1 1 1
X500 01/11/2020 1 2 2
X500 01/25/2020 2 2 3
X500 02/13/2020 1 1 4
X500 02/21/2020 1 2 2

我尝试了以下查询:

SELECT TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY') Sales_Date1, Vehicle_ID, 
COUNT(*) OVER (PARTITION BY Vehicle_ID, TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY')) SALES_COUNTS,
SUM(CASE WHEN TO_DATE(Sales_Date) BETWEEN TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY') - 10 AND TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY')
THEN 1 ELSE 0 END) OVER (PARTITION BY Vehicle_ID, TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY'))
Sales_Count_last_10,
SUM(CASE WHEN TO_DATE(Sales_Date) BETWEEN TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY') - 20 AND TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY')
THEN 1 ELSE 0 END) OVER (PARTITION BY Vehicle_ID, TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY'))
Sales_Count_last_10
FROM TABLE1

它只是为 Sales_Count、Sales_Count_last_10 和 Sales_Count_last_20 创建了相同的值。这是 N 天列只是 Sales_Count 的副本

我还尝试了以下查询。

SELECT TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY') Sales_Date1, Vehicle_ID, COUNT(*) SALES_COUNTS,
SUM(CASE WHEN TO_DATE(Sales_Date) BETWEEN TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY') - 10 AND TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY')
THEN 1 ELSE 0 END) Sales_Count_last_10,
SUM(CASE WHEN TO_DATE(Sales_Date) BETWEEN TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY') - 20 AND TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY')
THEN 1 ELSE 0 END) Sales_Count_last_20
FROM TABLE1
GROUP BY Vehicle_ID, TO_DATE(TO_CHAR(Sales_Date, 'MM/DD/YYYY'), 'MM/DD/YYYY')

它显示了与上一个查询相同的结果。复制 N 天列的 Sales_Count。

请帮我解决这个问题。

最佳答案

您可以使用sum 的窗口子句来指定您要查看多少天。

range between N preceding and current row,它包括从当前行 - N 到当前的所有值

给出:

with rws as (
select 'X500' vehicle_id, to_date ( '01/03/2020 02:00:00 PM', 'mm/dd/yyyy hh:mi:ss pm' ) sales_date from dual union all
select 'Y300' vehicle_id, to_date ( '01/11/2020 05:00:00 PM', 'mm/dd/yyyy hh:mi:ss pm' ) sales_date from dual union all
select 'Q240' vehicle_id, to_date ( '01/25/2020 06:00:00 PM', 'mm/dd/yyyy hh:mi:ss pm' ) sales_date from dual union all
select 'Q240' vehicle_id, to_date ( '01/25/2020 01:00:00 PM', 'mm/dd/yyyy hh:mi:ss pm' ) sales_date from dual union all
select 'F310' vehicle_id, to_date ( '02/13/2020 06:00:00 PM', 'mm/dd/yyyy hh:mi:ss pm' ) sales_date from dual union all
select 'E990' vehicle_id, to_date ( '02/21/2020 02:00:00 PM', 'mm/dd/yyyy hh:mi:ss pm' ) sales_date from dual
)
select distinct trunc ( sales_date ), vehicle_id,
count(*) over (
partition by vehicle_id, trunc(sales_date)
) sales_counts,
count (*) over (
order by trunc ( sales_date )
range between 10 preceding and current row
) last_10,
count (*) over (
order by trunc ( sales_date )
range between 20 preceding and current row
) last_20
from rws
order by 1, 2

TRUNC(SALES_DATE) VEHICLE_ID SALES_COUNTS LAST_10 LAST_20
03-JAN-2020 00:00:00 X500 1 1 1
11-JAN-2020 00:00:00 Y300 1 2 2
25-JAN-2020 00:00:00 Q240 2 2 3
13-FEB-2020 00:00:00 F310 1 1 3
21-FEB-2020 00:00:00 E990 1 2 2

请注意,如果您想从 Oracle 数据库中的日期中删除时间组件,只需trunc 即可。比 to_date(to_char(..., 'fmt')) 容易得多!

关于sql - 每辆车相对有 Count(For that Day), Count for last 10 days 和 Count of last 20 days,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60657378/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com