0

I have UCP table and SalesPerProduct which contained data I want to insert into SalesPerProduct From UCP Conditional select When i do select it works fine i used this query

select t.ID, sum(Qty) as sum_BioxellTerritories , 
sum(SalesResult) as Sales , BioxellTerritories 
from UCP as t group by ID ,BioxellTerritories
ORDER BY BioxellTerritories ASC 

when I Used insert

insert into SalesPerProduct 
(ProductID , Volume , Sales ,Territories )values
select t.ID, sum(Qty) as sum_BioxellTerritories , sum(SalesResult) 
as Sales , BioxellTerritories from UCP as t group by ID ,BioxellTerritories   ORDER BY BioxellTerritories ASC 

Didn't work for me .. for sample of data This is the result of select which I want to insert

enter image description here

6
  • what didn't work for you? did you get an error? you need to remove order by during insert and remove values (as you are using a select) Commented May 1, 2017 at 23:59
  • Incorrect syntax near the keyword 'select'. Commented May 2, 2017 at 0:00
  • Oh, you don't need the word values there Commented May 2, 2017 at 0:02
  • ty :) works for me can u write ur answer to accept it u deserve it :) Commented May 2, 2017 at 0:03
  • The same answer has already been posted, feel free to select one of them :) Commented May 2, 2017 at 0:06

2 Answers 2

2

You just want insert . . . select:

insert into SalesPerProduct (ProductID, Volume, Sales, Territories )
    select t.ID, sum(Qty) as sum_BioxellTerritories, sum(SalesResult) 
as Sales, BioxellTerritories
    from UCP as t
    group by ID, BioxellTerritories
    order by BioxellTerritories ASC ;

The VALUES keyword is unnecessary.

Sign up to request clarification or add additional context in comments.

1 Comment

Also note as per this answer: always use a target column list with the insert statement
1

Have a look at this reference for INSERT INTO SELECT statements: https://www.w3schools.com/sql/sql_insert_into_select.asp

In short, you don't need the VALUES keyword when combining a SELECT statement with INSERT INTO.

If you still have problems I'd suggest posting your table definitions so people can help you further.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.