x

A table with random values from each table

I have 3 table .a,b,c .The Name whose datatype is varchar .I need a query that will get one row from each table and give me a another table namely 'd'.This d will have 3 rows for eg. a1 above b3 above c2 ..these values in table d are random every time I run this query ..

  Table a   Table b   Table c 
  Name      Name       Name  
  a1        b1         c1  
  a2        b2         c2 
  a3        b3         c3

when i run the query I need a table 'd' with random

 Table d    Table d
  Name       Name
  a2         b1
  b3         a3
  c1         c3

and there is no relation between these tables !

more ▼

asked May 09, 2012 at 11:28 AM in Default

avatar image

fashraf
538 16 21 28

(comments are locked)
10|1200 characters needed characters left

1 answer: sort voted first
 declare @TableA table (name varchar(10))
 declare @TableB table (name varchar(10))
 declare @TableC table (name varchar(10))
 
 insert into @TableA select 'a1'
 insert into @TableA select 'a2'
 insert into @TableA select 'a3'
 insert into @TableB select 'b1'
 insert into @TableB select 'b2'
 insert into @TableB select 'b3'
 insert into @TableC select 'c1'
 insert into @TableC select 'c2'
 insert into @TableC select 'c3'
 
 
 --TableD
 select (select top 1 name from @TableA order by newid()) as Name
 union all
 select (select top 1 name from @TableB order by newid())
 union all
 select (select top 1 name from @TableC order by newid())
more ▼

answered May 09, 2012 at 11:33 AM

avatar image

Kev Riley ♦♦
64.2k 48 62 81

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x2091
x11

asked: May 09, 2012 at 11:28 AM

Seen: 621 times

Last Updated: May 09, 2012 at 11:33 AM

Copyright 2016 Redgate Software. Privacy Policy