Solved

Oracle -- SQL SELECT -- UNION

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Bash Script to Analyze Oracle Schemas 11 104
execute immediate plsql block 5 46
Oracle DB monitor SW 21 48
make null the repeated levels 2 22
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

777 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