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 |
|
cjjubb
Starting Member
5 Posts |
Posted - 2008-06-20 : 11:30:42
|
| Hi all.I am currently trying to achieve the followingI have the following two tablesTABLE 1 - USERSIdent | Name--------------------34 | Dave35 | Adam36 | Lucy37 | TomTABLE 2 - PROFILE_DATAPDID | Owner | Name | Value----------------------------------------------- 660 | 34 | AreaType | Rural661 | 34 | Country | United Kingdom662 | 35 | AreaType | Rural663 | 35 | Country | United States664 | 36 | AreaType | Rural665 | 36 | Country | United Kingdom666 | 37 | AreaType | City667 | 37 | Country | United KingdomI basically want to SELECT all the users from the database who are based in a 'Rural' location and live in the 'United Kingdom'.In this case this would be 'Dave' and 'Lucy'.I am aware I must create a JOIN between USERS.IDENT and PROFILE_DATA.IDENT and then execute two 'WHERE' clauses.However I am unsure how to go about doing this.Any help would be grately appreciated. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 11:38:38
|
| [code]SELECT u.NameFROM USERS uINNER JOIN (SELECT Owner, MAX(CASE WHEN Name='AreaType' THEN Value ELSE NULL END) AS AreaType, MAX(CASE WHEN Name='Country' THEN Value ELSE NULL END) AS Country FROM PROFILE_DATA GROUP BY Owner) oON o.Owner=u.IdentWHERE o.Areatype='Rural'AND o.Country='United Kingdom'[/code] |
 |
|
|
Van
Constraint Violating Yak Guru
462 Posts |
Posted - 2008-06-20 : 11:47:22
|
| Homework... |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-20 : 11:51:26
|
| Yup, Visakh's query might get him in trouble with his prof if he has to explain it, better use this one!SELECT a.Ident,a.[Name]FROM users aINNER JOIN profile_data bON a.ident = b.OwnerINNER JOIN profile_data cON b.owner = c.OwnerWHERE b.[value] = 'United Kingdom' and c.[value] = 'Rural' |
 |
|
|
cjjubb
Starting Member
5 Posts |
Posted - 2008-06-20 : 12:49:00
|
| Many Thanks jimf, that query appears to work.However, I now have a follow up question.I have added some numeric data to the PROFILE_DATA table as followsTABLE 1 - USERSIdent | Name--------------------34 | Dave35 | Adam36 | Lucy37 | TomTABLE 2 - PROFILE_DATAPDID | Owner | Name | Value -----------------------------------------------660 | 34 | AreaType | Rural661 | 34 | Country | United Kingdom662 | 35 | AreaType | Rural663 | 35 | Country | United States664 | 36 | AreaType | Rural665 | 36 | Country | United Kingdom666 | 37 | AreaType | City667 | 37 | Country | United Kingdom668 | 34 | Age | 31669 | 34 | Weight | 211670 | 35 | Age | 22671 | 35 | Weight | 198672 | 36 | Age | 56673 | 36 | Weight | 278674 | 37 | Age | 29675 | 37 | Weight | 304If I wanted to find those with a weight LESS THAN 250 (in this case Dave and Adam) I come across a problem. All of the users are returned as they all have an AGE less than 250!Is there a way to modify jimfs query to make use of PROFILE_DATA.Name to prevent this happening.Once again I would be grateful for any help regarding this issue. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 12:56:33
|
quote: Originally posted by cjjubb Many Thanks jimf, that query appears to work.However, I now have a follow up question.I have added some numeric data to the PROFILE_DATA table as followsTABLE 1 - USERSIdent | Name--------------------34 | Dave35 | Adam36 | Lucy37 | TomTABLE 2 - PROFILE_DATAPDID | Owner | Name | Value -----------------------------------------------660 | 34 | AreaType | Rural661 | 34 | Country | United Kingdom662 | 35 | AreaType | Rural663 | 35 | Country | United States664 | 36 | AreaType | Rural665 | 36 | Country | United Kingdom666 | 37 | AreaType | City667 | 37 | Country | United Kingdom668 | 34 | Age | 31669 | 34 | Weight | 211670 | 35 | Age | 22671 | 35 | Weight | 198672 | 36 | Age | 56673 | 36 | Weight | 278674 | 37 | Age | 29675 | 37 | Weight | 304If I wanted to find those with a weight LESS THAN 250 (in this case Dave and Adam) I come across a problem. All of the users are returned as they all have an AGE less than 250!Is there a way to modify jimfs query to make use of PROFILE_DATA.Name to prevent this happening.Once again I would be grateful for any help regarding this issue.
SELECT u.NameFROM USERS uINNER JOIN PROFILE_DATA pdON pd.Owner=u.Ident AND pd.Name='Weight'AND pd.Value<250 suggest you to learn read about joins in books onilne and understand their usage. You would gain nothing by simply asking the answer without trying it yourselves and understanding. |
 |
|
|
|
|
|