Reputation: 857
I have tow tables with same composite key. Following is the table
table T1
No | Date | S_hour
----+---------------+-------
1 | 2012-01-01 | 8
1 | 2012-01-02 | 8
1 | 2012-01-03 | 8
2 | 2012-01-01 | 8
2 | 2012-01-02 | 8
2 | 2012-01-03 | 8
table T2
No | Date | L_hour
----+---------------+-------
1 | 2012-01-01 | 8
1 | 2012-01-02 | 3
1 | 2012-01-03 | 8
2 | 2012-01-01 | 2
2 | 2012-01-02 | 8
2 | 2012-01-03 | 8
S_hour is scheduled hour and L_hour is leave hour.
In my Query out put I want S_hour - L_hour.Following should be query output Query output
No | Date | S_hour - L_hour
----+---------------+-------
1 | 2012-01-01 | 0
1 | 2012-01-02 | 5
1 | 2012-01-03 | 0
2 | 2012-01-01 | 6
2 | 2012-01-02 | 0
2 | 2012-01-03 | 0
Thanks in advance
Upvotes: 1
Views: 75
Reputation: 2827
SQL Query:
SELECT t1.no, t1.date1, t1.s_hour - t2.l_hour FROM t1, t2
WHERE t1.no=t2.no AND t1.date1=t2.date1;
Fiddle: Demo
Upvotes: 0
Reputation: 87
something like this
SELECT T1.No, T1.Date, s_hour - L_hour AS result
FROM T1 INNER JOIN
T2 ON t1.No = t2.No AND
T1.Date = T2.Date
Upvotes: 0
Reputation: 238226
select t1.No
, t1.Date
, t1.S_hour - t2.L_hour
from Table1 t1
join Table2 t2
on t1.No = t2.No
and t1.Date = t2.Date
Upvotes: 2