Solved

Oracle -- SQL SELECT -- UNION

Posted on 2009-07-09
5
980 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

691 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