Solved

How to Copy query output to the table (with out modifying the script) ?

Posted on 2014-09-04
3
85 Views
Last Modified: 2014-09-04
I have the following table

create table employee (Name varchar(50), avg1 numeric(5),dob datetime,
salary numeric(10,2),location varchar(1),bonus numeric(10,2));

insert into employee values ('AAA',31,'02/19/1980',25000,'A',45000);
insert into employee values ('AA1',44,'08/08/1982',35000,'A',35000);
insert into employee values ('AA3',10,'10/10/1975',25000,'B',50000);
insert into employee values ('AA4',30,'12/10/1999',20000,'B',25000);

Open in new window


How do I insert/copy  the following query result into the table ?

SELECT seq,
   CASE WHEN location IS NULL THEN 'Grand Total' WHEN name IS NULL THEN 'Total' ELSE name END
              name,
          AVG,
          dob,
          salary,
          location,
          bonus
     FROM (SELECT row_number() over(partition by location order by name) seq, t.* FROM employee  t
           UNION ALL
             SELECT NULL,
                          NULL,
                    SUM(AVG) AVG,
                    NULL,
                    SUM(salary) salary,
                    location,
                    SUM(bonus) bonus
               FROM employee 
           GROUP BY ROLLUP(location)) x
 ORDER BY CASE WHEN location IS NULL THEN 1 ELSE 0 END,
          location,
          CASE WHEN x.name IS NULL THEN 1 ELSE 0 END,
          x.name; 

Open in new window

0
Comment
Question by:Varshini S
[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
  • 2
3 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 500 total points
ID: 40304734
I think you are looking for a SELECT INTO:
http://www.w3schools.com/sql/sql_select_into.asp

So right before (above) your FROM statement
put INTO and the table name the data is to go into.

Obviously the columns from the select (above the INTO part) must be appropriate to fit into the columns of the destination table.
0
 

Author Comment

by:Varshini S
ID: 40304759
i try this options but it showing error

select * into temp12  from seq,
   CASE WHEN location IS NULL THEN 'Grand Total' WHEN name IS NULL THEN 'Total' ELSE name END
              name,
          AVG1,
          dob,
          salary,
          location,
          bonus
     FROM (SELECT row_number() over(partition by location order by name) seq, t.* FROM employee  t
           UNION ALL
             SELECT NULL,
                          NULL,
                    SUM(AVG1) AVG,
                    NULL,
                    SUM(salary) salary,
                    location,
                    SUM(bonus) bonus
               FROM employee 
           GROUP BY ROLLUP(location)) x
 ORDER BY CASE WHEN location IS NULL THEN 1 ELSE 0 END,
          location,
          CASE WHEN x.name IS NULL THEN 1 ELSE 0 END,
          x.name; 

Open in new window




Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'name'.
Msg 102, Level 15, State 1, Line 19
Incorrect syntax near 'x'.
0
 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
ID: 40304780
I think it would be more like:
SELECT seq,
   CASE WHEN location IS NULL THEN 'Grand Total' WHEN name IS NULL THEN 'Total' ELSE name END
              name,
          AVG,
          dob,
          salary,
          location,
          bonus
     INTO YOUR-DESTINATION-TABLE-NAME
     FROM (SELECT row_number() over(partition by location order by name) seq, t.* FROM employee  t
           UNION ALL
             SELECT NULL,
                          NULL,
                    SUM(AVG) AVG,
                    NULL,
                    SUM(salary) salary,
                    location,
                    SUM(bonus) bonus
               FROM employee 
           GROUP BY ROLLUP(location)) x
 ORDER BY CASE WHEN location IS NULL THEN 1 ELSE 0 END,
          location,
          CASE WHEN x.name IS NULL THEN 1 ELSE 0 END,
          x.name; 

Open in new window


Be sure to replace YOUR-DESTINATION-TABLE-NAME with the correct name.
Again, the columns selected must "fit" the destination table for this to work.  It shouldn't be the same table as the source, either.
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

632 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