Solved

How do I get rid of the cartesian effect

Posted on 2014-07-22
8
231 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
 
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now