| Author |
Topic |
|
shanmp
Starting Member
13 Posts |
Posted - 2008-08-11 : 03:08:33
|
| Hi all, Please let me know the 'minus' function in SQL server, its working in Oracle but not in SQL server, I searched it and finally i got the result that i need to use 'NOT EXISTS' but its not giving me the exact result which i need. Let me walk through an example: There are two tables Employee & Emp. The column emp_id and emp_name is loaded from Emp to Employee. Here is the query which i have written: Select emp_id, emp_name from Employeewhere not exists (select emp_id, emp_name from emp where emp_id=Employee.emp_id) Though there are some discrepancies in the Employee table , the query results are not showing them exactly. The transformation is populating three records in Employee table and some where null too Data: Emp tableEmp_Id Emp_Name 1 ABCDEmployee Table Emp_Id Emp_Name 1 Null1 ABCD1 ABCD Please let me know how to pick the records from Employee table such as Emp_Id Emp_Name 1 Null1 ABCD Its not working in the NOT Exists query. Please let me know is there any other possibility to work out this. since there are million of records its impos to check it manually. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 03:16:04
|
SELECT DISTINCT Emp_ID, Emp_Name from Employee E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 03:17:25
|
MINUS does exist in SQL Server with version 2005 and later.Read about EXCEPT in Books Online. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 03:19:57
|
[code]DECLARE @Emp TABLE (Emp_ID INT, Emp_Name VARCHAR(20))INSERT @EmpSELECT 1, 'ABCD'DECLARE @Employee TABLE (Emp_ID INT, Emp_Name VARCHAR(20))INSERT @EmployeeSELECT 1, NULL UNION ALLSELECT 2, 'ABCD'SELECT *FROM @EmployeeEXCEPTSELECT *FROM @Emp[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
shanmp
Starting Member
13 Posts |
Posted - 2008-08-11 : 06:27:29
|
| Hi, I am using TOAD for SQL server 3.1 , I am unable to use the function 'Except' . May i know any other possibilities to retrieve the unmatched records. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-11 : 06:36:45
|
| [code]SELECT e.Emp_Id,e.Emp_NameFROM Employee eLEFT JOIN EmpTable e1ON e1.Emp_Id=e.Emp_IdWHERE e1.Emp_Id IS NULL[/code] |
 |
|
|
shanmp
Starting Member
13 Posts |
Posted - 2008-08-11 : 06:55:07
|
| I have given an example as both the tables have the same column name , but it will not work when the column name or different in Left Join. Please let me know is there any other possibility to retrieve the unmatched records. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 08:03:06
|
Visakh, what happened to the NULL record? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-11 : 08:16:47
|
| [code]declare @Emp table(Emp_Id int,Emp_Name varchar(5))insert into @Empselect 1, 'ABCD' union allselect 3,'AAP'declare @Employee Table (Emp_Id int,Emp_Name varchar(5))insert into @Employeeselect 1, Null union allselect 1, 'ABCD' union allselect 2, 'ABCD' union allselect 2,'YRU' union allselect 3,'AAB' union allselect 3,'AAP'select e1.* from @EMployee e1LEFT JOIN @Emp eon e.Emp_Id=e1.Emp_IdAND e.Emp_Name=e1.Emp_NameWHERe e.Emp_ID IS NULLoutput----------------Emp_Id Emp_Name----------- --------1 NULL2 ABCD2 YRU3 AAB[/code] |
 |
|
|
shanmp
Starting Member
13 Posts |
Posted - 2008-08-12 : 02:57:45
|
| Hope I havent explain clearly what is my scenario. Let me explain it again. Its kinda testing whether the records are loaded properly from one table to another table. One is Emp and another one is Employee table. Now my job is to validate the records loaded into the Employee table. I have only one single record for the emp_id 1, but when the transformation is done its coming as three records instead of single records. So it means there are some issue in the transformation logic. Leaving it apart, now i need pick only the unmatched records from the Employee table. I need only SQL query , left join will not work for all the combinations. I have given the example that both the table of same column name and same set , but if there are more columns coming from two different table , what would be the case. So kindly gimme the answer which should match all perspectives. Hope I am clear by writing this query. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-12 : 03:00:34
|
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-12 : 04:22:23
|
quote: Originally posted by shanmp Hope I havent explain clearly what is my scenario. Let me explain it again. Its kinda testing whether the records are loaded properly from one table to another table. One is Emp and another one is Employee table. Now my job is to validate the records loaded into the Employee table. I have only one single record for the emp_id 1, but when the transformation is done its coming as three records instead of single records. So it means there are some issue in the transformation logic. Leaving it apart, now i need pick only the unmatched records from the Employee table. I need only SQL query , left join will not work for all the combinations. I have given the example that both the table of same column name and same set , but if there are more columns coming from two different table , what would be the case. So kindly gimme the answer which should match all perspectives. Hope I am clear by writing this query.
Not clear still. can you explain what you mean by thisI have given the example that both the table of same column name and same set , but if there are more columns coming from two different table , what would be the casealso provide some data examples to explain your scenario along with verbal description. |
 |
|
|
shanmp
Starting Member
13 Posts |
Posted - 2008-08-14 : 01:50:20
|
| In the Example which i have given , it has same column name so left join is possible but whereas I need to choose two column from two different tables the join will not work. Let me again give a clear example: Here is a scenario: Source tables: Emp , Location Emp table columns: (Id, Name)1 GeorgeLocation table columns: (Id, Location)ID Location 1 LATarget Tables: Emp_details Emp_details columns: (Id, Name, Location)Now the data are populated from Source table to target table:The Target table should have one single row: 1 George LA. But its populated wrongly as below:1 George LA 1 null null Now when i write query in Oracle SQL it works fine Select id, name, location from Emp_details minus select e.id, e.name , l.location from emp e, location lThis query will return the exact mismatch records as 1 null nullWhereas in SQL server , how to write the same query ? It has to be in simple ways rather than declare. For your information i used not exists function but its not showing the mismatch records. Its just says Records 0 of 0. But i need to pick this particular record 1 null null. How is that pos ? Note: This is not the case for one record there are million records and the query should work in an efficient way. I just want to filter the mismatch records. Hope i m clear. Revert me in case of any other questions |
 |
|
|
|