Solved

Oracle -- SQL SELECT -- UNION

Posted on 2009-07-09
5
978 Views
Last Modified: 2013-12-19
I currently run the below to
get ID_DESC & ID_BRAND changes.

How can I make this query shorter
by using just a SELECT statement ?
  ** tables attached
-----------------------------------------------------------------------------------
SELECT new.ID_STK_NO SKU, new.ID_DESC NAME,
       'ID_DESC' CHANGE_TYPE,
       old.ID_DESC OLDVALUE, new.ID_DESC NEWVALUE, new.ID_SOURCE
FROM GML_IMPORT_ITEMMSTR new
LEFT JOIN GML_IMPORT_ITEMMSTR_OS old
ON new.ID_STK_NO=old.ID_STK_NO
WHERE nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x')
and
  (
 
  new.ID_SOURCE in ('F','P')
 
  or
 
  old.ID_SOURCE in ('F','P')
 
  )
and
  (
 
   new.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   
   or
   
   old.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   
   )

union

SELECT new.ID_STK_NO SKU, new.ID_DESC NAME,
       'ID_BRAND' CHANGE_TYPE,
       old.ID_BRAND OLDVALUE, new.ID_BRAND NEWVALUE, new.ID_SOURCE
FROM GML_IMPORT_ITEMMSTR new
LEFT JOIN GML_IMPORT_ITEMMSTR_OS old
ON new.ID_STK_NO=old.ID_STK_NO
WHERE nvl(new.ID_BRAND,'x') != nvl(old.ID_BRAND,'x')
and
  (
 
  new.ID_SOURCE in ('F','P')
 
  or
 
  old.ID_SOURCE in ('F','P')
 
  )
and
  (
 
   new.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   or
   
   old.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   )
ORDER BY SKU, CHANGE_TYPE
tables.txt
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 24820600
may be something like this.. but not sure what you are trying to do and why ?

SELECT new.ID_STK_NO SKU, new.ID_DESC NAME,
       'ID_BRAND' CHANGE_TYPE,
       old.ID_BRAND OLDVALUE, new.ID_BRAND NEWVALUE,
       'ID_DESC' CHANGE_TYPE1,
       old.ID_DESC OLDVALUE1, new.ID_DESC NEWVALUE1,
       new.ID_SOURCE      
FROM GML_IMPORT_ITEMMSTR new
LEFT JOIN GML_IMPORT_ITEMMSTR_OS old
ON new.ID_STK_NO=old.ID_STK_NO
WHERE (
nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x')
or nvl(new.ID_BRAND,'x') != nvl(old.ID_BRAND,'x') )
and
  ( new.ID_SOURCE in ('F','P') or    old.ID_SOURCE in ('F','P')   )
and
  (  
   new.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')  
   or  
   old.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')  
   )
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 24820603
becasue you wanted everything in one query without using union, we need to have extra column in the above query to get the information you are looking for.
0
 
LVL 28

Expert Comment

by:Naveen Kumar
ID: 24820607
provide some sample data for 2 or 3 records in each table and say what output is coming for the old union query and what output you need without using the union query.
0
 
LVL 11

Expert Comment

by:yuching
ID: 24838186
If for one record, it can be only either ID_DESC or ID_BRAND changed, try this

Select new.ID_STK_NO SKU, new.ID_DESC NAME,
    Case When nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Then 'ID_DESC' Else 'ID_BRAND' End As CHANGE_TYPE,
    Case When nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Then  old.ID_DESC Else old.ID_BRAND End As OLDVALUE ,
    Case When nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Then new.ID_DESC Else new.ID_BRAND End As NEWVALUE,
    new.ID_SOURCE
FROM GML_IMPORT_ITEMMSTR new
LEFT JOIN GML_IMPORT_ITEMMSTR_OS Old ON new.ID_STK_NO=old.ID_STK_NO
WHERE (nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Or nvl(new.ID_BRAND,'x') != nvl(old.ID_BRAND,'x'))
And  ( new.ID_SOURCE in ('F','P') Or old.ID_SOURCE in ('F','P'))
and
  (
   new.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   Or
   old.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   )
0
 
LVL 11

Accepted Solution

by:
yuching earned 125 total points
ID: 24838187
If for one record, it can be only either ID_DESC or ID_BRAND changed, try this

Select new.ID_STK_NO SKU, new.ID_DESC NAME,
    Case When nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Then 'ID_DESC' Else 'ID_BRAND' End As CHANGE_TYPE,
    Case When nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Then  old.ID_DESC Else old.ID_BRAND End As OLDVALUE ,
    Case When nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Then new.ID_DESC Else new.ID_BRAND End As NEWVALUE,
    new.ID_SOURCE
FROM GML_IMPORT_ITEMMSTR new
LEFT JOIN GML_IMPORT_ITEMMSTR_OS Old ON new.ID_STK_NO=old.ID_STK_NO
WHERE (nvl(new.ID_DESC,'x') != nvl(old.ID_DESC,'x') Or nvl(new.ID_BRAND,'x') != nvl(old.ID_BRAND,'x'))
And  ( new.ID_SOURCE in ('F','P') Or old.ID_SOURCE in ('F','P'))
and
  (
   new.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   Or
   old.ID_BRAND in ('ALBRT', 'AMIGO', 'CMRKT', 'CUBFD', 'DMNCK',
                 'FLRIT', 'FDLAN', 'GRTVL', 'IGA..', 'RCHFD',
                 'RILEY', 'SAFWY', 'SCOTT', 'SH&'||'SV', 'SHPVL',
                 'WEGMN')
   )
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

734 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