Solved

Random Data Generation

Posted on 2013-06-27
5
320 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

14 Experts available now in Live!

Get 1:1 Help Now