September 30, 2008 at 10:11 pm
HI ,
Below is the script
---------------------------------------------------------
create table mytest
(
Recid int ,
Gid varchar(2) )
GO
------------------------------------------------
insert into mytest(gid,num) select 1 ,'a'
insert into mytest(gid,num) select 2 ,'a'
insert into mytest(gid,num) select 3 ,'a'
insert into mytest(gid,num) select 4 ,'f'
insert into mytest(gid,num) select 5 ,'f'
insert into mytest(gid,num) select 6 ,'f'
insert into mytest(gid,num) select 7 ,'b'
insert into mytest(gid,num) select 8 ,'b'
insert into mytest(gid,num) select 9 ,'b'
insert into mytest(gid,num) select 10 ,'d'
insert into mytest(gid,num) select 11 ,'d'
insert into mytest(gid,num) select 12 ,'d'
--------------------------------------
I want result (one record for every Gid) in one query.
For example
2 ,a
4 ,f
8 ,b
12,d
-------
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 30, 2008 at 10:26 pm
The test code produces errors.
The way to do this is to use MIN and GROUP BY.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 30, 2008 at 10:29 pm
Here's the corrected test code and a solution... Now, I'm curious... why don't you care which RecID you return, please?
create table mytest
(
Recid int ,
Gid varchar(2) )
GO
------------------------------------------------
insert into mytest(RecID,Gid) select 1 ,'a'
insert into mytest(RecID,Gid) select 2 ,'a'
insert into mytest(RecID,Gid) select 3 ,'a'
insert into mytest(RecID,Gid) select 4 ,'f'
insert into mytest(RecID,Gid) select 5 ,'f'
insert into mytest(RecID,Gid) select 6 ,'f'
insert into mytest(RecID,Gid) select 7 ,'b'
insert into mytest(RecID,Gid) select 8 ,'b'
insert into mytest(RecID,Gid) select 9 ,'b'
insert into mytest(RecID,Gid) select 10 ,'d'
insert into mytest(RecID,Gid) select 11 ,'d'
insert into mytest(RecID,Gid) select 12 ,'d'
SELECT MIN(RecID) AS RecID, Gid
FROM dbo.MyTest
GROUP BY Gid
--Jeff Moden
Change is inevitable... Change for the better is not.
September 30, 2008 at 10:37 pm
Is it possible to use TOP in this query ....i want to use TOP
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 30, 2008 at 10:38 pm
bhuvnesh.dogra (9/30/2008)
Is it possible to use TOP in this query ....i want to use TOP
Why?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 30, 2008 at 10:45 pm
Jeff....appreciate you immediate response.
Actually my requirement is slightly Stupid..
I want always different recid corresponding to their Gid whenever i run it.
For example
1st run
1 a
4 f
8 b
2nd run
2 a
5 f
9 b
11 d
i want to pick randomly
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 30, 2008 at 10:54 pm
This will work... without TOP or a correlated subquery to use it...
;WITH
cteRandom AS
(SELECT ROW_NUMBER() OVER (PARTITION BY Gid ORDER BY NEWID()) AS RandRow,
RecID,Gid
FROM dbo.MyTest
)
SELECT RecID,Gid
FROM cteRandom
WHERE RandRow = 1
Why do you need a random return like this? What are you doing that would require such a thing?
--Jeff Moden
Change is inevitable... Change for the better is not.
September 30, 2008 at 11:10 pm
thanks a lottt ..JEFF...it works Fine
Can u explain the logic ...i never use this CTE feature
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 30, 2008 at 11:16 pm
A CTE is nothing more than a "derived table" or an "inline view"... both of which are just sub-queries. If you did it the "old way" without a CTE, it would look like this...
SELECT r.RecID, r.Gid
FROM (SELECT ROW_NUMBER() OVER (PARTITION BY Gid ORDER BY NEWID()) AS RandRow,
RecID,Gid
FROM dbo.MyTest) r
WHERE r.RandRow = 1
The difference with a CTE is that it can appear in the FROM clause of the outer query more than once for self joins, etc.
You still need to tell me why you need to select in a random order. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
September 30, 2008 at 11:25 pm
this is basically Project requirement ,we need to show record randomly
and see below query ,i added BETWEEN clause bcoz i needed 2 records for every Gid.
BUT
Can you tell me how it is picking randomly records ? i still not got it
;WITH
cteRandom AS
(SELECT ROW_NUMBER() OVER (PARTITION BY Gid ORDER BY NEWID()) AS RandRow,
RecID,Gid
FROM dbo.MyTest
)
SELECT RecID,Gid
FROM cteRandom
WHERE RandRow between 1 and 2
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
September 30, 2008 at 11:30 pm
bhuvnesh.dogra (9/30/2008)
we need to show record randomly
Heh... I already know that... but, why is it a requirement? What is it that you are trying to do? ie. Why is it necessary to return data this way?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 1, 2008 at 6:04 am
I've answered several of your questions and provided a code solution... the least you could do is answer just one of mine... what is the purpose of this code?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 1, 2008 at 6:26 am
actually ......one of my colleague asked me about it . he is on leave today ..as sson as he comes i will let you know....
-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
October 1, 2008 at 6:57 am
Thanks. Oh, and the randomness comes from the fact that NEWID generates random numbers. If you order by a random number like that, the result will be in the same order as the random numbers... ie. Random.
The ROS_NUMBER() OVER thing just adds another column of integers to track what the ordinal order of the random numbers produced was.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 14 posts - 1 through 14 (of 14 total)
You must be logged in to reply to this topic. Login to reply