具有条件聚合的一对多查询以及如何从查询中检索不同的结果
Posted
技术标签:
【中文标题】具有条件聚合的一对多查询以及如何从查询中检索不同的结果【英文标题】:One-to-many query with conditional aggregation and how to retrieve distinct results from query 【发布时间】:2020-12-13 14:56:55 【问题描述】:一位成员 see original question 向我提供了一个解决方案,结果如下:
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
在我的数据库中,邮票可能不属于任何一个或三个 statusid 组的任意组合。对我来说,这些组代表“拥有”、“想要”和“交易”。
如果我只在其中一个组上包含 WHERE 语句说“有”,我会得到正确的结果,但如果我对两个或更多组做同样的事情,我会得到重复的结果。
我的问题是如何限制返回的结果,使其不包含重复项。
提前感谢您的帮助。
【问题讨论】:
【参考方案1】:您对 q.statusid
进行了条件聚合,而 StatusTable.StatusId
又与 StatusTable.StatusId
连接,因此您的 StatusTable.Status
可以与相同的 s.stampid, ct.Country
不同
您不应在group by
和select
子句中使用StatusTable.Status
(尽管您可以在StatusTable.Status
子句中的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
【讨论】:
以上是关于具有条件聚合的一对多查询以及如何从查询中检索不同的结果的主要内容,如果未能解决你的问题,请参考以下文章
在 Mongoose 中,我有具有一对多关系的用户和角色模式。如何查询特定用户是不是具有“管理员”角色?