all groups > sql server programming > february 2005 >
You're in the

sql server programming

group:

Data from three Tables (Joins)


Data from three Tables (Joins) agarwalp NO[at]SPAM eeism.com
2/2/2005 11:52:26 PM
sql server programming:
Thanks for your responses.

Well the time can be same and cannot be same
For ex:

Table1
(timestamp,readingA)
(2/2/2005 12:09:26,123)
(2/2/2005 12:19:26,324)

Table2
(timestamp,readingB)
(2/2/2005 12:09:26,321)
(2/2/2005 12:29:26,657)

Table3
(timestamp,readingC)
(2/2/2005 12:09:26,231)
(2/2/2005 12:39:26,987)

Result Desired
(timestamp,readingA,readingB,readingC)
2/2/2005 12:09:26,123,321,231
2/2/2005 12:19:26,324,,
2/2/2005 12:29:26,,657,
2/2/2005 12:39:26,,,987

AS you can see the timestamp which is common in all tables is
displayed once alongwith its readings, but the one which is not common
is displayed alongwith its readings while other readings are blank.

Re: Data from three Tables (Joins) oj
2/3/2005 12:06:22 AM
This is a typical xtab problem. Here is a trick.

select timestamp,max(case when tb=1 then reading else 0 end) readingA,
max(case when tb=2 then reading else 0 end) readingB,
max(case when tb=3 then reading else 0 end) readingC
from(
select *,1
from tb1
union all select *,2
from tb2
union all select *,3
from tb3) derived(timestamp,reading,tb)
group by timestamp

Do check out these:
http://support.microsoft.com/kb/175574
or
http://www.sqlteam.com/item.asp?ItemID=2955
or
http://www.rac4sql.net/


--
-oj


[quoted text, click to view]

Re: Data from three Tables (Joins) Hugo Kornelis
2/3/2005 10:35:06 AM
[quoted text, click to view]

Hi Pradeep,

Try if this accomplishes what you want:

SELECT COALESCE (A."timestamp", B."timestamp", C."timestamp"),
A.readingA, B.readingB, C.readingC
FROM Table1 AS A
FULL OUTER JOIN Table2 AS B
ON B."timestamp" = A."timestamp"
FULL OUTER JOIN Table3 AS C
ON C."timestamp" = COALESCE (A."timestamp", B."timestamp")
(untested)

By the way, I suggest you use another name for the timestamp column, as
timestamp is a reserved word in SQL Server.

Best, Hugo
--

AddThis Social Bookmark Button