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 |
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-02-28 : 17:24:19
|
Guys,I have tricky situation with alias_ids we use for some of the employee ids we use/store.Person and Employee table as more than couple million records, below is the sample data.Person TablePersonid Empid_____________________ 110 1220 2Employee TableEmpid Lname Alias_flag __________________________1 Tom N2 Tim N3 Tom, aka Y4 Tom, aka2 Y Alias TableAlias_id Empid________________________3 14 1This is my query to join Person and Employee tablesselect p.personid, p.empid, e.lnamefrom person p inner join employee e onp.empid = e.empidResultPersonid empid lname_______________________________110 1 Tom220 2 TimWhat I am trying to do is since empid has aliases 3, 4 associated with it I want the result to bePersonid empid lname_______________________________110 1 Tom220 2 Tim110 3 Tom, aka110 4 Tom, aka2For this I do not want to use 'UNION' since this brings down the performance as I am dealing with 3 millions rows in each Person adn employee table with different conditions in the 'where' clause.Is there any simple way to accomplish this without using 'UNION'?any suggestions/inputs would helpThanks |
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
Posted - 2007-02-28 : 17:36:05
|
I'm not sure I understand - how are 3 and 4 related to 1, just by the flag being Y and the fact that the first part of the names are the same?You should have an alias column in the table that holds 1 for 3 and 4. Then you could just join to that column too. |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-02-28 : 18:05:54
|
I think this works as you described it:-- SetupDECLARE @Person TABLE(PersonID INT, EmpID INT)INSERT @PersonSELECT 110, 1 UNION ALLSELECT 220, 2DECLARE @Employee TABLE(EmpID INT, LName VARCHAR(100), ALias_Flag CHAR(1))INSERT @Employee SELECT 1, 'Tom', 'N' UNION ALLSELECT 2, 'Tim', 'N' UNION ALLSELECT 3, 'Tom, aka', 'Y' UNION ALLSELECT 4, 'Tom, aka2', 'Y'DECLARE @Alias TABLE(Alias_ID INT, EmpID INT)INSERT @AliasSELECT 3, 1 UNION ALLSELECT 4, 1-- QuerySELECT p.PersonID, e.EmpID, e.LNameFROM @Employee eLEFT OUTER JOIN @Alias a ON e.EmpID = a.Alias_IDLEFT OUTER JOIN @Person p ON p.EmpID = e.EmpID OR a.EmpID = p.EmpID -Ryan |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-03-01 : 15:58:04
|
lamprey, Thank you for the reply, I tried your query below doesnt seem to work below - there is a little change the tables Please don't think this as a puzzle the only other thing I can do if the query doesn't work is added case_id to alias table update the column and then write a query to display the result, but that seems to not work too. SELECT e.person_alias_id, C.caseidFROM person eLEFT OUTER JOIN Alias a ON e.person_alias_id = a.Alias_IDLEFT OUTER JOIN Party p ON p.person_alias_id = e.person_alias_id OR a.alias_id = p.person_alias_idLEFT OUTER JOIN [fCASE] C ON C.CASEID = P.CASE_ID case tablecase_id___________31571party tableperson_alias_id case-id_______________________________30911 31571person tableperson_alias_id lastname_________________________________30911 Tom30912 Tom, aka30200 Tom, aka2alias tableentity_id alias_id________________________30911 3091230911 30200Resultperson_alias_id case_id lastname_________________________________________30911 31571 Tom30912 31571 Tom, aka30200 31571 Tom, aka2any suggestions/inputs to write this query Thanks |
 |
|
rlaubert
Yak Posting Veteran
96 Posts |
Posted - 2007-03-01 : 16:13:24
|
How about a left outer join and a having clause. Left outer join on person table so all records are returned and join on the other tables where there are matches. Having clause if you only want records with alaisRaymond LaubertMCDBA, MCITP:Administration, MCT |
 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2007-03-01 : 17:23:18
|
can you please elaborate ?, I think thats what I doing in queryThanks |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-03-01 : 17:40:49
|
Hehe, I had a couple minutes before I meeting. I think this will work.-- SET UPDECLARE @Case TABLE(case_id INT)INSERT @CaseSELECT 31571DECLARE @Party TABLE (person_alias_id INT, case_id INT)INSERT @PartySELECT 30911, 31571DECLARE @Person TABLE (person_alias_id INT, lastname VARCHAR(100))INSERT @PersonSELECT 30911, 'Tom' UNION ALLSELECT 30912, 'Tom, aka' UNION ALLSELECT 30200, 'Tom, aka2' UNION ALLSELECT 35000, 'Timmy'DECLARE @Alias TABLE (entity_id INT, alias_id INT)INSERT @AliasSELECT 30911, 30912 UNION ALLSELECT 30911, 30200-- QUERYSELECT DISTINCT p.person_alias_id, par.case_id, p.lastnameFROM @Person pLEFT OUTER JOIN @Alias a ON p.person_alias_id = a.entity_id OR p.person_alias_id = a.alias_idLEFT OUTER JOIN @Party par ON a.entity_id = par.person_alias_idLEFT OUTER JOIN @Case c ON par.case_id = c.case_idWHERE c.case_id IS NOT NULL |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-03-01 : 17:43:21
|
Also, here is another one without the join to Case as it should not be needed.SELECT DISTINCT p.person_alias_id, par.case_id, p.lastname, par.case_idFROM @Person pLEFT OUTER JOIN @Alias a ON p.person_alias_id = a.entity_id OR p.person_alias_id = a.alias_idLEFT OUTER JOIN @Party par ON a.entity_id = par.person_alias_idWHERE par.case_id IS NOT NULL |
 |
|
|
|
|
|
|