Solved

union query results and insert into existing table in sql server

Posted on 2014-10-28
3
252 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
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 250 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

809 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