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.
Author |
Topic |
jutiyi
Starting Member
9 Posts |
Posted - 2013-08-20 : 21:57:57
|
I have three table Table AID NAME1 NameATable BID Coutry1 County A1 Counry BTable CID Family1 Family A1 Family B1 Family CI use a outer joinI get the following result set1 County A Family A1 County A Family B1 County A Family C1 County B Family A1 County B Family B1 County B Family CBut I want to get the following1 County A Family A1 County B Family B1 NULL Family CWhich query should I use?Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-08-21 : 00:29:32
|
you should use something likeSELECT a.ID,b.County,c.FamilyFROM TableA aLEFT JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq FROM TableB )bON a.ID = b.IDLEFT JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq FROM TableC )cON c.ID = b.IDAND c.Seq = b.Seq ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
lazerath
Constraint Violating Yak Guru
343 Posts |
Posted - 2013-08-21 : 14:37:24
|
The solution visakh16 provided is close but needs one adjustment -- the "ORDER BY ID" in the OVER clauses should be changed to "ORDER BY Country" and "ORDER BY Family" so that the two datasets are guaranteed to lineup alphabetically. Otherwise, SQL doesn't guarantee they will even if they are physically ordered alphabetically in the tables.That said, although this will get you the result you want, it will not be reliable if the data is anything other than EXACTLY what you provided. In fact, based on the schema, there is no reliable way to do so unless your business rules say that these items should always lineup 1-to-1 alphabetically. Even then, what if one table skips an entry -- say Country goes A, C, D and Family goes A, B, C? Do you want Country C to lineup to Family B? If not, short of string parsing, which is in itself dependent on a specific data format, there is no good way to line up A to A, B to B, C to C and so on. This is not how you would want to design and build an IT solution as you would want to model your primary keys and foreign keys before constructing queries. Is TableB even related to TableA or is it just related to TableC? In the case of TableB & TableC, either or both tables should also have foreign key relationships that make the relationships to other entities explicit. Even though you asked for a SQL statement, it is useless unless you have the correct data model to begin with. This should get you started: http://www.databaseanswers.org/tutorial4_data_modelling/index.htm |
|
|
ShivaKrishna
Starting Member
20 Posts |
Posted - 2013-08-28 : 11:14:53
|
SELECT a.ID,b.County,c.FamilyFROM TableA aInner JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq FROM TableB )bON a.ID = b.IDRight JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Seq FROM TableC )cON c.ID = b.IDAND c.Seq = b.Seq |
|
|
VeeranjaneyuluAnnapureddy
Posting Yak Master
169 Posts |
Posted - 2013-08-29 : 04:30:24
|
This Query give what u expected,SELECT c.ID,b.country,c.FamilyFROM TableA aInner JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS SeqFROM TableB)bON a.ID = b.IDright JOIN (SELECT *,ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS SeqFROM TableC)cON c.ID = b.IDAND c.Seq = b.Seqveeranjaneyulu |
|
|
|
|
|
|
|