Solved

How do I get rid of the cartesian effect

Posted on 2014-07-22
8
249 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
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 45

Accepted Solution

by:
Kdo 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:Kdo
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:Kdo
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

825 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