Postgresql 从2个查询中减去结果

Postgresql 从2个查询中减去结果,postgresql,Postgresql,我有两个查询,它们给出相同的结果(相同的列、行),但数据不同。我需要将查询#1的一列的结果减去查询#2的同一列 这是我的疑问 QUERY 1 SELECT COUNT(DISTINCT CASE WHEN customer.ref IN ('emp', 'oth') THEN customer.id END) as visit_count, locations.name FROM visits LEFT JOIN customer ON customer.id = visits.id LE

我有两个查询,它们给出相同的结果(相同的列、行),但数据不同。我需要将查询#1的一列的结果减去查询#2的同一列

这是我的疑问

QUERY 1

SELECT 
COUNT(DISTINCT CASE WHEN customer.ref IN ('emp', 'oth') THEN customer.id END) as visit_count, locations.name
FROM visits 
LEFT JOIN customer ON customer.id = visits.id
LEFT JOIN locations ON customer.loc_id = locations.id
WHERE visits.group_id = 1
GROUP BY locations.name, locations.id;

RESULTS
visit_count  |   location
---------------------------
      5      |    loc1
      1      |    loc2
      1      |    loc3

QUERY 2

SELECT COUNT(customer.id) as visit_count, locations.name FROM locations 
LEFT JOIN visits ON locations.name = visits.location_check
LEFT JOIN customer ON visits.cust_id = customer.id AND customer.group_id = 1
WHERE locations.group_id = 1 AND locations.active = true
GROUP BY locations.location_name, locations.id;

RESULTS
visit_count  |   location
---------------------------
      10      |    loc1
      15      |    loc2
      18      |    loc3
结果是一样的,我只需要从第二个结果中减去第一个结果。所以我想要的结果是:

visit_count  |   location
---------------------------
      5      |    loc1
      14     |    loc2
      17     |    loc3

有没有一种方法可以将它们连接在一起并从另一列中减去一列?

这是最简单的答案:

SELECT b.visit_count-a.visit_count, a.location
FROM
(
  SELECT 
  COUNT(DISTINCT CASE WHEN customer.ref IN ('emp', 'oth') THEN customer.id END) as visit_count, locations.name
  FROM visits 
  LEFT JOIN customer ON customer.id = visits.id
  LEFT JOIN locations ON customer.loc_id = locations.id
  WHERE visits.group_id = 1
  GROUP BY locations.name, locations.id;
) as a
(
  SELECT COUNT(customer.id) as visit_count, locations.name FROM locations 
  LEFT JOIN visits ON locations.name = visits.location_check
  LEFT JOIN customer ON visits.cust_id = customer.id AND customer.group_id = 1
  WHERE locations.group_id = 1 AND locations.active = true
  GROUP BY locations.location_name, locations.id;
) as b
WHERE a.location=b.location;
这显然只是您提供的内容的复制/粘贴,但似乎有一些细节需要解决,比如一个查询中的
customer.id=visions.id
,另一个查询中的
customer.id=visions.cust\u id

您似乎希望获得每个位置的总访问次数,减去
customer.ref
emp
oth
的访问次数。如果是这样的话,我想你只是想要这样的东西:

SELECT count(*), l.name as location
FROM visits v
JOIN customer c ON c.id=v.id
JOIN locations l ON l.id=c.loc_id
WHERE v.group_id = 1
AND l.active
AND c.ref NOT IN ('emp','oth')
GROUP BY l.name;
当然,正确的查询取决于您的表结构,因为列名似乎不一致,所以我们在这里并不真正了解表结构


披露:我为

工作这是最直接的答案:

SELECT b.visit_count-a.visit_count, a.location
FROM
(
  SELECT 
  COUNT(DISTINCT CASE WHEN customer.ref IN ('emp', 'oth') THEN customer.id END) as visit_count, locations.name
  FROM visits 
  LEFT JOIN customer ON customer.id = visits.id
  LEFT JOIN locations ON customer.loc_id = locations.id
  WHERE visits.group_id = 1
  GROUP BY locations.name, locations.id;
) as a
(
  SELECT COUNT(customer.id) as visit_count, locations.name FROM locations 
  LEFT JOIN visits ON locations.name = visits.location_check
  LEFT JOIN customer ON visits.cust_id = customer.id AND customer.group_id = 1
  WHERE locations.group_id = 1 AND locations.active = true
  GROUP BY locations.location_name, locations.id;
) as b
WHERE a.location=b.location;
这显然只是您提供的内容的复制/粘贴,但似乎有一些细节需要解决,比如一个查询中的
customer.id=visions.id
,另一个查询中的
customer.id=visions.cust\u id

您似乎希望获得每个位置的总访问次数,减去
customer.ref
emp
oth
的访问次数。如果是这样的话,我想你只是想要这样的东西:

SELECT count(*), l.name as location
FROM visits v
JOIN customer c ON c.id=v.id
JOIN locations l ON l.id=c.loc_id
WHERE v.group_id = 1
AND l.active
AND c.ref NOT IN ('emp','oth')
GROUP BY l.name;
当然,正确的查询取决于您的表结构,因为列名似乎不一致,所以我们在这里并不真正了解表结构


披露:我为

工作,您可以将两个查询都转换为子查询,
连接
结果并在外部查询中进行计算。但可能有更有效的解决方案;为了让我们提供建议,您需要在原始表中显示数据,而不仅仅是现有查询的结果。好的,您可以将两个查询都转换为子查询,
join
结果并在外部查询中进行计算。但可能有更有效的解决方案;为了让我们提供建议,您需要在原始表中显示数据,而不仅仅是现有查询的结果。感谢您的解释!这真的帮助了我!:)谢谢你的解释!这真的帮助了我!:)