显示0表示没有数据的行

时间:2016-10-13 18:33:52

标签: mysql sql

下面的查询显示5个仪表板的记录。查询工作正常。但它仅显示存在数据的那些仪表板。我想要做的是,它应该显示所有Dashboards和数据不存在的列,它应该显示0或NULL。

    Select 
b.[path],
count(*) as "No of Calls",
a.TimeDataRetrieval as "DB Retrieval time",
a.TimeProcessing as "Processing time",
a.TimeRendering as "Rendering Time"

FROM LogStorage a inner join Catalog b on a.[ReportID] = b.[ItemID]

where b.[path] IN ('Dashboard1','Dashboard2','Dashboard3','Dashboard4','Dashboard5')
group by b.[path]

感谢。

1 个答案:

答案 0 :(得分:1)

关键部分是:

FROM LogStorage a inner join Catalog b on a.[ReportID] = b.[ItemID]
where b.[path] IN ('Dashboard1','Dashboard2','Dashboard3','Dashboard4','Dashboard5')

这会将结果集限制为仅存在于LogStorage和Catalog中的记录,而不管连接类型如何。

你想翻转它并使用左连接:

SELECT b.[path]
, SUM (CASE WHEN a.some_column IS NULL THEN 0 ELSE 1 END) as "No of Calls"
, a.TimeDataRetrieval as "DB Retrieval time"
, a.TimeProcessing as "Processing time"
, a.TimeRendering as "Rendering Time"
FROM Catalog b
LEFT JOIN LogStorage a on b.[ItemID] = a.[ReportID]
WHERE b.[path] IN ('Dashboard1','Dashboard2','Dashboard3','Dashboard4','Dashboard5')
group by b.[path]
, a.TimeDataRetrieval
, a.TimeProcessing
, a.TimeRendering

另外,您在哪个表中计算记录?这有点模棱两可,但如果我不得不猜测我会说LogStorage。在这种情况下,您可能希望聚合其他LogStorage列,因为没有聚合,输出将只是行,并且每行的计数将为1。

编辑:我忘了提及,COUNT可能需要是一个SUM CASE表达式,因为使用这样的聚合消除了空值。