Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Oracle -- SQL SELECT -- UNION

Posted on 2009-07-09
5
Medium Priority
?
989 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 500 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…

609 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