相关子查询:如何获取外部查询的ID?

时间:2015-06-01 07:45:33

标签: sql sum subquery correlated-subquery

我有多个表格:

Person
---------------------
IDPerson, Name

StickerTransaction
---------------------
IDTransac, IDPerson, IDSomeoneElse, NbStickersReceived, NbStickersGiven

Purchase
---------------------
IDPurchase, IDPerson, NbStickersBought

我正试图让每个从未进行交易或目前有0贴纸的人。要获得一个人拥有的贴纸数量,这里是公式:

NbStickersBought + NbStickersReceived - NbStickersGiven

这是我到目前为止的查询。我遇到的问题是在子子查询中...我无法访问该人的ID。

有没有办法可以访问此人的ID?还是有更好的方法来编写此查询?

SELECT People.IDPerson, People.Name
FROM
(
    -- Selects people that never made a sticker transaction

    SELECT p.IDPerson, p.Name
    FROM Person p
    LEFT JOIN StickerTransaction sT
    ON p.IDPerson = sT.IDPerson
    WHERE sT.IDPerson IS NULL

    UNION

    -- Selects people that have currently 0 sticker

    SELECT p.IDPerson, p.Name
    FROM Person p
    WHERE 0 = (
                SELECT SUM(NbStickers) AS NbStickers
                FROM (

                       -- I do not have access to p.IDPerson here...

                       SELECT (sT.NbStickersReceived - sT.NbStickersGiven) AS NbStickers
                       FROM StickerTransaction sT
                       WHERE sT.IDPerson = p.IDPerson

                     UNION ALL

                       -- I do not have access to p.IDPerson here either...

                       SELECT pu.NbStickersBought AS NbStickers
                       FROM Purchase pu
                       WHERE pu.IDPerson = p.IDPerson
                     )
              )
) People

4 个答案:

答案 0 :(得分:0)

SELECT People.IDPerson, People.Name
FROM Person 
where IDPerson not in 
   (select distinct IDPerson from StickerTransaction)
and IDPerson not in 
   (SELECT IDPerson from Purchase group by IDPerson having SUM(NbStickers) > 0)

答案 1 :(得分:0)

AFAIK您无法与派生表进行关联。但是您可以将查询重写为非相关子查询:

SELECT People.IDPerson, People.NAME
FROM
(
    -- Selects people that never made a sticker transaction

    SELECT p.IDPerson, p.NAME
    FROM Person p
    LEFT JOIN StickerTransaction sT
    ON p.IDPerson = sT.IDPerson
    WHERE sT.IDPerson IS NULL

    UNION

    -- Selects people that have currently 0 sticker

    SELECT p.IDPerson, p.NAME
    FROM Person p
    JOIN     (
                SELECT sT.IDPerson, SUM(NbStickers) AS NbStickers
                FROM (

                       SELECT (sT.NbStickersReceived - sT.NbStickersGiven) AS NbStickers
                       FROM StickerTransaction sT

                     UNION ALL

                       SELECT pu.NbStickersBought AS NbStickers
                       FROM Purchase pu
                     ) dt
                GROUP BY sT.IDPerson
                HAVING SUM(NbStickers) = 0
             ) sT
    ON sT.IDPerson = p.IDPerson

) People

答案 2 :(得分:0)

使用NOT EXISTS查找从未购买过的人。

使用另一个子查询来查找与NbStickersGiven具有相同数量的NbStickersReceived的人。

SELECT p.IDPerson, p.Name
FROM Person p
where not exists (select 1 from Purchase
                  where IDPerson = p.IDPerson)
   or 0 = (select sum(NbStickersReceived) - sum(NbStickersGiven)
           from StickerTransaction
           where IDPerson = p.IDPerson)

也许你需要围绕第二个子查询COALESCE来处理没有任何StickerTransactions的人。

答案 3 :(得分:0)

您必须在外部声明中建立链接。

-- Selects people that have currently 0 sticker
SELECT p.IDPerson, p.Name
 FROM Person p,
 (SELECT sT.IDPerson, SUM(NbStickers) sum_stickers
    FROM ((SELECT sT.IDPerson, (sT.NbStickersReceived - sT.NbStickersGiven) AS NbStickers
 FROM StickerTransaction sT)
UNION
(SELECT pu.IDPerson, pu.NbStickersBought AS NbStickers
  FROM PURCHASE pu
  ))
 group by sT.IDPerson
) zeroSticker
where zeroSticker.IDPerson = p.IDPerson and zeroSticker.sum_stickers = 0

因为普通SQL中不可能将参数注入子查询中。

相关问题