UNION ALL 并用 NULL 填充日期列
UNION ALL and fill date column with NULL
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
last_order DATE,
customer VARCHAR(255)
);
INSERT INTO sales
(last_order, customer)
VALUES
('2020-04-10', 'user_01'),
('2020-06-15', 'user_02'),
('2020-08-26', 'user_03');
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
first_order DATE,
customer VARCHAR(255)
);
INSERT INTO customers
(first_order, customer)
VALUES
('2020-03-10', 'user_01'),
('2020-05-15', 'user_02'),
('2020-01-17', 'user_03');
预期结果:
last_order | first_order |
-------------|-------------------|----
2020-04-10 | |
2020-06-15 | |
2020-08-26 | |
| 2020-03-10 |
| 2020-05-15 |
| 2020-01-17 |
| |
在 table sales
我有 last_order
的日期,在 table customers
我有 first_order
的日期.
现在我想 UNION ALL
两个 tables,因此需要用 NULL[=42= 填充 last_order
列和 - 分别 - first_order
列] 以保持结构相等。
在 postgresSQL
中,我正在使用这个查询,它没有任何问题:
SELECT
s.last_order AS last_order,
NULL AS first_order
FROM sales s
UNION ALL
SELECT
NULL AS last_order,
c.first_order AS first_order
FROM customers c
但是,当我将此查询应用于 amazon-redshift
时,出现错误:
Invalid operation: UNION types date and text cannot be matched
我需要如何修改amazon-redshift
中的查询才能达到预期的结果?
按照@Serg 的建议,请尝试将 null 转换为日期,如下所示:
查询:
SELECT
s.last_order AS last_order,
NULL::date AS first_order
FROM sales s
UNION ALL
SELECT
NULL::date AS last_order,
c.first_order AS first_order
FROM customers c
输出:
last_order
first_order
2020-04-10
null
2020-06-15
null
2020-08-26
null
null
2020-03-10
null
2020-05-15
null
2020-01-17
db<>fiddle here
一个有趣的方法是:
select s.last_order, c.first_order
from sales s full join
customers c
on 1 = 0; -- never true!
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
last_order DATE,
customer VARCHAR(255)
);
INSERT INTO sales
(last_order, customer)
VALUES
('2020-04-10', 'user_01'),
('2020-06-15', 'user_02'),
('2020-08-26', 'user_03');
CREATE TABLE customers (
id SERIAL PRIMARY KEY,
first_order DATE,
customer VARCHAR(255)
);
INSERT INTO customers
(first_order, customer)
VALUES
('2020-03-10', 'user_01'),
('2020-05-15', 'user_02'),
('2020-01-17', 'user_03');
预期结果:
last_order | first_order |
-------------|-------------------|----
2020-04-10 | |
2020-06-15 | |
2020-08-26 | |
| 2020-03-10 |
| 2020-05-15 |
| 2020-01-17 |
| |
在 table sales
我有 last_order
的日期,在 table customers
我有 first_order
的日期.
现在我想 UNION ALL
两个 tables,因此需要用 NULL[=42= 填充 last_order
列和 - 分别 - first_order
列] 以保持结构相等。
在 postgresSQL
中,我正在使用这个查询,它没有任何问题:
SELECT
s.last_order AS last_order,
NULL AS first_order
FROM sales s
UNION ALL
SELECT
NULL AS last_order,
c.first_order AS first_order
FROM customers c
但是,当我将此查询应用于 amazon-redshift
时,出现错误:
Invalid operation: UNION types date and text cannot be matched
我需要如何修改amazon-redshift
中的查询才能达到预期的结果?
按照@Serg 的建议,请尝试将 null 转换为日期,如下所示:
查询:
SELECT
s.last_order AS last_order,
NULL::date AS first_order
FROM sales s
UNION ALL
SELECT
NULL::date AS last_order,
c.first_order AS first_order
FROM customers c
输出:
last_order | first_order |
---|---|
2020-04-10 | null |
2020-06-15 | null |
2020-08-26 | null |
null | 2020-03-10 |
null | 2020-05-15 |
null | 2020-01-17 |
db<>fiddle here
一个有趣的方法是:
select s.last_order, c.first_order
from sales s full join
customers c
on 1 = 0; -- never true!