Solved

How do I get rid of the cartesian effect

Posted on 2014-07-22
8
261 Views
Last Modified: 2014-07-22
I am just selecting on a query that contain cartesian  records.  How do I get rid of these records in my query?

SELECT distinct newEmpid,
                newLastName,
                newFirstName,
                newD2LRole,
                newemail,
                newLoc,
                newLocname,
                prevEmpid,
                prevLastName,
                prevFirstName,
                prevD2LRole,
                prevemail,
                prevLoc,
                prevLocName,
                DateChanged,
                Status
  FROM [GSDR].[d2l].[D2L_Reporting_D2L_non_teach_delta]
  where newEmpid = 'e199702275'

see attachment for the records

I am getting 4 records but I only want 2
data.xlsx
0
Comment
Question by:Butterfly2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 40212280
Butterfly2,

The reason you're getting 4 rows is because location columns are different.  Do you need all those columns in your result set?  If not, eliminate them from your query.

regards,

AielloJ
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40212288
Do you actually have a unique key id on the table?  If so what is the column name?
0
 

Author Comment

by:Butterfly2
ID: 40212308
this is a table I made myself.  There is no unique key.  i was actually trying to do a delta table but I figured that maybe I could get rid of these records after i made the table.  But I will post my original query that made the table, maybe there is something I can do in there.

with a (newEmpid,
        newLastName,
        newFirstName,
        newD2LRole,
        newEmail,
        newLoc,
        NewLocname,
        DateChanged
        )
   as
     
 
(select distinct IsNUll(empid,'') as newEmpid,
                 IsNull(lastname,'') as newLastName,
                 IsNull(firstname,'') as newFirstName,
                 IsNull (d2l_Role,'') as newD2LRole,
                 IsNull(email, '') as newemail,
                 IsNull(loc,'') as newLoc,
                 ISNULL(locname,'') as NewLocname,
                 convert(date,GETDATE()) as DateChanged
                 
               
from d2l.D2L_Reporting_d2l_non_teach
except

select distinct IsNUll(empid,'') as newEmpid,
                 IsNull(lastname,'') as newLastName,
                 IsNull(firstname,'') as newFirstName,
                 IsNull (d2l_Role,'') as newD2LRole,
                 IsNull(email, '') as newemail,
                 IsNull(loc,'') as newLoc,
                 ISNULL(locname,'') as NewLocname,
                 convert(date,GETDATE()) as DateChanged
               
from d2l.D2L_Reporting_d2l_non_teach_prev

),

b (prevEmpid,
   prevLastName,
   prevFirstName,
   prevD2LRole,
   prevEmail,
   prevLoc,
   prevLocName,
   DateChanged)

 as
(select distinct IsNUll(empid,'') as prevEmpid,
                 IsNull(lastname,'') as prevLastName,
                 IsNull(firstname,'') as prevFirstName,
                 IsNull (d2l_Role,'') as prevD2LRole,
                 IsNull(email, '') as prevEmail,
                 IsNull(loc,'') as prevLoc,
                 IsNull(locname,'') as prevLocName,
                 convert(date,GETDATE()) as DateChanged
from d2l.D2L_Reporting_d2l_non_teach_prev
except
select distinct IsNUll(empid,'') as prevEmpid,
                 IsNull(lastname,'') as prevLastName,
                 IsNull(firstname,'') as prevFirstName,
                 IsNull (d2l_Role,'') as prevD2LRole,
                 IsNull(email, '') as prevEmail,
                 IsNull(loc,'') as prevLoc,
                 IsNull(locname,'') as prevLocName,
                 convert(date,GETDATE()) as DateChanged
from d2l.D2L_Reporting_d2l_non_teach

)
INSERT INTO [GSDR].[d2l].[D2L_Reporting_D2L_non_teach_delta]

select distinct  a.newEmpid,
                 a.newLastName,
                 a.newFirstName,
                 a.newD2LRole,
                 a.newEmail,
                 a.newLoc,
                 a.NewLocname,
                 b.prevEmpid,
                 b.prevLastName,
                 b.prevFirstName,
                 b.prevD2LRole,
                 b.prevEmail,
                 b.prevLoc,
                 b.prevLocName,
                 case
                 when a.DateChanged IS NULL
                 Then b.DateChanged
                 when b.DateChanged IS Null
                 Then a.DateChanged
                 else ''
                 end as DateChanged,
                 case
                 when a.newEmpid IS Null
                 Then 'Deleted'
                 when b.prevEmpid IS Null
                 Then 'Addition'
                 Else 'Change'
                 End as Status
       
from a
 full join b
   on a.newEmpid = b.prevEmpid
   
 
  order by a.newLastName
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 45

Accepted Solution

by:
Kent Olsen earned 430 total points
ID: 40212377
Hi Butterfly,

If you run just the SELECT Portion of the insert statement, you should see the duplicated rows, caused by the FULL JOIN.

It's at this point you'll want to filter the rows to insert only the desired rows.  There are quite a few ways to do this.  Filter by oldest/newest date, or any other column.


Kent


select distinct  a.newEmpid,
                 a.newLastName,
                 a.newFirstName,
                 a.newD2LRole,
                 a.newEmail,
                 a.newLoc,
                 a.NewLocname,
                 b.prevEmpid,
                 b.prevLastName,
                 b.prevFirstName,
                 b.prevD2LRole,
                 b.prevEmail,
                 b.prevLoc,
                 b.prevLocName,
                 case
                 when a.DateChanged IS NULL
                 Then b.DateChanged
                 when b.DateChanged IS Null
                 Then a.DateChanged
                 else ''
                 end as DateChanged,
                 case
                 when a.newEmpid IS Null
                 Then 'Deleted'
                 when b.prevEmpid IS Null
                 Then 'Addition'
                 Else 'Change'
                 End as Status
       
from a
 full join b
   on a.newEmpid = b.prevEmpid
   
 
  order by a.newLastName 

Open in new window

0
 

Author Comment

by:Butterfly2
ID: 40212426
Everytime I use a filter records that I need are removed.
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 40212457
Then we need to modify the filter.

Can you post what you've tried as the filter?
0
 

Author Comment

by:Butterfly2
ID: 40212513
ok I played around with the filters and I think I got it.

This is what I used.
(a.newLoc <> b.prevLoc
 or a.newD2LRole <> prevD2LRole
 or a.newEmpid is null
 or b.prevEmpid is null)
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 40212522
If that solves it, great!

Is the data likely to change so that a second row gets past the filter?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question