2014-11-04 123 views
1

我有PROJECT和allocation_time(整数)FROM TIMESHEET的相同模式名称和两列的allocation_days(整数)两个表PROJECT和TIMESHEET。我想要做的就是加入这两列并作简短的验证是这样的:加入并比较来自两个表的数据

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 

,但我不知道的条件下才能得到插入如果是< 24或休息。 谢谢。

+1

这样一个查询,它是SQL Server或MySQL的? – Lamak 2014-11-04 18:32:50

+0

SQL Server我忘了指定.. – Alienware 2014-11-04 18:36:00

+0

只是删除'MySQL'标签 – Fred 2014-11-04 18:36:39

回答

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