我想创建一个接受2023年所有日期的查询(在本例中,我使用Order表作为辅助表,它用于订单注册以获得日期,它始终具有每天的日期),然后使用所有客户ID提供Left Join,您每天有多少订单,如果您当天没有任何订单,则返回0.我以客户端id 10552为例,但还会有其他几个.

我只使用了一个表,即order,它包含下了订单,我将在其中使用它来获取一年中的日期以及按客户ID计算的订货量.

当我执行select * from order时,来自order表的一些信息的示例:

created_at order_status_id client_id
2018-04-05 22:19:29.000 3 10,282
2018-04-22 23:47:36.000 3 2
2018-04-25 23:42:22.000 3 11
2018-04-06 19:54:33.000 4 2
2018-04-09 12:11:06.000 3 2

我正在try 使用以下查询来执行我想要的操作:

with Datas as (
select distinct 
   CAST(o.created_at AS DATE) datas
from platform_bs."order" o
where date_format(o.created_at, '%Y') = '2023'
order by 1
),-- here I put it to list all the dates from 2023 until now
count_orders_client as(
SELECT
   datas,
   od.client_id,
   nullif(count(od.id),0) as count
from Datas as dt
left join platform_bs."order" od ON dt.datas = CAST(od.created_at AS DATE)
group by 1,2
order by 1,2
)-- here I do a left join to get the dates, client id and a count and where should I do it if there were no records on the day, it would return 0
select * from count_orders_client where client_id = 10552

我想让它退还这个:

datas client_id count
2023-01-01 10552 1
2023-01-02 10552 1
2023-01-03 10552 0
2023-01-04 10552 0
2023-01-05 10552 0
2023-01-06 10552 0
2023-01-07 10,552 1
2023-01-08 10,552 2
2023-01-09 10552 0
2023-01-10 10552 0
2023-01-11 10552 0
2023-01-12 10552 0

但当运行时,它返回如下所示:

datas client_id count
2023-01-01 10,552 1
2023-01-02 10,552 1
2023-01-07 10,552 1
2023-01-08 10,552 2
2023-01-13 10,552 1
2023-01-23 10,552 1
2023-02-27 10,552 1

我没有让左连接的一部分将2023年的日期与Order表连接起来,以返回值​​0.我该怎么办?

编辑1:正在使用AWS雅典娜

推荐答案

通常,要生成日期范围(最多sequence00个元素),我会使用sequence(它使所有内容都更清晰、更易于管理),因此您可以try 以下操作:

-- sample data
with dataset(created_at, order_status_id, client_id) as (
    values (timestamp '2023-04-05 22:19:29.000',    3,  '11'),
        (timestamp '2023-04-06 23:47:36.000',   3,  '2'),
        (timestamp '2023-04-07 23:42:22.000',   3,  '11'),
        (timestamp '2023-04-06 19:54:33.000',   4,  '2'),
        (timestamp '2023-04-08 12:11:06.000',   3,  '2')
),
-- query parts
clients as ( -- find all unique clients
    select distinct client_id
    from dataset
),
client_dates as ( -- create cartesian product of clients and dates
    select *
    from clients,
    -- use here needed start and end for `sequence`:
    unnest(sequence(date '2023-04-04', date '2023-04-09', interval '1' day)) as t(dt) 

)

select cd.client_id, dt, count(order_status_id) count
from client_dates cd
left join dataset d
    on cd.dt = date(created_at) and cd.client_id = d.client_id
group by cd.client_id, cd.dt;

输出:

client_id dt count
2 2023-04-04 0
2 2023-04-05 0
2 2023-04-06 2
2 2023-04-07 0
2 2023-04-08 1
2 2023-04-09 0
11 2023-04-04 0
11 2023-04-05 1
11 2023-04-06 0
11 2023-04-07 1
11 2023-04-08 0
11 2023-04-09 0

Sql相关问答推荐

如何返回字符串中包含相同值的数据?

在SQL中向每个子字节组添加字节行

用于平均多个数据并与一个数据点进行比较以判断偏移量的SQL查询

如何在联接条件不匹配时按日期获取上一条记录

如何根据计数和分组获取订单总数

DBeaver将过程中的属性列表转换为字符串

SQL:如何在表中同时使用GROUPING和CONDITION?

重用传递给 node 的参数-postgres upsert查询

动态组/转置

如何为该查询编写正确分区依据

通过对象分离实现原子性

存储过程太慢

将二维数组的第一个和第二个元素取消嵌套到两个一维数组中

递归 CTE 附加为行

在presto sql中解析带有区域的时间格式

BigQuery数组是否包含NULL值的判断方法

自动生成计算频率的列

SQL Select 最大并获取列名

SQL中所有先前日期的累计总和

如何刷新在视图之上创建的表