显示列中的所有行,即使计数值为NULL

时间:2015-07-16 08:08:09

标签: sql-server database count

我有这样的事情:

CODE,ORG,TYPE,DATE是表格列。

SELECT     COUNT(CODE) AS OK, ORG AS ORG
FROM       EVENTS 
WHERE      (TYPE = 'A') and (DATE BETWEEN '2015-07-06' AND '2015-07-12')
GROUP BY   ORG

结果是:

OK    ORG
---------
93     1
292    2
51     3

主要问题是,总共有6个Orgs,但它们没有列出,因为COUNT没有可计算的行数,因为本周它们无法工作。

我需要始终列出ORG,如果有效COUNT值,则显示它,否则显示0.这样的事情:

OK    ORG
---------
93     1
292    2
51     3
0      4
0      5
0      6

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

请你试试这个:

JLabel tabLabel = new JLabel("Tab", JLabel.LEFT);
//add new label at set location
jTabbedPane.setTabComponentAt(0, tabLabel);

答案 1 :(得分:0)

您需要先从ORG获取所有EVENTS,然后执行LEFT JOIN

SELECT
    ISNULL(COUNT(e.CODE), 0) AS OK,
    t.ORG AS ORG
FROM(
    SELECT DISTINCT ORG FROM EVENTS
)t
LEFT JOIN EVENTS e
    ON e.ORG = t.ORG
    AND e.TYPE = 'A'
    AND e.DATE BETWEEN '2015-07-06' AND '2015-07-12'
GROUP BY t.ORG