sql - Incorrect syntax near 'order' in insert query -


so... want able insert results of query in table.

select top 30 dt ,   count(*) numberoforders  shoptable  (name '%shop%')  group dt order dt 

this query returns number of orders per each day previous days.

now want insert table called shop_stats , query:

insert shop_stats  (dt, numberoforders)  (select top 30 dt ,   count(*) numberoforders  shoptable  (name '%shop%')  group dt order dt) 

i following error:

msg 156, level 15, state 1, line 1 incorrect syntax near keyword 'order'.

if remove order clause .... have 30 random days orders.. not last 30 need.

does know how solve in relatively easy way? (i.e. no additional scripts remove unnecessary data etc. - need single sql query).

there's no intrinsic ordering of rows within table - specific ordering supposed done when you're getting data out, not when you're putting them in.
so, remove order by clause, insert them, , order them again when select them.

that, said, if absolutely, positively, need insert them in specified order (as in case), can use subquery top clause

insert shop_stats (dt, numberoforders)  select *    (select top 30 dt, count(*) numberoforders          shoptable          (name '%shop%')          group dt          order dt) 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -