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
 Selecting records from multiple databases

Author  Topic 

mats.raemen
Starting Member

2 Posts

Posted - 2011-11-01 : 14:21:59
Hi guys,

I have to get data regarding events from 2 tables in 1 query.

The information I need from the events is in different tables, but I need them to be displayed in 1 table and the columns need to be merged.

I'll illustrate it with an example:

from both tables I need the EventName, StartTime and EndTime

so I use

SELECT
table1.EventName AS EventName,
table1.StartTime AS StartTime,
table1.EndTime AS EndTime,
table2.EventName AS EventName,
table2.StartTime AS StartTime,
table2.EndTime AS EndTime,

FROM
...

and I want the EventName, StartTime and Endtime from table 2 displayed in the same column as those from table 1 so the generated output looks like:
Event1FromTable1 11/1/2011 11/2/2011
Event2FromTable1 11/3/2011 11/4/2011
Event1FromTable2 11/1/2011 11/2/2011
Event2FromTable2 11/3/2011 11/5/2011

Is this possible?

Kind regards,

Mats

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-11-01 : 14:23:54
select
table1.EventName AS EventName,
table1.StartTime AS StartTime,
table1.EndTime AS EndTime,
from Table1
union all
select
table2.EventName AS EventName,
table2.StartTime AS StartTime,
table2.EndTime AS EndTime,
from Table2
order by ...
==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-01 : 14:41:14
[code]

select
table1.EventName AS EventName,
table1.StartTime AS StartTime,
table1.EndTime AS EndTime,1 as ord
from Table1
union all

select
table2.EventName AS EventName,
table2.StartTime AS StartTime,
table2.EndTime AS EndTime,2 as ord
from Table2
order by ord, EventName,...
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mats.raemen
Starting Member

2 Posts

Posted - 2011-11-05 : 04:24:09
thank you very very much!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-05 : 13:03:06
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -