SQL query result from multiple tables without dupl

2019-09-19 06:45发布

I have a number of tables with filtered from all the records customer ID's, Last Order Date and that order Total $, Segment Name. Each filter is based on different criteria but, same customer ID can belong two different tables, two different segments. Same ID would have different values in Last Order and Total in . Segments, table names are A, B, C, D.

I need to group the records from All the segment tables in a way that there are no duplicate ID's in the set. i.e.: if an ID appears in more than one table (say ID 2 is in tables A and B) the result set has to be showing ID columns from the first table, table A.

So I need to list of all the records and their column values from Segment A table, list of all the records and its values from Segment B table except if any ID in Segment B table is in Segment A and list of all the records from Segment C table except if ID from Segment C are in Segment A or B table . I hope it does makes sense.

I made it sound like a question from 70-461 exam :D I've researched it quite thoroughly but perhaps I don't see how to ask that questions. I wonder if anyone would have idea of how to build a query to get that result. Big thanks for any suggestions.

Thanks guys. I couldn't seem to post a screenshot. Let me try to type it via html. There are more segment tables but just typing two to give you an idea. Thanks guys!

Segment A
----------------------------------------
ID | Last Order Date  | Total  | Segment     
----------------------------------------
1  | 01/01/2012       | $1     |  A         
----------------------------------------
2  | 01/01/2012       | $1     |  A     
----------------------------------------              
3  | 01/01/2012       | $5     |  A     
----------------------------------------                 
6  | 01/01/2012       | $7     |  A     
----------------------------------------                 
8  | 01/01/2012       | $8     |  A  


Segment B

ID | Last Order Date | Total | Segment     
--------------------------------------
4  | 01/01/2010      | $3    |  B        
--------------------------------------
2  | 01/01/2010      | $5    |  B     
--------------------------------------            
1  | 01/01/2010      | $2    |  B     
--------------------------------------                
3  | 01/01/2010      | $1    |  B     
--------------------------------------                
5  | 01/01/2010      | $7    |  B  



Result Set

ID | Last Order Date | Total | Segment
--------------------------------------     
1  |    01/01/2012   |  $1   |    A         
--------------------------------------
2  |    01/01/2012   |  $1   |    A   
--------------------------------------                
3  |    01/01/2012   |  $5   |    A   
--------------------------------------                  
4  |    01/01/2010   |  $3   |    B   
--------------------------------------      
5  |    01/01/2010   |  $7   |    B  

1条回答
甜甜的少女心
2楼-- · 2019-09-19 07:16

Here's something to get you started:

SELECT ID, LastOrderDate, Total, Segment
FROM SegmentA

UNION ALL
SELECT ID, LastOrderDate, Total, Segment
FROM SegmentB
WHERE ID NOT IN (SELECT ID FROM SegmentA)

UNION ALL
SELECT ID, LastOrderDate, Total, Segment
FROM SegmentC
WHERE ID NOT IN (SELECT ID FROM SegmentA)
AND ID NOT IN (SELECT ID FROM SegmentB)

UNION ALL
SELECT ID, LastOrderDate, Total, Segment
FROM SegmentD
WHERE ID NOT IN (SELECT ID FROM SegmentA)
AND ID NOT IN (SELECT ID FROM SegmentB)
AND ID NOT IN (SELECT ID FROM SegmentC)

A very simplistic answer, more information is needed if you want to optimize this.

查看更多
登录 后发表回答