December 10, 2012 at 12:25 pm
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-01dfca534df330182425789030 25102012-01-03 00:00:00.0000
020a904b-86d3-44f2-9ceb-3e8714030c3d20165483254935 33102012-01-02 00:00:00.0000
69fd5291-52f4-4352-a5a3-4e24515875c020191378932545 44102012-01-02 00:00:00.0000
b09fd985-36ba-4c4f-ada5-8de9b6a5d6f320171392014940 41102012-01-02 00:00:00.0000
45537587-1f3c-4a71-9b73-afa7f0042aa730151222510145 23102012-01-04 00:00:00.0000
4bfe58f8-75fa-410c-a893-bf59757b03a110171304381045 32102012-01-01 00:00:00.0000
dae5e189-8f60-4448-a4a0-d683cf43b24710181347523240 43102012-01-01 00:00:00.0000
f62f7a3f-1210-4005-976d-edc4166ba79d10161321654640 31102012-01-01 00:00:00.0000
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..
December 10, 2012 at 3:31 pm
Dear Experts Please Help.
solve this issue
December 11, 2012 at 10:30 am
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
December 11, 2012 at 11:11 am
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
December 11, 2012 at 9:29 pm
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
December 12, 2012 at 6:30 am
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
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply