Solved

union query results and insert into existing table in sql server

Posted on 2014-10-28
3
248 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
3 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
Thanks Folks !
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

8 Experts available now in Live!

Get 1:1 Help Now