| Author |
Topic |
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-01 : 10:24:34
|
| I have a table as belowCol1 usr_GroupUsr-----------------------ser1 individual1ser2 group1ser2 individual2ser3 individual3ser4 group2ser4 group3TAB2group usr ------------------------group1 individual1group1 individual2group1 individual3group2 abc1group2 xyz1group2 abc2group3 abc1group3 abc2I want to o/p of the table 1 where ever group1..n is thereshould be replace with all users for that particular group from table 2 other colum value would repeat like ser col value can repeate.can any one help me in updating this.regards,aakcse |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-01 : 10:50:56
|
| [code]-- Fake tablesdeclare @yak table( Col1 char(4), usr_GroupUsr varchar(15))declare @llama table ([group] varchar(15), usr varchar(15))-- insert our datainsert into @yak (Col1,usr_GroupUsr)SELECT 'ser1', 'individual1'UNION ALL SELECT 'ser2', 'group1'UNION ALL SELECT 'ser2', 'individual2'UNION ALL SELECT 'ser3', 'individual3'UNION ALL SELECT 'ser4', 'group2'UNION ALL SELECT 'ser4', 'group3'insert into @llama([group],usr)SELECT 'group1', 'individual1'UNION ALL SELECT 'group1', 'individual2'UNION ALL SELECT 'group1', 'individual3'UNION ALL SELECT 'group2', 'abc1'UNION ALL SELECT 'group2', 'xyz1'UNION ALL SELECT 'group2', 'abc2'UNION ALL SELECT 'group3', 'abc1'UNION ALL SELECT 'group3', 'abc2'-- do the workselect y.Col1,CASE WHEN l.[group] IS NULL THEN y.usr_GroupUsr ELSE l.usr END as UsrFROM @yak yLEFT JOIN @llama lON y.usr_GroupUsr = l.[group] [/code][Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-01 : 10:58:08
|
| Thanks,however the group are more in nuber say there are around 50 groups.Thanks againRegards,aak |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-04-01 : 11:43:40
|
| Can you give the expected output based on your sample data. This will make things clear. |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-01 : 11:50:32
|
| Try it and see what happens. Otherwise, follow the first link in my signature and restate your question.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-01 : 13:27:43
|
| My sincere apology if, the question was not clear.Expected out put would be likeTable1Col1 usr_groupUsr---------------------ser1 individual1ser2 individual1 ser2 individual2ser2 individual3ser3 individual3ser4 abc1ser4 xyz1ser4 abc2ser4 abc1ser4 abc2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-01 : 13:37:30
|
| [code]SELECT Col1,UsrFROM(SELECT t1.Col1, t1.usr_GroupUsr AS UsrFROM table1 t1LEFT JOIN table2 t2ON t2.group=t1.usr_GroupUsrWHERE t2.group IS NULLUNION SELECT t1.Col1,t2.usrFROM table1 t1JOIN table2 t2ON t2.group=t1.usr_GroupUsr)tORDER BY Col1,Usr[/code] |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-01 : 14:09:34
|
Why would ser4 have a repeating abc2, but ser2 not have a repeating individual2?Your expected output does not match your given data and criteria. The code i posted gives what you asked for.Col1 Usrser1 individual1ser2 individual1ser2 individual2ser2 individual3ser2 individual2ser3 individual3ser4 abc1ser4 xyz1ser4 abc2ser4 abc1ser4 abc2 You should get ser2 individual2 from group1, and also from the individual record containing individual2.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-01 : 14:11:25
|
Thank you all.quote: Originally posted by visakh16
SELECT Col1,UsrFROM(SELECT t1.Col1, t1.usr_GroupUsr AS UsrFROM table1 t1LEFT JOIN table2 t2ON t2.group=t1.usr_GroupUsrWHERE t2.group IS NULLUNION SELECT t1.Col1,t2.usrFROM table1 t1JOIN table2 t2ON t2.group=t1.usr_GroupUsr)tORDER BY Col1,Usr
|
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-01 : 14:16:00
|
| visakh's code does not give you what you asked for. It eliminates duplicates. you asked for them to be there. Which is it?Otherwise, slap a DISTINCT clause on my code and you get what you want also.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-02 : 00:30:41
|
Point noted Don. quote: Originally posted by DonAtWork visakh's code does not give you what you asked for. It eliminates duplicates. you asked for them to be there. Which is it?Otherwise, slap a DISTINCT clause on my code and you get what you want also.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp
|
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-02 : 03:36:31
|
| The below query I have tested for couple of recrods it looks to be fine.Don is this correct?SELECT A.col1, COALESCE(B.usr,A.usr_groupUsr) AS usrFROM Tab1 A LEFT JOIN Tab2 BON A.usr_groupUsr = B.groups |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-04-02 : 05:18:49
|
| selecta.col1,CASE WHEN a.usr_GroupUsr= b.groupTHEN b.usr ELSE a.usr_GroupUsr END Only_Usersfromtab1 a LEFT JOIN tab2 b ON A.usr_GroupUsr = B.group The above select query is also giving me the same results as the above is giving.is this right.Regards,aak |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2009-04-02 : 10:36:58
|
| If the query gives you what you want, then i would say it is right.[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQL or How to sell Used CarsFor ultra basic questions, follow these links.http://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|