Solved

Create a UNION statement from current sql lines

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

11 Experts available now in Live!

Get 1:1 Help Now