Solved

Create a UNION statement from current sql lines

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TSQL previous 5 36
sql Total query 2 27
SQL Server stored proc 2 20
This query failed in sql 2014 5 32
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Backup & Restore 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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

803 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