If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Join 2 SQL statements

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-25-07, 12:05
robert casaletta robert casaletta is offline
Registered User
 
Join Date: Oct 2001
Location: fairview, tn
Posts: 8
Join 2 SQL statements

I am having a difficulty joining these two statements. I would like union them and get one set of unique data. Currently, there is overlap in the part_no and eff_dt. Any thoughts?

SELECT A.FCLTY_NO, A.VIN_PFX_CD||A.VIN_SER_NO AS VIN, A.WO_NO, A.WOL_NO, B.WO_OPEN_DT, C.EFF_DT, B.WO_COMPL_DT,
A.PART_NO, C.DLR_NET_PRC_AM
FROM DRBA.SVC_WOL_PART_T A,
DRBA.SVC_WO_T B,
DRBA.PRT_PART_PRC_T C
WHERE A.FCLTY_NO = B.FCLTY_NO
AND A.VIN_PFX_CD = B.VIN_PFX_CD
AND A.VIN_SER_NO = B.VIN_SER_NO
AND A.WO_NO = B.WO_NO
AND A.PART_NO = C.PART_NO
AND A.FCLTY_NO = '2261'
AND B.LAST_SUBM_DT BETWEEN '01NOV2006' AND '30APR2007'
AND C.EFF_DT BETWEEN B.WO_OPEN_DT AND B.WO_COMPL_DT
AND A.PART_NO LIKE '%9MP%'
ORDER BY A.WO_NO, A.WOL_NO, A.PART_NO

SELECT FCLTY_NO, VIN, WO_NO, WOL_NO, WO_OPEN_DT, EFF_DT, PART_NO, DLR_NET_PRC_AM
FROM (SELECT DISTINCT A.FCLTY_NO, A.VIN_PFX_CD||A.VIN_SER_NO AS VIN, A.WO_NO, A.WOL_NO, B.WO_OPEN_DT, C.PART_NO, C.DLR_NET_PRC_AM,
C.EFF_DT
FROM DRBA.SVC_WOL_PART_T A,
DRBA.SVC_WO_T B,
DRBA.PRT_PART_PRC_T C
WHERE A.FCLTY_NO = B.FCLTY_NO
AND A.VIN_PFX_CD = B.VIN_PFX_CD
AND A.VIN_SER_NO = B.VIN_SER_NO
AND A.WO_NO = B.WO_NO
AND A.PART_NO = C.PART_NO
AND A.FCLTY_NO = '2261'
AND B.LAST_SUBM_DT BETWEEN '01NOV2006' AND '30APR2007'
AND A.PART_NO LIKE '%9MP%'
AND C.EFF_DT = (SELECT MAX(EFF_DT)
FROM DRBA.PRT_PART_PRC_T D
WHERE C.PART_NO = D.PART_NO
AND EFF_DT < B.WO_OPEN_DT)
ORDER BY A.WO_NO, A.WOL_NO, C.PART_NO)
Reply With Quote
  #2 (permalink)  
Old 05-25-07, 12:47
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
add a NULL in between EFF_DT and PART_NO in the SELECT clause of the second query


__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-25-07, 12:49
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh, and also remove the ORDER BY clauses, and add a new ORDER BY clause at the end, after the two SELECTs

and of course use UNION between them
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 05-25-07, 12:54
robert casaletta robert casaletta is offline
Registered User
 
Join Date: Oct 2001
Location: fairview, tn
Posts: 8
Not sure what you mean by add NULL between them....please explain.
Reply With Quote
  #5 (permalink)  
Old 05-25-07, 13:19
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Code:
SELECT ..., CAST(NULL AS INT) AS part_no, ...
Also, if you know that all records are unique, use UNION ALL. UNION implies a duplicate elimination.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #6 (permalink)  
Old 05-29-07, 05:46
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Rudy means, that you don't have the same number of columns in each select statement, and as a result they won't line up on top of each other, hence your error.

Rule of thumb : For union statements you MUST have the same number of columns in each select statement involved.
Reply With Quote
  #7 (permalink)  
Old 05-29-07, 10:30
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Technically, that's not a rule of thumb. Its just a plain-old rule!
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #8 (permalink)  
Old 05-29-07, 11:46
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
I have it written on MY thumb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On