Set field to random value from another table

jPol34
update table1
set firstname = (select top 1 firstname from table2 order by NEWID())

This just sets table1.firstname to the same value for all records. I know it's possible to do this, but everything I've seen online expects the same row count in both tables (or at least a greater amount in table1). I have 200,000 records in table1, I have 200 in table2. How can I set table1.firstname to a random value from table2.firstname when the row counts are off?

Devart
DECLARE @t1 TABLE (a INT)
DECLARE @t2 TABLE (b INT, c INT)

INSERT INTO @t1(a)
VALUES (0), (1), (2), (3), (4), (5)

INSERT INTO @t2(b)
VALUES (0), (1), (2)

UPDATE t2
SET c = t1.a
FROM @t2 t2
CROSS APPLY (
    SELECT TOP(1) t1.a
    FROM @t1 t1
    WHERE t2.b IS NOT NULL -- any calculations for t2 columns
    ORDER BY NEWID()
) t1

SELECT * FROM @t2

Output -

b           c
----------- -----------
0           5
1           1
2           0

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Change value of table field when value of another field was changed in oracle

From Dev

set table random value from table

From Dev

Find records from a table that, among associated records in another table, don't have a record with a specific field value

From Dev

xpages set a value to a field from a repeat field

From Dev

Gravityforms field value from another field

From Dev

Calculated field with value from another table in Microsoft Access

From Dev

Access 2013 - Set a field value based on value of another field

From Dev

Field involving multiple rows from another table

From Dev

Rails 4 update_all and set value from another field

From Dev

OrderBy with value from another table

From Dev

update tables value from another table in mysql where updated field is foreign key

From Dev

MySQL - Update/Set a column in one table equal to MAX value from another table

From Dev

MySQL set column value from another table (JOIN 3 tables)

From Dev

how to set one field in a table equal to another field in a table in access

From Dev

MySQL overwrite value with value from another field

From Dev

select field from table in another field from another table

From Dev

Row value from another table

From Dev

set table random value from table

From Dev

How do I set the value of my field so that I can call it from another class?

From Dev

Calculated field with value from another table in Microsoft Access

From Dev

Rails 4 update_all and set value from another field

From Dev

Set Value for Field in complete Table if Value of other Field is met

From Dev

how to show field value when i store its id from another table in mysql

From Dev

How to display value of a field from a different table by using the value of a different column in another table

From Dev

Get data from a table based on a table name as a field value from another table

From Dev

Update query where set statement gets value from another table

From Dev

Select pairs of value from 2 columns from table A and insert it into another table B in random order

From Dev

Laravel select random rows from table based on another field

From Dev

Update a MySQL table with index from another table based on value set in both tables

Related Related

  1. 1

    Change value of table field when value of another field was changed in oracle

  2. 2

    set table random value from table

  3. 3

    Find records from a table that, among associated records in another table, don't have a record with a specific field value

  4. 4

    xpages set a value to a field from a repeat field

  5. 5

    Gravityforms field value from another field

  6. 6

    Calculated field with value from another table in Microsoft Access

  7. 7

    Access 2013 - Set a field value based on value of another field

  8. 8

    Field involving multiple rows from another table

  9. 9

    Rails 4 update_all and set value from another field

  10. 10

    OrderBy with value from another table

  11. 11

    update tables value from another table in mysql where updated field is foreign key

  12. 12

    MySQL - Update/Set a column in one table equal to MAX value from another table

  13. 13

    MySQL set column value from another table (JOIN 3 tables)

  14. 14

    how to set one field in a table equal to another field in a table in access

  15. 15

    MySQL overwrite value with value from another field

  16. 16

    select field from table in another field from another table

  17. 17

    Row value from another table

  18. 18

    set table random value from table

  19. 19

    How do I set the value of my field so that I can call it from another class?

  20. 20

    Calculated field with value from another table in Microsoft Access

  21. 21

    Rails 4 update_all and set value from another field

  22. 22

    Set Value for Field in complete Table if Value of other Field is met

  23. 23

    how to show field value when i store its id from another table in mysql

  24. 24

    How to display value of a field from a different table by using the value of a different column in another table

  25. 25

    Get data from a table based on a table name as a field value from another table

  26. 26

    Update query where set statement gets value from another table

  27. 27

    Select pairs of value from 2 columns from table A and insert it into another table B in random order

  28. 28

    Laravel select random rows from table based on another field

  29. 29

    Update a MySQL table with index from another table based on value set in both tables

HotTag

Archive