0

我有两个疑问。首先 -

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由于不同的数据类型和不同的列数,我无法使用。我会感谢你们的帮助。

4

3 回答 3

2

不确定 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 可以避免少量重复

于 2017-12-14T08:28:23.817 回答
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
于 2017-12-14T08:23:27.540 回答
0

第一个查询:您想要由 标识的最后一次通信MAX(c.com_id),但前提是它是 SMS ( c.send_as_sms = TRUE)。对于此 SMS,您需要发件人的姓名。根据上次通信是否为 SMS,此查询会产生一行或零行。

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

如何组合这两个查询并不明显。您是否真的想要带有收件人姓名的最新短信,但只添加发件人姓名以防最后一条短信也是最后一次通信?这似乎不太可能。我猜你宁愿想要

  1. 最新的通信,以防它是 SMS 或
  2. 最新的短信通讯

对于发件人和收件人。或者您可以通过附加的 SMS 进行非 SMS 通信吗?

下面是最后一次短信通信的查询:

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;
于 2017-12-14T09:24:04.037 回答