我正在编写一些select语句。
因此:
Select JobType, count(JobType) as count from PrintJobs with (nolock)
where JobType in (1, 2, 3, 4)
and convert(datetime, TimeSubmitted) > cast(CAST(dateadd(dd, -1,GETDATE()) as date) as datetime) + CAST('17:30:00.000' as time)
group by JobType
问题在于,当JobTypes没有显示任何内容时,表格如下所示
JobTypes | count |
------------------|
2 1 |
3 2 |
__________________|
有没有办法让它像这样:
JobTypes | count |
------------------|
1 0 |
2 1 |
3 2 |
4 0 |
__________________|
所以,即使作业类型的结果中没有任何内容,它仍会显示出来?我试图使其尽可能动态,其中可以在c#sql参数中设置jobtypes
提前致谢
答案 0 :(得分:1)
一个解决方案是left join
:
Select j.JobType, count(pj.JobType) as count
from (values (1), (2), (3), (4)) j(jobtype) left join
PrintJobs pj
on pj.JobType = j.jobType and
convert(datetime, TimeSubmitted) > cast(CAST(dateadd(day, -1,GETDATE()) as date) as datetime) + CAST('17:30:00.000' as time)
group by j.JobType
答案 1 :(得分:1)
在这样的SQL Server中:
.scaleImage{
width:100%;
}
在这里,您可以创建类型和连接的表。
答案 2 :(得分:0)
Select JTypes.JType, count(P.JobType) as count
from (
SELECT 1 as JType
UNION ALL
SELECT 2 as JType
UNION ALL
SELECT 3 as JType
UNION ALL
SELECT 4 as JType
) AS JTypes
LEFT JOIN PrintJobs AS P ON P.JobType = JTypes.JType
WHERE convert(datetime, P.TimeSubmitted) > cast(CAST(dateadd(dd, -1,GETDATE()) as date) as datetime) + CAST('17:30:00.000' as time)
group by JTypes.JType