为 null groupby sql(presto) 填充未来日期
Fill Future date for null groupby sql(presto)
这可能比我想的要简单,但本质上是想填写 ID 2 为空的值。示例如下。谢谢
给定 Table:
|ID| food category | time |
:--:----------:-------
|1 |italian | 2021-10-01|
|1 | indian | 2021-10-23|
|1 | american| 2021-10-05|
|1 | mexican | 2021-10-07|
|1 | Chinese | 2021-10-09|
|1 | vietnamese| 2021-10-11|
|1 | thai | 2021-10-12|
|1 | Moroccan| 2021-9-01|
|1 | russian | 2021-7-01|
|1 | korean | 2021-4-30|
|1 | canadian| 2021-7-01|
|2 |italian | 2020-10-11|
|2 | indian | 2021-04-23|
|2 | american| 2021-10-25|
|2 | mexican | 2021-10-27|
我想通过按 id 和食物类别分组来转换上面的 table,但仍然有时间将 ID 2 替换为未来的日期(date_add('year',1, now()) 为空时间。因为对于中国、越南、泰国、摩洛哥、俄罗斯、韩国和加拿大的食品类别,ID 2 没有记录,所以这些将是空的,但我会希望他们在 table 之前仍显示在组中,并在 1 年后的日期之前放置。下面是所需结果的示例。感谢您的帮助。
想要Table:
|ID| food category | time |
:--:----------:-------
|1 |italian | 2021-10-01|
|1 | indian | 2021-10-23|
|1 | american| 2021-10-05|
|1 | mexican | 2021-10-07|
|1 | Chinese | 2021-10-09|
|1 | vietnamese| 2021-10-11|
|1 | thai | 2021-10-12|
|1 | Moroccan| 2021-9-01|
|1 | russian | 2021-7-01|
|1 | korean | 2021-4-30|
|1 | canadian| 2021-7-01|
|2 |italian | 2020-10-11|
|2 | indian | 2021-04-23|
|2 | american| 2021-10-25|
|2 | mexican | 2021-10-27|
|2 | Chinese | 2022-11-23|
|2 | vietnamese| 2022-11-23|
|2 | thai | 2022-11-23|
|2 | Moroccan| 2022-11-23|
|2 | russian | 2022-11-23|
|2 | korean | 2022-11-23|
|2 | canadian| 2022-11-23|
首先使用 CTE 收集食品类别列表。然后收集 ID 列表。
WITH cteCat AS (
select distinct [food category] from table
)
, cteID AS (
select distinct [ID] from table
)
SELECT id.[ID], cat.[food category],
COALESCE(t.[time], dateadd(year, 1, getdate())) as [time]
FROM cteCat cat
, cteID id
LEFT OUTER JOIN table t
ON t.[ID] = id.[ID]
AND t.[food category] = cat.[food category]
您可以使用以下查询
SELECT COALESCE(t1.ID,t2.ID) as ID,
COALESCE(t1.foodcategory,t2.foodcategory) as foodcategory,
CAST(COALESCE(t2.time,dateadd(year, 1, getdate())) AS DATE) time
FROM
(SELECT *
FROM
(SELECT foodcategory
FROM testTB
GROUP BY foodcategory) t1
JOIN
(SELECT id
FROM testTB
GROUP BY id) t2 on 1=1) t1
LEFT JOIN testTB t2 on t1.ID = t2.ID and t1.foodcategory = t2.foodcategory
或
WITH cte AS (
select distinct foodcategory from testTB
)
SELECT t2.ID,t1.foodcategory,CAST(COALESCE(t3.time,dateadd(year, 1, getdate())) AS DATE) time
FROM cte t1
FULL OUTER JOIN (
select distinct [ID] from testTB
) t2 on 1=1
left join testTB t3 on t2.ID = t3.ID and t1.foodcategory = t3.foodcategory
order by t2.id
演示在 db<>fiddle
这可能比我想的要简单,但本质上是想填写 ID 2 为空的值。示例如下。谢谢
给定 Table:
|ID| food category | time |
:--:----------:-------
|1 |italian | 2021-10-01|
|1 | indian | 2021-10-23|
|1 | american| 2021-10-05|
|1 | mexican | 2021-10-07|
|1 | Chinese | 2021-10-09|
|1 | vietnamese| 2021-10-11|
|1 | thai | 2021-10-12|
|1 | Moroccan| 2021-9-01|
|1 | russian | 2021-7-01|
|1 | korean | 2021-4-30|
|1 | canadian| 2021-7-01|
|2 |italian | 2020-10-11|
|2 | indian | 2021-04-23|
|2 | american| 2021-10-25|
|2 | mexican | 2021-10-27|
我想通过按 id 和食物类别分组来转换上面的 table,但仍然有时间将 ID 2 替换为未来的日期(date_add('year',1, now()) 为空时间。因为对于中国、越南、泰国、摩洛哥、俄罗斯、韩国和加拿大的食品类别,ID 2 没有记录,所以这些将是空的,但我会希望他们在 table 之前仍显示在组中,并在 1 年后的日期之前放置。下面是所需结果的示例。感谢您的帮助。
想要Table:
|ID| food category | time |
:--:----------:-------
|1 |italian | 2021-10-01|
|1 | indian | 2021-10-23|
|1 | american| 2021-10-05|
|1 | mexican | 2021-10-07|
|1 | Chinese | 2021-10-09|
|1 | vietnamese| 2021-10-11|
|1 | thai | 2021-10-12|
|1 | Moroccan| 2021-9-01|
|1 | russian | 2021-7-01|
|1 | korean | 2021-4-30|
|1 | canadian| 2021-7-01|
|2 |italian | 2020-10-11|
|2 | indian | 2021-04-23|
|2 | american| 2021-10-25|
|2 | mexican | 2021-10-27|
|2 | Chinese | 2022-11-23|
|2 | vietnamese| 2022-11-23|
|2 | thai | 2022-11-23|
|2 | Moroccan| 2022-11-23|
|2 | russian | 2022-11-23|
|2 | korean | 2022-11-23|
|2 | canadian| 2022-11-23|
首先使用 CTE 收集食品类别列表。然后收集 ID 列表。
WITH cteCat AS (
select distinct [food category] from table
)
, cteID AS (
select distinct [ID] from table
)
SELECT id.[ID], cat.[food category],
COALESCE(t.[time], dateadd(year, 1, getdate())) as [time]
FROM cteCat cat
, cteID id
LEFT OUTER JOIN table t
ON t.[ID] = id.[ID]
AND t.[food category] = cat.[food category]
您可以使用以下查询
SELECT COALESCE(t1.ID,t2.ID) as ID,
COALESCE(t1.foodcategory,t2.foodcategory) as foodcategory,
CAST(COALESCE(t2.time,dateadd(year, 1, getdate())) AS DATE) time
FROM
(SELECT *
FROM
(SELECT foodcategory
FROM testTB
GROUP BY foodcategory) t1
JOIN
(SELECT id
FROM testTB
GROUP BY id) t2 on 1=1) t1
LEFT JOIN testTB t2 on t1.ID = t2.ID and t1.foodcategory = t2.foodcategory
或
WITH cte AS (
select distinct foodcategory from testTB
)
SELECT t2.ID,t1.foodcategory,CAST(COALESCE(t3.time,dateadd(year, 1, getdate())) AS DATE) time
FROM cte t1
FULL OUTER JOIN (
select distinct [ID] from testTB
) t2 on 1=1
left join testTB t3 on t2.ID = t3.ID and t1.foodcategory = t3.foodcategory
order by t2.id
演示在 db<>fiddle