如果有3行数据,如何选择5行,接下来的2行应为空行。
答案 0 :(得分:2)
您可以将结果与空行结合使用,该行应具有与表相同类型的列。例如:
with my_table(id, str, a_date) as (
values
(1, 'first', '2017-09-01'::date),
(2, 'second', '2017-09-02'),
(3, 'third', '2017-09-03')
)
select *
from my_table
union all
select null::int, null::text, null::date
from generate_series(1, 5) -- generate 5 empty rows
order by id nulls last
limit 5
id | str | a_date
----+--------+------------
1 | first | 2017-09-01
2 | second | 2017-09-02
3 | third | 2017-09-03
| |
| |
(5 rows)
答案 1 :(得分:0)
您需要一个包含5个数字的表,然后为表分配行号并执行左连接。
cte是数据,最后是查询需要它:
<强> SQL DEMO 强>
WITH all5 as (
SELECT *
FROM (VALUES(1), (2), (3), (4), (5)) AS t (ID)
), some2 as (
SELECT *
FROM (VALUES('first'), ('second')) AS t (value)
)
SELECT *
FROM all5
LEFT JOIN (SELECT row_number() OVER (ORDER BY value) as rn, *
FROM some2) as t
ON all5.ID = t.rn
<强>输出强>