Solved

How do I get rid of the cartesian effect

Posted on 2014-07-22
8
225 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

759 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

18 Experts available now in Live!

Get 1:1 Help Now