Solved

Random Data Generation

Posted on 2013-06-27
5
321 Views
Last Modified: 2013-06-28
I have a table in the following structure,

<pre lang="sql">CREATE TABLE [dbo].[RateCard](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
    [rate] [money] NULL,
 CONSTRAINT [PK_RateCard] PRIMARY KEY CLUSTERED
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF</pre>


<pre lang="sql">insert into ratecard(name,rate)values(&#39;A&#39;,10);
insert into ratecard(name,rate)values(&#39;B&#39;,20);
insert into ratecard(name,rate)values(&#39;C&#39;,25);
insert into ratecard(name,rate)values(&#39;D&#39;,30);
insert into ratecard(name,rate)values(&#39;E&#39;,65);
insert into ratecard(name,rate)values(&#39;F&#39;,65);
insert into ratecard(name,rate)values(&#39;G&#39;,10);
insert into ratecard(name,rate)values(&#39;H&#39;,10);
insert into ratecard(name,rate)values(&#39;I&#39;,20);
insert into ratecard(name,rate)values(&#39;J&#39;,10);
insert into ratecard(name,rate)values(&#39;K&#39;,10);
insert into ratecard(name,rate)values(&#39;L&#39;,15);
insert into ratecard(name,rate)values(&#39;M&#39;,10);
insert into ratecard(name,rate)values(&#39;N&#39;,50);</pre>


select *From ratecard where rate=10 order by rate

When I run the above query I get the result as follow as

1      A      10.00
7      G      10.00
8      H      10.00
10      J      10.00
11      K      10.00
13      M      10.00

When I run the query for the second time I need the output as

7      G      10.00
1      A      10.00
8      H      10.00
10      J      10.00
11      K      10.00
13      M      10.00


When I run the query for the Third time I need the output as

7      G      10.00
1      A      10.00
10      J      10.00
8      H      10.00
11      K      10.00
13      M      10.00


Each time I run the query I should get the random result. How to do it in SQL Server Query.
0
Comment
Question by:Loganathan Natarajan
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
Louis01 earned 250 total points
ID: 39280903
If you simply want a random order, this would work:
select *
  from ratecard 
 where rate=10 
 order by NEWID()

Open in new window

0
 
LVL 16

Assisted Solution

by:EvilPostIt
EvilPostIt earned 250 total points
ID: 39280905
select *From ratecard where rate=10 order by newid()
0
 
LVL 11

Expert Comment

by:Louis01
ID: 39281145
hhhmmmm... just curious... why was my answer (being exactly the same) (and first) (and better formatted) ...;)... not accepted?
0
 
LVL 16

Expert Comment

by:EvilPostIt
ID: 39281191
How dare you ;)
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now