Using SQL Server 2005
Table1
ID FromDate ToDate
001 23-02-2009 25-02-2009
001 27-02-2009 29-02-2009
002 12-02-2009, 25-03-2009
...,
Table2
ID Name Total
001 Raja 30
002 Ravi 22
I want to get total day for the personid
Tried Query,
SELECT
table2.Id, table2.name, table2.total,
datediff(day, table1.fromdate, table2.todate)
FROM table1
LEFT OUTER JOIN table2 ON table1.personid = table2.personid
Getting output
ID Name Total Days
001 Raja 30 3
001 Raja 30 3
...,
It should total the days and it should display in one line,
Note: Suppose I am selecting the particular period date means it should display that days only
For example
where date between 26-02-2009 to 03-03-2009, It should display
ID Name Total Days
001 Raja 30 3
...,
Because am taking date after 25-02-2009,
Expected Output
ID Name Total Days
001 Raja 30 6
002 Ravi 22 16
How to modify my query?