具有条件聚合的一对多查询以及如何从查询中检索不同的结果
One-to-many query with conditional aggregation and how to retrieve distinct results from query
一位成员 向我提供了解决方案,结果如下:
SELECT s.StampId
, ct.Country
,StatusTable.Status
, MAX(case when q.statusId = 1 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as HaveMNH
, MAX(case when q.statusId = 1 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as HaveMH
, MAX(case when q.statusId = 1 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as HaveUsed
, MAX(case when q.statusId = 2 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as WantMNH
, MAX(case when q.statusId = 2 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as WantMH
, MAX(case when q.statusId = 2 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as WantUsed
, MAX(case when q.statusId = 3 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as TradeMNH
, MAX(case when q.statusId = 3 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as TradeMH
, MAX(case when q.statusId = 3 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as TradeUsed
FROM StampTable s
LEFT JOIN StampQuantatiesTable sq ON s.StampId = sq.StampId
LEFT JOIN QuantatiesTable q ON q.QuantatiesId = sq.QuantatiesId
left Join CountryTable ct on S.Country = ct.CountryId
Left Join StatusTable on q.StatusId= StatusTable.StatusId
GROUP BY s.StampId
, ct.Country
,StatusTable.Status
在我的数据库中,邮票可能位于 none 或 3 个 statusid 组的任意组合中。对我来说,这些群体代表“拥有”、“想要”和“交易”。
如果我只在其中一个组中包含一个 WHERE 语句说“有”,我会得到正确的结果,但如果我对两个或更多组执行相同的操作,我会得到重复的结果。
我的问题是如何限制返回的结果,使其不包含重复项。
提前感谢您的帮助。
您对 q.statusid
进行了条件聚合,而 q.statusid
又与 StatusTable.StatusId
相结合,因此对于相同的 s.stampid, ct.Country
,您的 StatusTable.Status
可能会有所不同
您不应在 group by
和 select
子句中使用 StatusTable.Status
(尽管您可以在 select
中的 StatusTable.Status
上使用聚合函数条款)
使用以下查询:
SELECT s.StampId
, ct.Country
-- ,StatusTable.Status -- or max, min, etc aggregate function on StatusTable.Status
, MAX(case when q.statusId = 1 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as HaveMNH
, MAX(case when q.statusId = 1 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as HaveMH
, MAX(case when q.statusId = 1 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as HaveUsed
, MAX(case when q.statusId = 2 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as WantMNH
, MAX(case when q.statusId = 2 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as WantMH
, MAX(case when q.statusId = 2 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as WantUsed
, MAX(case when q.statusId = 3 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as TradeMNH
, MAX(case when q.statusId = 3 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as TradeMH
, MAX(case when q.statusId = 3 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as TradeUsed
FROM StampTable s
LEFT JOIN StampQuantatiesTable sq ON s.StampId = sq.StampId
LEFT JOIN QuantatiesTable q ON q.QuantatiesId = sq.QuantatiesId
left Join CountryTable ct on S.Country = ct.CountryId
Left Join StatusTable on q.StatusId= StatusTable.StatusId
GROUP BY s.StampId
, ct.Country
-- ,StatusTable.Status
一位成员
SELECT s.StampId
, ct.Country
,StatusTable.Status
, MAX(case when q.statusId = 1 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as HaveMNH
, MAX(case when q.statusId = 1 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as HaveMH
, MAX(case when q.statusId = 1 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as HaveUsed
, MAX(case when q.statusId = 2 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as WantMNH
, MAX(case when q.statusId = 2 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as WantMH
, MAX(case when q.statusId = 2 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as WantUsed
, MAX(case when q.statusId = 3 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as TradeMNH
, MAX(case when q.statusId = 3 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as TradeMH
, MAX(case when q.statusId = 3 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as TradeUsed
FROM StampTable s
LEFT JOIN StampQuantatiesTable sq ON s.StampId = sq.StampId
LEFT JOIN QuantatiesTable q ON q.QuantatiesId = sq.QuantatiesId
left Join CountryTable ct on S.Country = ct.CountryId
Left Join StatusTable on q.StatusId= StatusTable.StatusId
GROUP BY s.StampId
, ct.Country
,StatusTable.Status
在我的数据库中,邮票可能位于 none 或 3 个 statusid 组的任意组合中。对我来说,这些群体代表“拥有”、“想要”和“交易”。
如果我只在其中一个组中包含一个 WHERE 语句说“有”,我会得到正确的结果,但如果我对两个或更多组执行相同的操作,我会得到重复的结果。
我的问题是如何限制返回的结果,使其不包含重复项。
提前感谢您的帮助。
您对 q.statusid
进行了条件聚合,而 q.statusid
又与 StatusTable.StatusId
相结合,因此对于相同的 s.stampid, ct.Country
StatusTable.Status
可能会有所不同
您不应在 group by
和 select
子句中使用 StatusTable.Status
(尽管您可以在 select
中的 StatusTable.Status
上使用聚合函数条款)
使用以下查询:
SELECT s.StampId
, ct.Country
-- ,StatusTable.Status -- or max, min, etc aggregate function on StatusTable.Status
, MAX(case when q.statusId = 1 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as HaveMNH
, MAX(case when q.statusId = 1 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as HaveMH
, MAX(case when q.statusId = 1 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as HaveUsed
, MAX(case when q.statusId = 2 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as WantMNH
, MAX(case when q.statusId = 2 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as WantMH
, MAX(case when q.statusId = 2 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as WantUsed
, MAX(case when q.statusId = 3 and q.MintUsedId = 1 then 'True' ELSE 'False' END) as TradeMNH
, MAX(case when q.statusId = 3 and q.MintUsedId = 2 then 'True' ELSE 'False' END) as TradeMH
, MAX(case when q.statusId = 3 and q.MintUsedId = 3 then 'True' ELSE 'False' END) as TradeUsed
FROM StampTable s
LEFT JOIN StampQuantatiesTable sq ON s.StampId = sq.StampId
LEFT JOIN QuantatiesTable q ON q.QuantatiesId = sq.QuantatiesId
left Join CountryTable ct on S.Country = ct.CountryId
Left Join StatusTable on q.StatusId= StatusTable.StatusId
GROUP BY s.StampId
, ct.Country
-- ,StatusTable.Status