Solved

union query results and insert into existing table in sql server

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Amazon RDS migrate to SQL Server 3 46
Assigning Database Principals to Database Roles 3 39
Database Mail Profiles 1 48
when sa SQL Login becomes locked 6 32
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

737 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