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
Solved

Create a UNION statement from current sql lines

Posted on 2014-10-27
4
109 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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