简体   繁体   中英

mysql (innodb) select distinct + calculate values to copy in other table

I'm trying to combine some queries to (eventually) use them in a stored procedure because I'm afraid in the long run it will take some time to process due to calculating and I guess a datawarehouse makes faster lookups possible.

I came up with this, but I keep getting syntax error messages (#1064) (check the manual that corresponds to your MySQL server version for the right syntax to use near 'select distinct) Does anyone know a nice aproach for this?

insert into datawarehouse (itemid,rating) values
(select distinct itemid from ratings),
(select sum(rating)/count(*)from ratings where itemid in (select distinct itemid from ratings))

If I run the inner select queries separately it works but combining it seems troublesome.

In a nutshell I want to retrieve (i) the distinct itemid from table ratings, (ii) perform some calculations on the rows of table ratings for each itemid and copy those into table datawarehouse.

If anyone has an idea or a good read on this, I would love to hear it.

使用INSERT INTO ... SELECTGROUP BY

INSERT INTO datawarehouse (itemid, rating) SELECT itemid, sum(rating)/count(*) FROM ratings GROUP BY itemid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM