Solved

Random Data Generation

Posted on 2013-06-27
5
324 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

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…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

733 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