2013-12-16 59 views
0
SELECT distinct denomination, 
(SELECT count(com) from security where denomination = 200), 
(SELECT count(com) from security where denomination = 50), 
(SELECT count(com) from security where denomination = 1000), 
(SELECT count(com) from security where denomination = 100) from security; 

denomination | ?column? | ?column? | ?column? | ?column? 
--------------+----------+----------+----------+---------- 
      200 |  1 |  2 |  1 |  2 
      50 |  1 |  2 |  1 |  2 
      100 |  1 |  2 |  1 |  2 
     1000 |  1 |  2 |  1 |  2 

上面的代码打印上面的结果。那不是我想要的。我如何为每个面额值写出com的数量,以便在各个面额附近的单独列中出现?我希望它看起来像这样:需要简单的sql解决方案

denomination | ?column? | 
--------------+----------+ 
      200 |  1 | 
      50 |  2 | 
      100 |  2 | 
     1000 |  1 | 

我确定答案很简单,我只是不能让我的思绪围绕它。

+1

您正在寻找一个支点解决方案。现在大多数RDBMS支持它,但语法不尽相同。你正在使用哪种RDBMS? – Mureinik

+0

不太确定你的意思,但我使用psql。 – user3106585

+0

这意味着您使用的是哪个版本的数据库。例如MS SQL,MySQL,Oracle等 – Fred

回答

2
SELECT denomination, count(com) 
FROM security 
WHERE denomination IN (200, 50, 1000, 100) 
GROUP BY denomination;