简体   繁体   English

联合两个表中的所有客户并从第二个表中分配日期值,以防客户在第一个表中不存在

[英]Union all customers from two tables and assign date values from second table in case customer does not exist in first table

DB-Fiddle DB-小提琴

CREATE TABLE sales 
(
    id SERIAL PRIMARY KEY,
    last_order DATE,
    customer VARCHAR(255)
);

INSERT INTO sales (last_order, customer)
VALUES 
('2020-09-10', 'user_01'),
('2020-10-15', 'user_02'),
('2020-11-26', 'user_03');


CREATE TABLE customers 
(
    id SERIAL PRIMARY KEY,
    first_order DATE,
    customer VARCHAR(255)
);

INSERT INTO customers (first_order, customer)
VALUES 
('2020-08-10', 'user_01'),
('2020-09-15', 'user_02'),
('2020-10-17', 'user_03'),
('2020-05-03', 'user_04'),
('2020-04-12', 'user_05');

Expected result:预期结果:

customer   |   used_date    |
-----------|----------------|------
user_01    |   2020-09-10   |
user_02    |   2020-10-15   |
user_03    |   2020-11-26   |
user_04    |   2020-05-03   |
user_05    |   2020-04-12   |

I have two tables and I want to query all customers in both tables.我有两个表,我想查询两个表中的所有客户。

Additionally, in case the customer does not exist in the table sales I want to apply the first_order from table customers as used_date .此外,如果客户在表sales中不存在,我想将表customers中的first_order应用为used_date

So far I came up with this query:到目前为止,我想出了这个查询:

SELECT
    t1.customer,
    (CASE WHEN t1.last_order IS NULL THEN t1.first_order ELSE t1.last_order END) AS used_date
FROM
   (SELECT
        s.customer,
        s.last_order AS last_order,
        NULL::date AS first_order
    FROM 
        sales s
    GROUP BY 
        1, 2, 3

    UNION ALL
 
    SELECT
        c.customer,
        NULL::date AS last_order,
        c.first_order AS first_order
    FROM 
        customers c
    GROUP BY 
        1, 2, 3) t1
GROUP BY 
    1, 2
ORDER BY 
    1;

How do I need to modify the CASE WHEN statement to get the expected result?如何修改CASE WHEN语句以获得预期结果?

This looks like a left join :这看起来像left join

select c.*, 
       coalesce(s.last_order, c.first_order) as used_date
from customers c left join
     sales s
     on c.customer = s.customer;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 连接两个表并将第二个表中的值分配给第一个表的每个日期 - Join two tables and assign values from second table to each date of the first table 将两个表中的行配对,如果第二个表中不存在,则仅显示第一个表的内容 - Pairing rows from two tables and show only content of first table if it does not exist in second table 如何查询两个表并从第一个表返回所有记录,而不管第二个表中是否存在 - How to query two tables and return all records from first, regardless on whether exist in the second table MYSQL:两个表之间的UNION结果,如果在第二个表中找到PK,则从第一个表中删除记录 - MYSQL: UNION results between two tables where omitting records from first table if PK found in second table 使用从第二个字段开始的表2的数据来联合两个表 - Union two tables with data of table 2 starting from second field MySQL 5.1.7有条件地联接两个表,从第二个表中按日期选择顶部记录,从第一个表中选择所有记录 - MySQL 5.1.7 conditional join of two tables selecting top record by date from second table and all records from first 比较两个表中的值,并将从第一个表中获取的值插入第二个表中 - Compare the values in two tables and insert into the second table the value fetched from the first table 如何连接两个表并仅从第二个表中获取值(如果存在)? - How do I join two tables and only take values from the second table if they exist? 从两个表中获取所有记录,但仅从第二个表中获取不在第一个表中的记录 - get all records from two tables but from second table only the ones that are not in first 如何联接两个表并从第一个表返回不在第二个表中的值? - How do I join two tables and return the values from the first that aren't in the second table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM