加入并比较两个表中的数据

时间:2014-11-04 18:31:38

标签: sql sql-server sql-server-2008 tsql

我有两个表PROJECT和TIMESHEET在相同的模式名称和两个列allocation_days(整数)来自PROJECT和allocation_time(整数)FROM TIMESHEET.All我想做的是加入这两列并做一个简短的验证像这样:

SELECT Project.ID, Project.allocation_days, Timesheet.allocation_time
FROM Project

INNER JOIN Timesheet
ON Project.ID=Timesheet.ID;

DECLARE @hours int

SET @hours = SELECT SUM(allocation_time) from PROJECT

IF (@hours /24) < allocation_days 

    --insert something

 ELSE 

    BREAK

ENDIF

但是如果&lt; 24或者中断,我不知道插入条件的顺序。 感谢。

1 个答案:

答案 0 :(得分:1)

您可以在单个查询中执行此操作,例如

select *,
case when SUM(allocation_time) / 24 < allocation_days then 'Something' 
else 'some_other_thing' end as computed_column
from
(
SELECT Project.ID, Project.allocation_days, Timesheet.allocation_time
FROM Project
INNER JOIN Timesheet
ON Project.ID=Timesheet.ID;
) tab
GROUP BY ID, allocation_days, allocation_time