Solved

union query results and insert into existing table in sql server

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

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

830 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