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 |
|
henrikop
Constraint Violating Yak Guru
280 Posts |
Posted - 2003-08-19 : 04:34:16
|
| SQL is like solving puzzles. Sometimes I just don't see it.I have users, and they have media (telephone numerbers, e-mail adresses, fax numbers, etc)I am using the table media not only for Users, but also customers, and creditors, etc.I have three tables:UsersMediaUserMediaUsers : UserId, UserNameMedia : MediaId, Content, ContentCategorie (where content is the number and Categorie is the kind of number (fax, telephone, e-mail)UserMedia: ID, User, MediaWhat I want is make a query which returns a user only once, and with a business telephone if availableSELECT u.UserName, m.ContentFROM User uLEFT JOIN UserMedia um ON um.User = u.UserId LEFT JOIN Media m ON m.MediaId = um.Media AND m.ContentCategory = 2GROUP BY u.Username, m.ContentORDER BY m.Content DESC My result would be likeDick NULLHenri 12345678Henri NULLCarl 67832647Carl NULLMichel NULLOf course I want a name only to show up once, and with a telephone number prefferably.( m.ContentCategory = 2 is businessphone #)WHILE writing this I think I got it.instead of m.Contact I make a subquery like this(SELECT TOP 1 m.Content FROM Media m JOIN UserContent uc ON uc.Media = m.MediaId WHERE ContentCategory = 2 AND uc.User = u.User) It's this the way?Henri~~~Success is the ability to go from one failure to another with no loss of enthusiasm |
|
|
Amethystium
Aged Yak Warrior
701 Posts |
Posted - 2003-08-19 : 05:03:34
|
Hi there,SELECT u.UserName, m.ContentFROM User uJOIN UserMedia um ON um.User = u.UserId LEFT JOIN Media m ON m.MediaId = um.Media AND m.ContentCategory = 2GROUP BY u.Username, m.ContentORDER BY m.Content DESC No need for a LEFT JOIN on USERMEDIA.Also try and avoid keywords such as USER for your column/table names.------------------------------------------------------------------------------I enjoy using SQL Server but I am not part of the Microsoft fanboy club! NEVER!! |
 |
|
|
henrikop
Constraint Violating Yak Guru
280 Posts |
Posted - 2003-08-19 : 05:17:29
|
thx Amethystium, but this does not the trick. Some users don't have telephones, then no records shows up. And when there's a specific record, the name shows up twice.My problem is a little more complicated and I only wrote the 'gist' of the problem. I won't use reserved words Henri~~~Success is the ability to go from one failure to another with no loss of enthusiasm |
 |
|
|
mohdowais
Sheikh of Yak Knowledge
1456 Posts |
Posted - 2003-08-19 : 06:00:08
|
| Try this:SELECT u.UserName, MAX(m.Content)FROM User uLEFT JOIN UserMedia um ON um.User = u.UserId LEFT JOIN Media m ON m.MediaId = um.Media AND m.ContentCategory = 2GROUP BY u.UsernameORDER BY MAX(m.Content) DESC Owais |
 |
|
|
henrikop
Constraint Violating Yak Guru
280 Posts |
Posted - 2003-08-19 : 09:15:31
|
Amethystium,That's working as should be, thx!! Henri~~~Success is the ability to go from one failure to another with no loss of enthusiasm |
 |
|
|
|
|
|
|
|