如何过滤分组查询结果集 (SQL)

发布于 2024-10-21 10:34:16 字数 1337 浏览 2 评论 0原文

我做了这样的查询:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱她像谁 2024-10-28 10:34:16

您可以添加条件来告诉“此代码必须包含带有 CT 的行”sa 执行子查询:

SELECT Code FROM transaction WHERE kind='CT' GROUP BY Code ;

并在第一个查询中添加一个过滤器以仅显示在上一个子查询中具有代码的记录:

... AND Code IN (SELECT Code FROM transaction WHERE kind='CT' GROUP BY Code ) ...

这将删除记录代码2,因为第一个查询的结果中不会出现 2

You can add condition that tells "This Code must have a row with CT" sa do a sub-query:

SELECT Code FROM transaction WHERE kind='CT' GROUP BY Code ;

And to your first query add a filter to show only those records which have Code in previous subquery:

... AND Code IN (SELECT Code FROM transaction WHERE kind='CT' GROUP BY Code ) ...

This will get rid of record Code 2, because 2 will no be in results from first query

生活了然无味 2024-10-28 10:34:16

这应该可以做到:

SELECT Code,
       kind,
       SUM(valP) AS value 
FROM transaction 
WHERE tp = 'N' 
  AND date = '20110312' 
  AND code IN (SELECT t2.code
               FROM transaction t2
               WHERE t2.kind = 'CT')
GROUP BY Code,kind 
ORDER BY Code,kind;

This should do it:

SELECT Code,
       kind,
       SUM(valP) AS value 
FROM transaction 
WHERE tp = 'N' 
  AND date = '20110312' 
  AND code IN (SELECT t2.code
               FROM transaction t2
               WHERE t2.kind = 'CT')
GROUP BY Code,kind 
ORDER BY Code,kind;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文