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 |
|
MiffySnow
Starting Member
2 Posts |
Posted - 2012-05-22 : 17:56:52
|
| HiI am very new to SQL.I have a table of data containing everything I need, but several columns have IDs, these IDs are all defined in different tables. I think I have worked out a way to return the data from the table with the IDs replaced by their equivalent names, but I'm not sure it is completely accurate as there is an increase from 7073 to 7076 rows. I am using Microsoft SQL Server 2005.tblA ClientID | FoodID | ColourID | DrinkID=======================2|1|3|43|1|2|45|2|2|35|2|4|2tblFoodFood ID | Food=========1|Apples2|Pears3|BananastblColourColourID | Colour===========1|Red2|Blue3|White4|GreentblDrinkDrinkID|Drink=========1|Milk2|Water3|Beer4|TeaI am using a similar query to this:SELECT a.[ClientID],b.[Food],c.[Colour],d[Drink]FROM a [tblA]LEFT JOIN b [tblFood] ON a.[FoodID]=b.[FoodID]LEFT JOIN c [tblColour] ON a.[ColourID]=c.[ColourID]LEFT JOIN d [tblDrink] ON a.[DrinkID]=d.[DrinkID]I hope this is the result (it appears to be, apart from the additional 3 rows).ClientID | FoodID | ColourID | DrinkID=======================2|Apples|White|Tea3|Apples|Blue|Tea5|Pears|Blue|Beer5|Pears|Green|WaterI'd really appreciate any advice on how I could improve this query and apologise if I have put too much in with the example.Thank you in advance. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
vinu.vijayan
Posting Yak Master
227 Posts |
Posted - 2012-05-23 : 03:12:31
|
quote: Originally posted by tkizer Your query looks fine. You have more rows because some of the IDs map to multiple rows.The only comment I have is make sure that LEFT JOIN is what you want. You may only need JOIN, which is shorthand for INNER JOIN. LEFT JOIN is shorthand for LEFT OUTER JOIN. Make sure you understand the difference between INNER and OUTER joins.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
+1. The Left JOIN might be the culprit.It might be bringing more data than you are expecting from one of the tables.N 28° 33' 11.93148"E 77° 14' 33.66384" |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2012-05-23 : 03:40:02
|
LEFT JOIN is unlikely the issue here. The additional 3 rows could be due to you have multiple same ID in the tblFood or tblColour or tblDrink table as Tara has mentionYou can verify this byselect FoodIDfrom tblFoodgroup by FoodIDhaving count(*) > 1 KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
MiffySnow
Starting Member
2 Posts |
Posted - 2012-05-23 : 04:42:43
|
| Thank you so much for all your help. I hadn't realised that LEFT JOIN stood for LEFT OUTER JOIN, I think this is what I need in this case but is certainly something to think about in other queries, which have been going squiffy!KH's query was really helpful and I was able to locate some multiple IDs, leading to the additional rows. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-05-23 : 15:56:46
|
| [code]SELECT ClientID,IDVal,CatValINTO #TempFROM tblA tUNPIVOT(IDVal FOR CatVal IN (FoodID,ColourID,DrinkID))uSELECT ClientID,MAX(CASE WHEN CatVal = 'FoodID' THEN Val END) AS FoodID,MAX(CASE WHEN CatVal = 'ColourID' THEN Val END) AS ColourID,MAX(CASE WHEN CatVal = 'DrinkID' THEN Val END) AS DrinkIDFROM(SELECT t.ClientID,t.CatVal,f.Food AS ValFROM #Temp tINNER JOIN tblFood fON f.FoodID = t.IDValWHERE t.CatVal='FoodID'UNION ALLSELECT t.ClientID,t.CatVal,c.ColorFROM #Temp tINNER JOIN tblFood cON c.ColourID = t.IDValWHERE t.CatVal='ColourID'UNION ALLSELECT t.ClientID,t.CatVal,d.DrinkFROM #Temp tINNER JOIN tblDrink dON d.DrinkID = t.IDValWHERE t.CatVal='DrinkID')tDROP TABLE #Temp[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|