DB2 顶级分组
我已经尝试了几个小时,但无法让查询使用 DB2 执行我想要的操作。 从表“公司和用户”中,我有以下每个公司/用户的门票数量信息,
user company quantity
------------ ------------ ------------
mark nissan 300
tom toyota 50
steve krysler 80
mark ford 20
tom toyota 120
jose toyota 230
tom nissan 145
steve toyota 10
jose krysler 35
steve ford 100
这是由查询生成的:
SELECT T.USER, COUNT(T.USER) AS QUANTITY, T.COMPANY FROM TICKET T
INNER JOIN COMPANY P ON P.COMPANY = T.COMPANY
GROUP BY (T.USER, T.COMPANY) -- ORDER BY QUANTITY DESC
我想看到的是每个公司的顶级用户,因此鉴于上面的数据,查询应该向我显示:
user company quantity (Top user per company)
------------ ------------ --------------------------------
mark nissan 300
jose toyota 230
steve ford 100
steve krysler 80
如何我写SQL来返回这个结果?
最终答案(在评论中注明):
SELECT user, quantity, company
FROM (SELECT user, quantity, company,
RANK () OVER (PARTITION BY company ORDER BY quantity DESC) AS r
FROM (SELECT T.USER, COUNT(T.USER) AS QUANTITY, T.COMPANY
FROM TICKET T JOIN COMPANY P ON P.COMPANY = T.COMPANY
GROUP BY (T.USER, T.COMPANY) ) s ) t
WHERE r = 1;
I've been trying for hours but can't get the query to do what I want using DB2.
From table Company and Users I have the following tickets quantity info per company/user
user company quantity
------------ ------------ ------------
mark nissan 300
tom toyota 50
steve krysler 80
mark ford 20
tom toyota 120
jose toyota 230
tom nissan 145
steve toyota 10
jose krysler 35
steve ford 100
This is generated by the query:
SELECT T.USER, COUNT(T.USER) AS QUANTITY, T.COMPANY FROM TICKET T
INNER JOIN COMPANY P ON P.COMPANY = T.COMPANY
GROUP BY (T.USER, T.COMPANY) -- ORDER BY QUANTITY DESC
What I want to see is the top user for each company, so given the data above, the query should show me:
user company quantity (Top user per company)
------------ ------------ --------------------------------
mark nissan 300
jose toyota 230
steve ford 100
steve krysler 80
How can I write the SQL to return this result?
Final answer (noted in a comment):
SELECT user, quantity, company
FROM (SELECT user, quantity, company,
RANK () OVER (PARTITION BY company ORDER BY quantity DESC) AS r
FROM (SELECT T.USER, COUNT(T.USER) AS QUANTITY, T.COMPANY
FROM TICKET T JOIN COMPANY P ON P.COMPANY = T.COMPANY
GROUP BY (T.USER, T.COMPANY) ) s ) t
WHERE r = 1;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一步步把它建立起来。
查找每个公司的最大数量,假设问题中显示的第一个数据表称为“门票”:
现在,查找该公司具有该最大数量的用户的数据:
如果特定公司的最大数量比如说,200,并且两个用户对该公司的得分都为 200,那么此查询会列出这两个用户。
现在,如果您的意思是问题中显示的查询生成第一个结果表,那么我上面所说的门票需要是派生表:
在这种情况下,我们可以使用WITH子句(语法未检查,但我认为根据 SQL 标准,它是正确的):
显然,如果您愿意,您也可以将 WITH 子查询写出两次。
Build it up step by step.
Find the maximum quantity for each company, assuming the first data table shown in the question is called 'Tickets':
Now, find the data for the user(s) with that maximum quantity for that company:
If the top quantity for a particular company was, say, 200 and two users both scored 200 for that company, then this query lists both users.
Now, if you mean that the query you show in the question generates the first result table, then what I called tickets just above needs to be the derived table:
In which case, we can use a WITH clause (syntax unchecked, but I think it is correct per SQL standard):
Clearly, you can also write the WITH sub-query out twice if you prefer.
这应该有效。创建派生视图来计算每个用户和每个公司的数量。然后获取数量的最大值,然后将最大值加入到数量的计算中。
可以在此处找到按 StackOverflow 数据标签显示排名靠前的用户的工作示例。
This should work. Create a derived view to calculate the Quantity per user and per company. Then get the max of then Quantity and then join the max back to the the calculation of the quantity.
A working sample that shows the top users by tag for the StackOverflow data can be found here.