Link to home
Start Free TrialLog in
Avatar of Varshini S
Varshini S

asked on

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

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

SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Varshini S
Varshini S

ASKER

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'.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial