?
Solved

union query results and insert into existing table in sql server

Posted on 2014-10-28
3
Medium Priority
?
273 Views
Last Modified: 2014-10-28
I have the following previous question for reference.


What I need:

I need to take the results from this UNION ALL query and insert the results into an existing table.
Same Database
Table Name is  :
OneToOneResults
fields:
WWGSKU
MODELNUMBER
MODELNAME


The union query:
--  SELECT MIN(SKU) AS SKU
--       ,[MfrNumCondensed] AS [MfrNumCondensed]
--       ,[CompMfrName] AS [CompMfrName]
--      -- ,COUNT([wwgsku]) AS [Count]
--   FROM [QA].[dbo].[bkp_vsm]
--   WHERE SKU > '' and [MfrNumCondensed] > '' and [CompMfrName] > '' and
--         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
--   GROUP BY  
--        [CompMfrName]
--   ,[MfrNumCondensed]
--   HAVING COUNT(SKU) = 1
--   --ORDER BY [MfrNumCondensed]
--UNION ALL
--  --FC
--   SELECT MIN(SKU)
--       ,[CompMfrNumCondensed]
--       ,[VdrBrandName]
--     --  ,COUNT([wwgsku]) AS [Count]
--   FROM [QA].[dbo].[bkp_vsm]
--   WHERE SKU > '' and [CompMfrNumCondensed] > '' and [VdrBrandName] > '' and
--         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
--   GROUP BY  
--        [VdrBrandName]
--   ,[CompMfrNumCondensed]
--   HAVING COUNT(SKU) = 1
--   ORDER BY [MfrNumCondensed],[CompMfrName]


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
3 Comments
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 1000 total points
ID: 40409409
INSERT INTO OneOnOneResults (WWGSKU, MODELNUMBER, MODELNAME) 
SELECT a.SKU, a.MfrNumCondensed, a.CompMfrName
FROM ( 
   Insert the UNION query here, without the comment marks) a

Open in new window

0
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 1000 total points
ID: 40409414
Here is the code:

insert into OneToOneResults (WWGSKU, MODELNUMBER, MODELNAME)
select WWGSKU, MODELNUMBER, MODELNAME
from (
  SELECT MIN(SKU) AS WWGSKU
       ,[MfrNumCondensed] AS MODELNUMBER
       ,[CompMfrName] AS MODELNAME
   FROM [QA].[dbo].[bkp_vsm]
   WHERE SKU > '' and [MfrNumCondensed] > '' and [CompMfrName] > '' and
         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
   GROUP BY  
        [CompMfrName]
   ,[MfrNumCondensed]
   HAVING COUNT(SKU) = 1
UNION ALL
   SELECT MIN(SKU)
       ,[CompMfrNumCondensed]
       ,[VdrBrandName]
   FROM [QA].[dbo].[bkp_vsm]
   WHERE SKU > '' and [CompMfrNumCondensed] > '' and [VdrBrandName] > '' and
         XRef_Comment NOT IN ('CHILD CROSSES', 'BRAND XREFS','BRAND FROM 1 TO 18')
   GROUP BY  
        [VdrBrandName]
   ,[CompMfrNumCondensed]
   HAVING COUNT(SKU) = 1
)a

Open in new window

0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 40409535
Thanks Folks !
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

770 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