Solved

Create a UNION statement from current sql lines

Posted on 2014-10-27
4
97 Views
Last Modified: 2014-10-28
sql server 2008

Is there a way to make the following statement a UNION statement ?
since i'am using different field names and orderby ?



----AB
  SELECT MIN(GSKU) AS GSKU
       ,[CompSKUCondensed]
       ,[CompName]
       ,COUNT([GSKU]) AS [Count]
   FROM [QA].[dbo].[xref_gw]
   WHERE GSKU > '' and CompSKUCondensed > '' and CompName > '' and
         XRef_Comment NOT IN ('PARENT CHILD CROSSES', 'BRAND XREFS FROM 1 TO 16','BRAND FROM 1 TO 16')
   GROUP BY  
        [CompSKUCondensed]
       ,[CompName]
   HAVING COUNT(GSKU) = 1
   ORDER BY CompSKUCondensed
  - --CD
  SELECT MIN(GSKU) AS GSKU
       ,[CompMfrNumCondensed]
       ,[CompMfrName]
       ,COUNT([GSKU]) AS [Count]
   FROM [QA].[dbo].[xref_gw]
   WHERE GSKU > '' and CompMfrNumCondensed > '' and CompMfrName > '' and
         XRef_Comment NOT IN ('PARENT CHILD CROSSES', 'BRAND XREFS FROM 1 TO 16','BRAND FROM 1 TO 16')
   GROUP BY  
        [CompMfrNumCondensed]
       ,[CompMfrName]
   HAVING COUNT(GSKU) = 1
   ORDER BY CompMfrNumCondensed


Thanks
fordraiders
0
Comment
Question by:fordraiders
  • 2
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 40406509
Yes.  Couple of things...

1.

The labels in the first part of the UNION will be the labels for the entire set

2.

Make sure what you're marrying up has the same data type.  

3.

ORDER BY only works at the end, i.e. you can't order by each query in a UNION

4.

btw I renamed count to gksu_count to avoid any confusion.
----AB
SELECT MIN(GSKU) AS GSKU
       ,[CompSKUCondensed] as [CompSKUCondensed]
       ,[CompName] as CompName, 
       ,COUNT([GSKU]) AS [gksu_count]
FROM blah blah blah
UNION
SELECT MIN(GSKU), 
       ,[CompMfrNumCondensed]
       ,[CompMfrName]
       ,COUNT([GSKU])
FROM blah blah blah
 ORDER BY CompSKUCondensed

Open in new window

0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40406512
The above also assumes that the four columns in each part of the UNION returns as the same.
If that is not the case, the way to populate a column with only one part of a UNION is to create dummy values, such as...

SELECT id, name as name_one, NULL as name_two
FROM table1
UNION
SELECT id, NULL, name
FROM table2
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 250 total points
ID: 40407660
UNION is designed to produce a result of uniquely different rows & to do this it does MORE WORK than UNION ALL

As you do want all rows from both queries use the faster UNION ALL

All the other conditions that Jim has pointed to still apply.


also:
the column  COUNT([GSKU]) will always be 1 due to the having clause.

it isn't necessary to output COUNT([GSKU]) to use that calculation in the HAVING clause, e.g. this would still work:
SELECT MIN(GSKU) AS GSKU
       ,[CompSKUCondensed]
       ,[CompName]
   FROM [QA].[dbo].[xref_gw]
   WHERE GSKU > '' and CompSKUCondensed > '' and CompName > '' and
         XRef_Comment NOT IN ('PARENT CHILD CROSSES', 'BRAND XREFS FROM 1 TO 16','BRAND FROM 1 TO 16')
   GROUP BY  
        [CompSKUCondensed]
       ,[CompName]
   HAVING COUNT(GSKU) = 1

Open in new window

0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40408951
Thanks to Both for helping.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now