Solved

Distinct in union

Posted on 2012-03-16
4
582 Views
Last Modified: 2012-03-16
Hi,

I have the following union query:

SELECT
      'BECKWITH' as 'SLC',
      SOI,
      Cast(RTrim(substring(supplier_description,patindex('%-%',supplier_description)+1,50)) as nvarchar(50)) 'Supplier'
FROM         CEVA5_Compass.dbo.titems_inv with(nolock)
                   JOIN CEVA5_Compass.dbo.tsupplier on CEVA5_Compass.dbo.titems_inv.supplier_key = CEVA5_Compass.dbo.tsupplier.supplier_key
GROUP BY SOI, supplier_description

UNION ALL

SELECT
      'BECKWITH' as 'SLC',
      SOI,
      Cast(RTrim(substring(supplier_description,patindex('%-%',supplier_description)+1,50)) as nvarchar(50)) 'Supplier'
FROM         NFC_Compass.dbo.titems_inv with(nolock)
                   JOIN NFC_Compass.dbo.tsupplier on NFC_Compass.dbo.titems_inv.supplier_key = NFC_Compass.dbo.tsupplier.supplier_key
GROUP BY SOI, supplier_description

Which yields (subset)

BECKWITH      69640       AVOCENT
BECKWITH      13560       BROCADE
BECKWITH      70020       CERQA
BECKWITH      56730       CHICONY AMERICA, INC
BECKWITH      56730       CHICONY AMERICA, INC
BECKWITH      75160       EATON CORPORATION
BECKWITH      69070       ELPIDA
BECKWITH      GIG01       EMULEX
BECKWITH      72743       FORCECON
BECKWITH      72744       FORCECON


My question is how do I only get distinct values meaning that it is ok to display FORCECON twice since the second column is different (72743 abd 72744) but I only want to display  CHICONY AMERICA, INC once since both rows are identical.

Thank you !
0
Comment
Question by:JessyRobinson1234
[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
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
leoahmad earned 500 total points
ID: 37729032
1.
SELECT
      'BECKWITH' as 'SLC',
      SOI,
      Cast(RTrim(substring(supplier_description,patindex('%-%',supplier_description)+1,50)) as nvarchar(50)) 'Supplier'
FROM         CEVA5_Compass.dbo.titems_inv with(nolock)
                   JOIN CEVA5_Compass.dbo.tsupplier on CEVA5_Compass.dbo.titems_inv.supplier_key = CEVA5_Compass.dbo.tsupplier.supplier_key
GROUP BY SOI, supplier_description

UNION

SELECT
      'BECKWITH' as 'SLC',
      SOI,
      Cast(RTrim(substring(supplier_description,patindex('%-%',supplier_description)+1,50)) as nvarchar(50)) 'Supplier'
FROM         NFC_Compass.dbo.titems_inv with(nolock)
                   JOIN NFC_Compass.dbo.tsupplier on NFC_Compass.dbo.titems_inv.supplier_key = NFC_Compass.dbo.tsupplier.supplier_key
GROUP BY SOI, supplier_description

2.
select distinct * from
(SELECT
      'BECKWITH' as 'SLC',
      SOI,
      Cast(RTrim(substring(supplier_description,patindex('%-%',supplier_description)+1,50)) as nvarchar(50)) 'Supplier'
FROM         CEVA5_Compass.dbo.titems_inv with(nolock)
                   JOIN CEVA5_Compass.dbo.tsupplier on CEVA5_Compass.dbo.titems_inv.supplier_key = CEVA5_Compass.dbo.tsupplier.supplier_key
GROUP BY SOI, supplier_description

UNION ALL

SELECT
      'BECKWITH' as 'SLC',
      SOI,
      Cast(RTrim(substring(supplier_description,patindex('%-%',supplier_description)+1,50)) as nvarchar(50)) 'Supplier'
FROM         NFC_Compass.dbo.titems_inv with(nolock)
                   JOIN NFC_Compass.dbo.tsupplier on NFC_Compass.dbo.titems_inv.supplier_key = NFC_Compass.dbo.tsupplier.supplier_key
GROUP BY SOI, supplier_description))
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 37729111
Hi Jessy,

Leo's code is correct.  The explanation is that UNION ALL does just that.  It performs a UNION of ALL rows in the result set.  The UNION operator filters out the duplicates.


Good Luck,
Kent
0
 

Author Comment

by:JessyRobinson1234
ID: 37729148
Thank you both, should I go with suggestion 1?
0
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 37729197
I would.  :)
0

Featured Post

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

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