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

x
?
Solved

Distinct in union

Posted on 2012-03-16
4
Medium Priority
?
588 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:
Muhammad Ahmad Imran earned 2000 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 46

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 46

Expert Comment

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Shell script to create broker configuration file using current broker Configuration, solely for purpose of backup on Linux. Script may need to be modified depending on OS-installation. Please deploy and verify the script in a test environment.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

597 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