我有一个表叫xDays
设置如下图所示:选择列,其中另一列相关的总数为0
╔══════════════╦═════════════════════════╦═══════╗
║ Project_Name ║ Date ║ Hours ║
╠══════════════╬═════════════════════════╬═══════╣
║ proj1 ║ 2010-03-03 00:00:00.000 ║ 0 ║
║ proj1 ║ 2010-03-04 00:00:00.000 ║ 0 ║
║ proj1 ║ 2010-03-05 00:00:00.000 ║ 0 ║
║ proj2 ║ 2010-03-03 00:00:00.000 ║ 1 ║
║ proj2 ║ 2010-03-04 00:00:00.000 ║ 0 ║
║ proj2 ║ 2010-03-05 00:00:00.000 ║ 0 ║
╚══════════════╩═════════════════════════╩═══════╝
我试图做的是选择每个Project_Name
谁是总在Hours
列0
。在上面的例子中,SELECT语句应该返回proj1
,因为它总hours
为0,而对于proj2
总课时为1
我得到的最接近是:
select sum(hours) as 'total', project_name
From xDays
group by project_name
order by project_name
这给了我一个表格显示每个project_name的总小时数,它显示0
某些project_names的总小时数。从这里,我已经尝试了一些不同的东西,要么得到一个
转换转换为varchar值定义了int类型
错误或空结果时失败。我试过的一些例子:
select sum(hours) as 'total', project_name
From xDays
where 'total' = convert(varchar(10), 0)
group by project_name
order by project_name`
这会返回空结果。
select sum(hours) as 'total', project_name
From xDays
where 'total' = 0
group by project_name
order by project_name
这会返回转换错误(无法将varchar转换为int)。
我如何才能正常工作?
如果所有项目都不应该出现在提取中,那么所有项目在'xDays'表中是否都有条目?是否有可能将负小时分配给项目? – JohnLBevan
无负面小时。 – pfinferno
注意:'where'total'= 0'这里你将字符串值'total'(不是列/结果)与0进行比较。 – JohnLBevan