Can I select from a destination table in a subquery?

user3808188

Assume this is my Destination table:

    Name | Reason for Visit | Visit Number | Visit_date
    Dan  | Foo              | 1            | 04/03/2014
    Henry| Bar              | 1            | 05/07/2014
    Dan  | Tau              | 2            | 08/10/2014

I already have the Name, Reason_For_Visit and Visit_Date in my SOURCE table, and have to create the Visit_Number column. Am I allowed to sub-query the still-forming destination table like this and will I get the correct visit_number?

    insert into DESTINATION
    NAME,
    REASON_FOR_VISIT,
    (select count(d.Name)+1 from destination d where d.Name=Name) AS VISIT_NUMBER,
    VISIT_DATE
    from SOURCE
    ORDER BY VISIT_DATE ASC;

If rows are inserted one by one, I feel like I should get the correct Visit_number.

SlimsGhost

The answer to your question is YES - if you insert the first row, the second insert will see the first row already in the destination table.

Still, doing it row by row is an inefficient way to accomplish what you are doing. Why don't you try out something like this:

insert into destination
select 
    name, 
    reason_for_visit, 
    row_number() over (partition by name order by visit_date) as visit_number,
    visit_date
from source

The row_number() function will compute the visit_number correctly for you, and inserting all the rows with one query will definitely be more efficient.

Good luck!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I access a field defined as "Select 1" from a subquery in the outer query?

From Dev

How can I select from many table to one table?

From Dev

MySQL update a table and select from the same table in a subquery

From Dev

Select table with subquery

From Dev

Laravel 4 select column from another table in subquery

From Dev

Select All Values From Table That Match All Values of Subquery

From Dev

How can I select 2 different random rows from a table?

From Dev

How can I insert values from SELECT SUM into other table?

From Dev

Can I use SELECT from dataframe instead of creating this temp table?

From Dev

How can I update a table based on results from a select query?

From Dev

How can I insert values from SELECT SUM into other table?

From Dev

Can I use SELECT from dataframe instead of creating this temp table?

From Dev

MySQL, how can I reference a subquery field in a SELECT query

From Dev

Can I do a subquery in in SELECT of LINQ to Entities to fill List in model?

From Dev

How can I add to select multiple values of subquery in main query

From Dev

How can I add to select multiple values of subquery in main query

From Dev

How can I select and order items based on a subquery?

From Dev

How can I force a subquery to perform as well as a #temp table?

From Dev

How can I limit a subquery [MySQL] with union table

From Dev

Subquery from the same table

From Dev

How do I select from a subquery using the Django ORM?

From Dev

How can I pull a destination from a textbox? C#

From Dev

subquery the same table in select statement

From Dev

How can I select data from a mysql table, and then throw it into a html table

From Dev

How can I select data from a mysql table, and then throw it into a html table

From Dev

How can I use a variable from one table as a SELECT parameter for another table in php

From Dev

QueryOver: select columns from subquery

From Dev

Referencing FROM subquery in SELECT clause

From Dev

MYSQL Update from a select subquery

Related Related

  1. 1

    Why can't I access a field defined as "Select 1" from a subquery in the outer query?

  2. 2

    How can I select from many table to one table?

  3. 3

    MySQL update a table and select from the same table in a subquery

  4. 4

    Select table with subquery

  5. 5

    Laravel 4 select column from another table in subquery

  6. 6

    Select All Values From Table That Match All Values of Subquery

  7. 7

    How can I select 2 different random rows from a table?

  8. 8

    How can I insert values from SELECT SUM into other table?

  9. 9

    Can I use SELECT from dataframe instead of creating this temp table?

  10. 10

    How can I update a table based on results from a select query?

  11. 11

    How can I insert values from SELECT SUM into other table?

  12. 12

    Can I use SELECT from dataframe instead of creating this temp table?

  13. 13

    MySQL, how can I reference a subquery field in a SELECT query

  14. 14

    Can I do a subquery in in SELECT of LINQ to Entities to fill List in model?

  15. 15

    How can I add to select multiple values of subquery in main query

  16. 16

    How can I add to select multiple values of subquery in main query

  17. 17

    How can I select and order items based on a subquery?

  18. 18

    How can I force a subquery to perform as well as a #temp table?

  19. 19

    How can I limit a subquery [MySQL] with union table

  20. 20

    Subquery from the same table

  21. 21

    How do I select from a subquery using the Django ORM?

  22. 22

    How can I pull a destination from a textbox? C#

  23. 23

    subquery the same table in select statement

  24. 24

    How can I select data from a mysql table, and then throw it into a html table

  25. 25

    How can I select data from a mysql table, and then throw it into a html table

  26. 26

    How can I use a variable from one table as a SELECT parameter for another table in php

  27. 27

    QueryOver: select columns from subquery

  28. 28

    Referencing FROM subquery in SELECT clause

  29. 29

    MYSQL Update from a select subquery

HotTag

Archive