我尝试将数据列表填充到折线图中。我的X轴将是我的StartTime
,我的Y轴将是Total
想要询问是否有可能查询一系列数据,如果数据不在数据库中并将其返回为null而不是不显示,请参考下面的示例:
|StartTime |Qty |
-----------------------
|10 |1 |
|11 |3 |
|12 |2 |
|13 |1 |
|11 |2 |
我的预期结果是什么:在9到12之内作为StartTime
的条款
|StartTime |TOTAL |
-----------------------
|9 |NULL |
|10 |1 |
|11 |5 |
|12 |2 |
有人能告诉我以及查询的例子吗?因为我根本不知道。
答案 0 :(得分:1)
您可以使用另一个要在图表上显示的Starttime表。左连接第一个表,然后从新表开始执行组。将计数用于您的目的。
TableTimes:
| StartTime |
--------------
| 9 |
| 10 |
| 11 |
| 12 |
| 13 |
| 14 |
Select Sum(Qty) From TableTimes TT
Left Join FirstTable FT on TT.StartTime=FT.StartTime
Where TT.StartTime Between 9 and 12
Group by TT.StartTime
答案 1 :(得分:1)
您可以使用子查询或CTE来创建一个全新的表:
select h.Hour, Sum(i.Qty) as Qty
from ItemsPerHour i
right outer join (
select 1 as Hour union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 12 union all
select 13 union all
select 14 union all
select 15 union all
select 16 union all
select 17 union all
select 18 union all
select 19 union all
select 20 union all
select 21 union all
select 24
) h
on h.Hour = i.StartTime
order by h.Hour;
将WITH
用于hours
CTE:
with hours as
(
select 1 as Hour union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9 union all
select 10 union all
select 11 union all
select 12 union all
select 13 union all
select 14 union all
select 15 union all
select 16 union all
select 17 union all
select 18 union all
select 19 union all
select 20 union all
select 21 union all
select 24
)
select h.Hour, Sum(i.Qty) as Qty
from ItemsPerHour i
right outer join hours h
on h.Hour = i.StartTime
order by h.Hour;