sql—填充“select”语句结果中遗漏的行

epggiuax  于 2021-06-26  发布在  Impala
关注(0)|答案(1)|浏览(445)

有没有一种方法可以填充一个结果中省略的行 select 声明?
我有这样的数据:

person,day
1,20
1,24

…由以下简单查询返回:

select *
from example_table
where day>=20

我希望为缺少的天数添加行,结果如下所示:

person,day
1,20
1,21
1,22
1,23
1,24

有没有办法通过sql来实现这一点?

hlswsv35

hlswsv351#

一种解决方案是一个帮助表,其中有一个整数列,其值从1到最大值(31?)。

select person, day
from yourtable
UNION ALL
select intcol, 
from helptable h
where not exists (select day from yourtable y where y.day = h.helptable);

相关问题