Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Random Data Generation

Posted on 2013-06-27
5
Medium Priority
?
333 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 1000 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 1000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

650 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