Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 almost beginner, please help :-)

Author  Topic 

bellocarico
Starting Member

2 Posts

Posted - 2009-12-18 : 04:38:50
Hello, I'm running 3 similar queries towards 3 different tables on the same database.


Select MIN(Started) as Started, MAX(Ended) as Ended from tbl_DB
Where btj_id = 9
And started > '01/06/09'
GROUP BY Started
ORDER BY Started asc

Select MIN(Started) as Started, MAX(Ended) as Ended from tbl_DB
Where btj_id = 15
And started > '01/06/09'
GROUP BY Started
ORDER BY Started asc

Select MIN(Started) as Started, MAX(Ended) as Ended from tbl_DB
Where btj_id = 39
And started > '01/06/09'
GROUP BY Started
ORDER BY Started asc


The result is very simiar in format: Started time on the left and ended time on the right.


Started Ended
2009-06-01 17:06:58.000 2009-06-01 17:14:48.000
2009-06-01 17:07:03.000 2009-06-01 17:14:46.000
2009-06-01 17:07:11.000 2009-06-01 17:14:46.000
...

2009-06-01 17:01:12.000 2009-06-01 17:03:19.000
2009-06-02 17:01:12.000 2009-06-02 17:03:00.000
2009-06-03 17:00:45.000 2009-06-03 17:02:30.000
...

2009-06-01 17:52:43.000 2009-06-01 17:52:46.000
2009-06-02 18:06:16.000 2009-06-02 18:06:18.000
2009-06-03 18:53:04.000 2009-06-03 18:53:06.000
...


How can I have a single query returning a single table with the earliest starting time and the latest ending time each day between the 3 tables?[code]

Any help would be much appreciated!
Thanks

bellocarico
Starting Member

2 Posts

Posted - 2009-12-18 : 04:55:45
That was simple, I should have slept more last night ;-)

Select MIN(Started) as Started1, MAX(Ended) as Ended from tbl_DB
Where (btj_id = 9 OR btj_id = 15 OR btj_id = 39)
and Started > '01/06/09'
GROUP BY Started
ORDER BY Started asc
Go to Top of Page

senthil_nagore
Master Smack Fu Yak Hacker

1007 Posts

Posted - 2009-12-18 : 05:01:46
or

Select MIN(Started) as Started1, MAX(Ended) as Ended from tbl_DB
Where btj_id in(9,15,39)
and Started > '01/06/09'
GROUP BY Started
ORDER BY Started asc

Senthil.C
------------------------------------------------------
[Microsoft][ODBC SQL Server Driver]Operation canceled

http://senthilnagore.blogspot.com/
Go to Top of Page
   

- Advertisement -