Solved

Oracle -- SQL SELECT -- UNION

Posted on 2009-07-09
5
966 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
  • 3
  • 2
5 Comments
 
LVL 28

Expert Comment

by:Naveen Kumar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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.

Join & Write a Comment

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 recover a database from a user managed backup

772 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

11 Experts available now in Live!

Get 1:1 Help Now