如何过滤分组查询结果集 (SQL)
我做了这样的查询:
SELECT Code, kind, SUM(valP) AS value FROM transaction WHERE tp = 'N' and date = '20110312' GROUP BY Code,kind ORDER BY Code,kind;
在一个数据库表上,给我这个结果集:
+------+------+------+
| Code | kind| value|
+------+------+------+
| 1 | TR | 25,99|
| 1 | CT | 22,17|
| 2 | TR | 14,23|
| 3 | DD | 09,67|
| 3 | DD | 23,87|
| 3 | CT | 34,87|
+------+------+------+
代码代表交易代码,种类代表付款类型,价值代表付款的价值。
我只想在结果集中包含包含 kind type = 'CT' 的分组结果,但我还想在同一交易中包含其他付款类型,例如,我尝试在上面的查询中添加having :
SELECT Code, kind, SUM(valP) AS value FROM transaction WHERE tp = 'N' and date = '20110312' GROUP BY Code,kind HAVING kind = 'CT' ORDER BY Code,kind;
但我得到这样的东西:
+------+------+------+
| Code | kind| value|
+------+------+------+
| 1 | CT | 22,17|
| 3 | CT | 34,87|
+------+------+------+
相反,我想检索这样的东西:
+------+------+------+
| Code | kind| value|
+------+------+------+
| 1 | TR | 25,99|
| 1 | CT | 22,17|
| 3 | DD | 09,67|
| 3 | DD | 23,87|
| 3 | CT | 34,87|
+------+------+------+
类型为 CT 的记录中包含的所有交易行,有没有办法获得这个?
我该怎么办?
我实际上正在开发 SQL Server 2005,但我可能需要在 PostgreSQL 中运行类似的查询。
谢谢
I've done a query like this :
SELECT Code, kind, SUM(valP) AS value FROM transaction WHERE tp = 'N' and date = '20110312' GROUP BY Code,kind ORDER BY Code,kind;
on a database table that give me this resultset :
+------+------+------+
| Code | kind| value|
+------+------+------+
| 1 | TR | 25,99|
| 1 | CT | 22,17|
| 2 | TR | 14,23|
| 3 | DD | 09,67|
| 3 | DD | 23,87|
| 3 | CT | 34,87|
+------+------+------+
The code represent a transaction code, kind represent payments type, and value represent the value of the payments.
I want to inclue in the result set only the grouped result that contain a kind type = 'CT' but i want to include also the other payments kind in the same transaction, for example i've tried by adding an having to the upper query:
SELECT Code, kind, SUM(valP) AS value FROM transaction WHERE tp = 'N' and date = '20110312' GROUP BY Code,kind HAVING kind = 'CT' ORDER BY Code,kind;
but i obtain something like this :
+------+------+------+
| Code | kind| value|
+------+------+------+
| 1 | CT | 22,17|
| 3 | CT | 34,87|
+------+------+------+
instead i want to retrieve something like this :
+------+------+------+
| Code | kind| value|
+------+------+------+
| 1 | TR | 25,99|
| 1 | CT | 22,17|
| 3 | DD | 09,67|
| 3 | DD | 23,87|
| 3 | CT | 34,87|
+------+------+------+
All lines of the transaction that have within a record with type CT , is there a way to obtain this ?
How can i do ?
I'm actually working on a SQL Server 2005 but i may need run a similar query in PostgreSQL.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以添加条件来告诉“此代码必须包含带有 CT 的行”sa 执行子查询:
并在第一个查询中添加一个过滤器以仅显示在上一个子查询中具有代码的记录:
这将删除记录代码2,因为第一个查询的结果中不会出现 2
You can add condition that tells "This Code must have a row with CT" sa do a sub-query:
And to your first query add a filter to show only those records which have Code in previous subquery:
This will get rid of record Code 2, because 2 will no be in results from first query
这应该可以做到:
This should do it: