Author |
Topic |
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-05-26 : 05:40:39
|
I have this table structure create table ( id ,team_code ,team_name ,group_code ,group_name)This table serves as lookup table, and it also holds hierarchy of a department like father-child relationship.I have to do join with a field from another table which contains both team code and group name mixed.Am I to do two different joins on the tables? one for team_code and one for group_code Or do we have an optimised way to do the joins to cater for both team_code and group_code at the same time?Many thanks.I sign for fame not for shame but all the same, I sign my name. |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-05-26 : 06:54:46
|
It may be possible to do a single join, but hard to say without seeing the structure of the other table and some sample data. Does the other table have something to distinguish what is a group code and what is a team code?If "team code and group names are mixed", does that mean that the data is fundamentally incorrect, by having assigned some group names to team codes instead of group codes? If the data is incorrect, there may be nothing that can salvage it. |
 |
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-05-26 : 08:33:25
|
quote: hard to say without seeing the structure of the other table and some sample data. Does the other table have something to distinguish what is a group code and what is a team code?
No, there is nothing to distinguish between group code and team code.Kindly see below if this can help.. Thanksselect a.tran_id ,a.tran_date .... ... , b.team_name , b.group_bame from tableA a left outer join tableB b on a.code = b.team_code left outer join tableB b on a.code = b.group_code Note Since tableB is an hierachy table, both team_code and group_code are necessary when I want to roll-up figures. I dont't want to join to have two different entries like these {b.team_name, b.group_bame} Say if am joining through team_code then automatically, I have group code and if through group_code, then may be team_code is null. See sample report below tran_id tran_date ....... group team 1 2011-04-01 ...... 10 0 <- tableA code field = group code 2 2011-04-01 ...... 10 10 <- TableA code field = team_code Doing roll up, group sum will 20 and team sum will be 10.Thanks quote: If "team code and group names are mixed", does that mean that the data is fundamentally incorrect, by having assigned some group names to team codes instead of group codes? If the data is incorrect, there may be nothing that can salvage it.
They are actually cleaned and correct data, the application saves it that way.I sign for fame not for shame but all the same, I sign my name. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-05-26 : 12:47:05
|
Thanks LampreyKindly see belowDECLARE @team table (team_code int,group_code int, group_name varchar(20), deptcode varchar(20) )insert into @team (team_code,group_code, group_name, deptcode)select 1, 100, 'A', 'ABC' UNION ALLselect NULL, 200, 'B', 'ABD' UNION ALLselect 3, 300, 'C', 'ABE' UNION ALLselect 4, 300, 'D', 'ABE' UNION ALLselect NULL, 500, 'E', 'ABG' UNION ALLselect 2, 600, 'F', 'ABH' UNION ALLNote: @team join fields (i) team_code, (ii) group_codedeclare @transaction table (id int , desc varchar(100), code int)insert into @transaction (id , desc, code)select 1, 'Abacus', 1 UNION ALLselect 2, 'Lamprey',200 UNION ALLselect 3, 'sunitabeck', 3 UNION ALLselect 4, 'spirit1',2 UNION ALLselect 5, 'visakh16',600 UNION ALLselect 6, 'lol',300 UNION ALLNote: @transaction join fields (i) codeMy expected output1, 'Abacus', 'ABC' 2, 'Lamprey', 'ABD'3, 'sunitabeck', 'ABE'4, 'spirit1', 'ABH'5, 'visakh16', 'ABH'6, 'lol', 'ABE'I sign for fame not for shame but all the same, I sign my name. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2011-05-26 : 13:31:28
|
[code]DECLARE @team table (team_code int,group_code int, group_name varchar(20), deptcode varchar(20) )insert into @team (team_code,group_code, group_name, deptcode)select 1, 100, 'A', 'ABC' UNION ALLselect NULL, 200, 'B', 'ABD' UNION ALLselect 3, 300, 'C', 'ABE' UNION ALLselect 4, 300, 'D', 'ABE' UNION ALLselect NULL, 500, 'E', 'ABG' UNION ALLselect 2, 600, 'F', 'ABH' --Note: @team join fields (i) team_code, (ii) group_codedeclare @transaction table (id int , [desc] varchar(100), code int)insert into @transaction (id , [desc], code)select 1, 'Abacus', 1 UNION ALLselect 2, 'Lamprey',200 UNION ALLselect 3, 'sunitabeck', 3 UNION ALLselect 4, 'spirit1',2 UNION ALLselect 5, 'visakh16',600 UNION ALLselect 6, 'lol',300 --Note: @transaction join fields (i) code--My expected output----1, 'Abacus', 'ABC' --2, 'Lamprey', 'ABD'--3, 'sunitabeck', 'ABE'--4, 'spirit1', 'ABH'--5, 'visakh16', 'ABH'--6, 'lol', 'ABE'select DISTINCTt1.id,t1.[desc],coalesce(t2.deptcode,t3.deptcode,'error') as deptcodefrom @transaction as t1left join @team as t2 on t2.team_code=t1.codeleft join @team as t3 on t3.group_code=t1.code[/code] No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
abacusdotcom
Posting Yak Master
133 Posts |
Posted - 2011-05-26 : 14:08:07
|
Thanks Boss @webfred.. Yes Yak 'n' Roll... @All, Thanks..I sign for fame not for shame but all the same, I sign my name. |
 |
|
|
|
|