Вопрос

I have SQL query that need to do some calculation by a subquery.

INSERT INTO db.my_new_table 
SELECT db.table1.id  ,  db.table1.value  *  db.table2.factor  AS myValue 
FROM
 (
      SELECT a.id, SUM(a.value) AS factor
      FROM  db.table0  a
      GROUP by a.id
 ) AS table1
 JOIN  db.table2   AS  b
 ON db.table1.id  =  b.id;

But, I got error:

  ERROR [HY000] ERROR:  Cross Database Access not supported for this type of command

I do not want to create a new table at first before running the query on db. I have access to db. I want to do creating and inserting at the same time.

Thanks !

Это было полезно?

Решение

The problem is in your column references. You do not need to prefix columns with the schema, just with the alias. Also, your subquery does not have the field "value"; I think you may have transposed fields. Try this:

INSERT INTO db.my_new_table 
SELECT a.id, 
    a.factor * b.value AS myValue 
FROM (
        SELECT id, SUM(value) AS factor
        FROM  db.table0
        GROUP by id
     ) AS a
JOIN db.table2 AS b
     ON a.id = b.id;

Другие советы

If your problem is truly that you don't have a table and want to create it as part of the insert statement, just change your insert to CREATE TABLE db.my_new_table AS SELECT ....

See the docs here: http://pic.dhe.ibm.com/infocenter/ntz/v7r0m3/index.jsp?topic=%2Fcom.ibm.nz.dbu.doc%2Fr_dbuser_create_table_as.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top