SQL:在一个查询中对两个select语句的列执行计算

时间:2018-04-03 11:28:09

标签: sql postgresql

我想从两个不相关的SQL表(由单独的SQL查询构成)中选择信息,为我提供一个包含四列的表:一个作为标识符,一个表示第一个表中的值,一个表示第二个表中的值,和一个计算他们之间。

一个简单的例子是:

tblSold

 Shop name | items sold
 Shop A    | 100   
 Shop B    | 50    
 Shop C    | 75    
 Shop D    | 80    

tblReturned

  Shop name | Items returned
  Shop A    | 10
  Shop B    | 5   
  Shop C    | 7 
  Shop D    | 8   

我正试图找一张看起来像这样的表

  Shop name | items sold | items returned | net (sold - returned)
  Shop A    | 100        | 10             | 90
  Shop B    | 50         | 5              | 45
  Shop C    | 75         | 7              | 68
  Shop D    | 80         | 8              | 72

在上面的例子中,tblSold来自一个如下所示的查询:

SELECT global.shopname, COUNT(stock.sold) 
FROM global
INNER JOIN  stock ON global.id = stock.shop
GROUP BY shopname

tblReturned来自一个如下所示的查询:

SELECT global.shopname, COUNT(stock.returned) 
FROM global
INNER JOIN  stock ON global.id = stock.shop
WHERE stock.datereturned > 1.01.2010
GROUP BY shopname

从上一个问题SQL: Two select statements in one query我得到以下问题:

(SELECT global.shopname, COUNT(stock.sold) 
FROM global
INNER JOIN  stock ON global.id = stock.shop
GROUP BY shopname)

UNION

(SELECT global.shopname, COUNT(stock.returned) 
FROM global
INNER JOIN  stock ON global.id = stock.shop
WHERE stock.datereturned > 1.01.2010
GROUP BY shopname)

这给了我一张只有两列的表,无法进行计算。有没有办法以某种方式加入这些以使其有效?

3 个答案:

答案 0 :(得分:4)

我认为你只需要条件聚合:

SELECT g.shopname, COUNT(s.sold),
       SUM(CASE WHEN s.datereturned > '2010-01-01' AND s.returned IS NOT NULL THEN 1 ELSE 0 END) as returned,
       (COUNT(s.sold) - 
        SUM(CASE WHEN s.datereturned > '2010-01-01' AND s.returned IS NOT NULL THEN 1 ELSE 0 END)
       ) as net
FROM global INNER JOIN
     stock
     ON g.id = s.shop
GROUP BY g.shopname;

让日期逻辑仅适用于退货很奇怪。另外,我猜你想要SUM()的广告资源,而不是COUNT()。因此,这可能会产生更准确的结果:

SELECT g.shopname, SUM(s.sold) as items_sold,
       SUM(CASE WHEN s.datereturned > '2010-01-01' THEN s.returned ELSE 0 END) as items_returned,
       SUM(CASE WHEN s.datereturned > '2010-01-01' THEN s.items_sold - s.returned
                ELSE s.items_sold END)
FROM global INNER JOIN
     stock
     ON g.id = s.shop
GROUP BY g.shopname;

答案 1 :(得分:0)

SELECT t1.shopname,
       t1.items_sold,
       t2.items_returned,(t1.items_sold-t2.items_returned) AS NET 
FROM (SELECT global.shopname, COUNT(stock.sold) as items_sold
      FROM global
      INNER JOIN  stock ON global.id = stock.shop
      GROUP BY shopname) t1
INNER JOIN  (SELECT global.shopname, COUNT(stock.returned) as items_returned
             FROM global
             INNER JOIN  stock ON global.id = stock.shop
             WHERE stock.datereturned > '2010-01-01'
             GROUP BY shopname) t2
ON t1.shopname = t2.shopname

答案 2 :(得分:0)

您可以使用CTE

WITH tblSold AS (
    SELECT
      global.shopname,
      COUNT(stock.sold) AS sold
    FROM global
      INNER JOIN stock ON global.id = stock.shop
    GROUP BY shopname),
    tblReturned AS (
      SELECT
        global.shopname,
        COUNT(stock.returned) AS returned
      FROM global
        INNER JOIN stock ON global.id = stock.shop
      WHERE stock.datereturned > '1.01.2010'
      GROUP BY shopname)
SELECT
  shopname,
  sold,
  returned,
  sold - returned
FROM tblSold
  INNER JOIN tblReturned USING (shopname);