Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Select GROUP BY QUERY


Select GROUP BY QUERY

Author
Message
imneaz
imneaz
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 258
I have a table like:
CREATE TABLE [dbo].[tbl_FaceBookScore](
[ROWGUID] [uniqueidentifier] ROWGUIDCOL NOT NULL,
[Quiz_No] [bigint] NULL,
[Mobile_No] [varchar](20) NULL,
[Score] [nchar](10) NULL,
[TotalTime] [bigint] NULL,
[TotalQuestion] [int] NULL,
[EntryDate] [datetime] NULL,
[Flag] [int] NULL)

and some records like :
84ed3af6-617b-4022-97bb-01dfca534df3 3 01824257890 30 25 10 2012-01-03 00:00:00.000 0
020a904b-86d3-44f2-9ceb-3e8714030c3d 2 01654832549 35 33 10 2012-01-02 00:00:00.000 0
69fd5291-52f4-4352-a5a3-4e24515875c0 2 01913789325 45 44 10 2012-01-02 00:00:00.000 0
b09fd985-36ba-4c4f-ada5-8de9b6a5d6f3 2 01713920149 40 41 10 2012-01-02 00:00:00.000 0
45537587-1f3c-4a71-9b73-afa7f0042aa7 3 01512225101 45 23 10 2012-01-04 00:00:00.000 0
4bfe58f8-75fa-410c-a893-bf59757b03a1 1 01713043810 45 32 10 2012-01-01 00:00:00.000 0
dae5e189-8f60-4448-a4a0-d683cf43b247 1 01813475232 40 43 10 2012-01-01 00:00:00.000 0
f62f7a3f-1210-4005-976d-edc4166ba79d 1 01613216546 40 31 10 2012-01-01 00:00:00.000 0


SELECT a.Quiz_No,a.Mobile_No,a.Score,a.TotalTime,a.TotalQuestion
FROM dbo.tbl_FaceBookScore a
WHERE a.Quiz_No IN (SELECT TOP 1 y.Quiz_No FROM dbo.tbl_FaceBookScore y
WHERE y.Quiz_No = a.Quiz_No
ORDER BY y.Quiz_No)
ORDER BY a.Quiz_No ASC,a.Score DESC, TotalTime DESC

output is
Quiz_No Mobile_No Score TotalTime TotalQuestion
------- ---------- ---- ------- -------------
1---01713043810---45 ---32--- 10
1---01813475232---40 ---43--- 10
1---01613216546---40--- 31--- 10
2---01913789325---45--- 44--- 10
2---01713920149---40--- 41--- 10
2---01654832549---35--- 33--- 10
3---01512225101---45--- 23--- 10
3---01824257890---30--- 25--- 10

but desire result is not execute

my requirement is :

Quiz_No Mobile_No Score TotalTime TotalQuestion
-------------------- -------------------- ---------- --------------------
1--- 01713043810--- 45--- 32--- 10
2--- 01913789325--- 45--- 44--- 10
3--- 01512225101--- 45--- 23--- 10

somebody help me please..
imneaz
imneaz
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 258
Dear Experts Please Help.
solve this issue
lnoland
lnoland
SSC-Enthusiastic
SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)SSC-Enthusiastic (143 reputation)

Group: General Forum Members
Points: 143 Visits: 794
If you want help, to start with, you really need to explain what it is you are trying to do. You posted a query which doesn't do what you want so we can't tell for sure from that what you want because we already know the query is wrong. You posted the data you wish to see but there are an infinite number of ways of producing that output data, including a query which just contains those values as literals. Most of those ways (including the one with the literals) are likely not at all what you want -- but you haven't told us what you want so we can only guess.

From the nature of the data, I suspect that what you are trying to get is the row, for each quiz number, which has the highest score (I have no idea what you want to do if there is more than one row with the high score so I will just presume that you want all of them).

I don't have time to explain what is wrong with your query but suffice it to say that the correlated subquery is absolutely useless because it always produces the same value as the main query, so you always get the full table (also, it is attempting to filter by the quiz_no when apparently you want to filter by the score).

So, my guess at what you want is:


SELECT a.Quiz_No,a.Mobile_No,a.Score,a.TotalTime,a.TotalQuestion
FROM dbo.tbl_FaceBookScore a
WHERE a.Score =
(
SELECT TOP 1 y.Score
FROM dbo.tbl_FaceBookScore y
WHERE y.Quiz_No = a.Quiz_No
ORDER BY y.Score DESC
)
ORDER BY a.Quiz_No ASC, TotalTime DESC



Not tested because you provided no INSERT statements to populate the table and I'm not going to spend my time doing the work you should have done.

I hope that helps.

- Les
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
I can't tell what criteria you are using to determine which rows to return in your query.

Is it the last (per EntryDate) for each quiz? The first? Something else?

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
imneaz
imneaz
Grasshopper
Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)Grasshopper (17 reputation)

Group: General Forum Members
Points: 17 Visits: 258
Dear SSC Journeyman
thank you for your quick response.
i actually want to show a report which will publish in facebook
the main application is deal with SMS quiz competition and all quiz participant's detail result (total question, total time, total score and participant's mobile no) store in a table dbo.tbl_FaceBookScore.

so my requirement is to publish only one TOP scorer in every group (Quiz_No) who score highest in the shortest time. by default 10 question will sent to every participant one by one. and time count by minute.

thankyou your quick response. it helps me lot. i used you query and make something changes and so i get my desire result.

SELECT a.Quiz_No,a.Mobile_No,a.Score,a.TotalTime,a.TotalQuestion
FROM dbo.tbl_FaceBookScore a
WHERE a.Score =
( SELECT TOP 1 y.Score FROM dbo.tbl_FaceBookScore y
WHERE y.Quiz_No = a.Quiz_No ORDER BY y.Score DESC
) AND
a.TotalTime =
( SELECT TOP 1 z.TotalTime FROM dbo.tbl_FaceBookScore z
WHERE z.Quiz_No = a.Quiz_No AND z.Score = a.Score
ORDER BY z.TotalTime ASC )
ORDER BY a.Quiz_No DESC, a.TotalTime ASC, a.Score DESC

thank you once again Mr. SSC Journeyman


Regards

imneaz
GSquared
GSquared
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14385 Visits: 9729
Are you actually using SQL 2000 (or 7), as per the forum, or do you have a more recent version available? Which solution to use depends on that.

If it's actually 2000, then you need to write one query that pulls the Max() score for each quiz, and then join that to the data so you can get the non-aggregate rows.

If it's 2005 or later, you can query the quizzes and use Outer Apply to get the top row for each. (Use Outer instead of Cross, if you want to show quizzes that don't have top scores yet. Use Cross Apply if you want to leave those off.)

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search