在PostgreSQL中将具有不同数据类型和列数的两个SELECT语句合并为一个输出

时间:2017-12-14 08:12:41

标签: sql postgresql-9.1 multi-query

我有两个问题。第一个 -

SELECT
  communications.creation_date as message_date, 
  message as message_text, 
  employees.first_name || ' ' || coalesce(employees.middle_name,'') || ' ' || employees.last_name as message_by
FROM app.communications 
INNER JOIN app.employees ON communications.message_from = employees.emp_id 
WHERE send_as_sms = TRUE AND com_id = (SELECT MAX(com_id) FROM app.communications)

基本上输出 - | message_date | message_text | message_by |

第二个查询 -

SELECT
  cs.com_id, 
  cs.first_name ||' ' || cs.last_name AS recipient_name, 
  cs.sim_number AS phone_number
FROM app.communication_sms cs
WHERE cs.com_id = (SELECT MAX(cs2.com_id) FROM app.communication_sms cs2)
ORDER BY first_name ASC

输出 - | com_id | recipient_name | phone_number |

从查询中可以看出,两个表都有一个"com_id"列。我需要的是制作一个单独的查询,它将合并上面的两个查询以获得单个输出,如 -

|message_date|message_text|message_by|recipient_name|phone_number|

我怎样才能实现这一目标?由于数据类型和列数不同,我无法使用UNION。我很感激你的帮助。

3 个答案:

答案 0 :(得分:1)

不确定com_id是否相等,但如果它们可能不相同,那么我建议:

select * -- list out the columns, I haven't bothered here
FROM (
      SELECT MAX(com_id) as com_id FROM app.communications 
      UNION
      SELECT MAX(cs2.com_id) FROM app.communication_sms cs2
      ) u
left join (
      SELECT
        com_id -- don't know which table this comes from
        communications.creation_date as message_date, 
        message as message_text, 
        employees.first_name || ' ' || coalesce(employees.middle_name,'') || ' ' || employees.last_name as message_by
      FROM app.communications 
      INNER JOIN app.employees ON communications.message_from = employees.emp_id 
      WHERE send_as_sms = TRUE AND com_id = (SELECT MAX(com_id) FROM app.communications)
     ) s1 on u.com_id = s1.com_id
left join (
SELECT
        cs.com_id, 
        cs.first_name ||' ' || cs.last_name AS recipient_name, 
        cs.sim_number AS phone_number
      FROM app.communication_sms cs
      WHERE cs.com_id = (SELECT MAX(cs2.com_id) FROM app.communication_sms cs2)
      ORDER BY first_name ASC
      ) s2 on u.com_id = s2.com_id

注意使用CTE可以避免少量重复

答案 1 :(得分:0)

你有什么理由需要加入或加入吗?在上下文中,您要问com_id问题无关紧要。

尝试这样的事情(这个查询没什么特别的,基本上只是将两者合并在一起)

SELECT
  communications.creation_date as message_date, 
  message as message_text, 
  employees.first_name || ' ' || coalesce(employees.middle_name,'') || ' ' || employees.last_name as message_by
  cs.com_id, 
  cs.first_name ||' ' || cs.last_name AS recipient_name, 
  cs.sim_number AS phone_number
FROM app.communications, app.communication_sms cs
INNER JOIN app.employees ON communications.message_from = employees.emp_id 
WHERE send_as_sms = TRUE AND com_id = (SELECT MAX(com_id) FROM app.communications)
AND cs.com_id = (SELECT MAX(cs2.com_id) FROM app.communication_sms cs2)
ORDER BY first_name ASC

答案 2 :(得分:0)

第一次查询:您希望MAX(c.com_id)标识的上次通信,但仅限于短信(c.send_as_sms = TRUE)。对于此短信,您需要发件人的姓名。此查询会产生一行或零行,具体取决于上次通信是否为SMS。

第二次查询:您希望此时由MAX(cs.com_id)标识的上次短信通信的最后一条短信。这看起来多余。当您从匹配的send_as_sms记录的存在中看到这是一条短信时,为什么communications中有communication_sms?您可能想要考虑这个设计。无论如何,你想要得到收件人的姓名和一些电话号码。

如何组合这两个查询并不明显。您真的想要收件人姓名的最新短信,但只有最后一次短信也是最后一次通讯时才添加发件人姓名吗?这似乎不太可能。我想你更想要

  1. 最新通讯,如果是短信或
  2. 最新的短信通讯
  3. 发件人和收件人。或者,您是否可以通过短信连接进行非短信通信?

    以下是上次短信通信的查询:

    SELECT
      c.creation_date AS message_date, 
      c.message AS message_text, 
      e.first_name || ' ' || coalesce(e.middle_name,'') || ' ' || e.last_name AS message_by,
      cs.com_id, 
      cs.first_name ||' ' || cs.last_name AS recipient_name, 
      cs.sim_number AS phone_number
    FROM (SELECT * FROM app.communication_sms ORDER BY com_id DESC FETCH FIRST ROW ONLY) cs
    JOIN app.communications c ON c.com_id = cs.com_id
    JOIN app.employees e ON c.message_from = e.emp_id 
    ORDER cs.first_name ASC;