2014-11-04 86 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