Solved

Create a UNION statement from current sql lines

Posted on 2014-10-27
4
111 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
[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 66

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 66

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 49

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

     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 …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

690 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