SQL - 每列的where条件
这是一个一般的 SQL 问题,但我如果你必须知道数据库是 Firebird。
我有这个表(简化的):
user amount type -------------------- john 25 credit john 20 debit john 5 debit john 15 credit mike 15 credit
我想要使用单个查询得到一个看起来像这样的结果:
user credit debit -------------------- john 40 25 mike 15 NULL
Where credit = SUM(amount) WHERE type=credit
and debit = SUM(amount) WHERE type=debit
基本上我想在结果上有多个字段基于一个字段的总和(在本例中为金额)但在不同的条件(在本例中为类型)。
预先感谢您的任何建议。
This is a general SQL question, but I if you must know the database is Firebird.
I have this table (simplified):
user amount type -------------------- john 25 credit john 20 debit john 5 debit john 15 credit mike 15 credit
I want to have a result that looks like this, using a single query:
user credit debit -------------------- john 40 25 mike 15 NULL
Where credit = SUM(amount) WHERE type=credit
and debit = SUM(amount) WHERE type=debit
Basically I want to have multiple fields on the result based on a SUM of a field (in this case amount) but on different conditions (in this case type).
Thanks in advance for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准 SQL 是:
Standard SQL would be:
我不确定 Firebird 是如何做到的,但是你能做类似的事情吗:
我正在引用我认为在 PostgreSQL 中的内容,以防帮助你找到你需要的东西。
I'm not sure how Firebird does it, but can you do something like:
I'm referencing what I think it would be in PostgreSQL, in case that helps you find what you need.
试试这个:
try this :