Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Select GROUP BY QUERY Expand / Collapse
Author
Message
Posted Monday, December 10, 2012 12:25 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 12:00 AM
Points: 11, Visits: 195
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..
Post #1394748
Posted Monday, December 10, 2012 3:31 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 12:00 AM
Points: 11, Visits: 195
Dear Experts Please Help.
solve this issue
Post #1394803
Posted Tuesday, December 11, 2012 10:30 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 9:47 AM
Points: 114, Visits: 564
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
Post #1395238
Posted Tuesday, December 11, 2012 11:11 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
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
Post #1395254
Posted Tuesday, December 11, 2012 9:29 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Today @ 12:00 AM
Points: 11, Visits: 195
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






Post #1395415
Posted Wednesday, December 12, 2012 6:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Monday, November 17, 2014 12:50 PM
Points: 13,872, Visits: 9,598
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
Post #1395631
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse