Manually insert resultset into query

dzona

I have one DB with managers and its dependent users. And the other db on other server, with some other information about users.

I want to query second DB and get data grouped by managers. One solution for this is to loop through result from db2 and calculate new result in application, but I would like to try to build result set from db1, with data about users and its managers to query to be executed on db2. Something like pseudo code below:

SELECT
b.manager_id,
SUM(a.column)
FROM user a
JOIN <resultset_with_assoc_between_user_and_manager> b ON a.user_id = b.manager_id
GROUP BY b.manager_id

Is this possible, without making any new tables in db2, just to make query with data from db1?

dzona

This is solution I made. It works fine, but I don't know what are performance penalties vs creating tmp table with user_id -> manager_id records, and joining them with db2_table . But as I said, db2_table is huuuuuuuge

SELECT

CASE
   WHEN a.user_id IN(user_id11, user_id12,user_id13, ...) THEN manager_id1
   WHEN a.user_id IN(user_id21, user_id22,user_id23, ...) THEN manager_id2
   WHEN a.user_id IN(user_id31, user_id32,user_id33, ...) THEN manager_id3
   ...
   ELSE a.user_id
END AS manager,
...

FROM db2_table a

GROUP BY manager

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

simple insert query not working

分類Dev

Insert query is failing

分類Dev

php mysql(INSERT QUERY)

分類Dev

mysql insert into query not working

分類Dev

Numeric overflow in insert query

分類Dev

Can sql query result be manually mapped with room?

分類Dev

SQL insert query throws error

分類Dev

Improve INSERT INTO SELECT Query performance?

分類Dev

Insert query format comprising of date

分類Dev

Mysql INSERT query inserts zeroes

分類Dev

Django insert query results into database

分類Dev

MYSQL INSERT INTO FROM SELECT QUERY

分類Dev

Wait for query result of Bookshelf insert

分類Dev

Creating single query with subquery, essentially a insert query with select query

分類Dev

JUnit namedParameterJdbcTemplate.query( ""、parameters、(ResultSet rs))をモックする方法

分類Dev

Can I use the same ResultSet statement object in if-else ladder with different query?

分類Dev

How to execute a LINQ to Entities query, pulling values from an existing resultset variable instead of against the DB

分類Dev

Postgres INSERT ON CONFLICT DO NOTHING vs SELECT + INSERT query

分類Dev

UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

分類Dev

Sql query to insert datetime in SQL Server

分類Dev

Paypal transaction tracking with Wordpress insert query?

分類Dev

How to make insert query using SQLiteCommand?

分類Dev

How to execute Multiple Insert statements in a single query?

分類Dev

Insert or update query executed successfully but data are not persistent

分類Dev

insert using sub-query and inner join

分類Dev

Syntax error for insert query alias in Postgres

分類Dev

Why is my SQL "INSERT INTO" query not working?

分類Dev

Mysql Insert query with values and select as subquery

分類Dev

C Sharp MysqlBulkInsert vs Muplitple Insert Query

Related 関連記事

  1. 1

    simple insert query not working

  2. 2

    Insert query is failing

  3. 3

    php mysql(INSERT QUERY)

  4. 4

    mysql insert into query not working

  5. 5

    Numeric overflow in insert query

  6. 6

    Can sql query result be manually mapped with room?

  7. 7

    SQL insert query throws error

  8. 8

    Improve INSERT INTO SELECT Query performance?

  9. 9

    Insert query format comprising of date

  10. 10

    Mysql INSERT query inserts zeroes

  11. 11

    Django insert query results into database

  12. 12

    MYSQL INSERT INTO FROM SELECT QUERY

  13. 13

    Wait for query result of Bookshelf insert

  14. 14

    Creating single query with subquery, essentially a insert query with select query

  15. 15

    JUnit namedParameterJdbcTemplate.query( ""、parameters、(ResultSet rs))をモックする方法

  16. 16

    Can I use the same ResultSet statement object in if-else ladder with different query?

  17. 17

    How to execute a LINQ to Entities query, pulling values from an existing resultset variable instead of against the DB

  18. 18

    Postgres INSERT ON CONFLICT DO NOTHING vs SELECT + INSERT query

  19. 19

    UPDATE, INSERT and ON DUPLICATE KEY UPDATE in one query

  20. 20

    Sql query to insert datetime in SQL Server

  21. 21

    Paypal transaction tracking with Wordpress insert query?

  22. 22

    How to make insert query using SQLiteCommand?

  23. 23

    How to execute Multiple Insert statements in a single query?

  24. 24

    Insert or update query executed successfully but data are not persistent

  25. 25

    insert using sub-query and inner join

  26. 26

    Syntax error for insert query alias in Postgres

  27. 27

    Why is my SQL "INSERT INTO" query not working?

  28. 28

    Mysql Insert query with values and select as subquery

  29. 29

    C Sharp MysqlBulkInsert vs Muplitple Insert Query

ホットタグ

アーカイブ