tags:

views:

92

answers:

3

I'm trying to write sql that produces the desired result from the data below.

data:

IDNum  Opt1 Opt2 Opt3 Count
1      A     A    E    1 
1      A     B    J    4
2      A     A    E    9
3      B     A    F    1
3      B     C    K    14
4      A     A    M    3
5      B     D    G    5
6      C     C    E    13
6      C     C    M    1

desired result:

IDNum  Opt1 Opt2 Opt3 Count
1      A     B    J     4
2      A     A    E     9
3      B     C    K     14
4      A     A    M     3
5      B     D    G     5
6      C     C    E     13

Essentially I want, for each ID Num, the full record with the highest count. I tried doing a group by, but if I group by Opt1, Opt2, Opt3, this doesn't work because it returns the highest count for each (ID Num, Opt2, Opt3, Opt4) combination which is not what I want. If I only group by ID Num, I can get the max for each ID Num but I lose the information as to which (Opt1, Opt2, Opt3) combination gives this count.

I feel like I've done this before, but I don't often work with sql and I can't remember how. Is there an easy way to do this?

+1  A: 

Edit Prior to op clarifying question for access this would have worked. I am not famillar with access to know if this query would be supported.


I think this will work on SQL Server.

select * from data
inner join (select idnum, max(count) from data
            group by idNum )sub
on sub.IdNum=data.IdNum && sub.Count=data.Count

Of course if you have two id's with the same count it would return both rows...

JoshBerke
yeah sorry about that. this kind of thing will work in access if you don't mind not being able to "see" the query in design view. what i'll do so that my boss can understand is just split into two queries and have a third do the join. thanks!
oob
Ugh you also have to use the designer?
JoshBerke
Unfortunately I do. It can save time if you just need quick results for non-programming related tasks, but it sure is a pain for subqueries because you have to actually design a new query for each subquery you might write in handmade sql.
oob
To both of you: Stop bashing Access. If you don't like it, keep it to yourself. It makes you look like childish idiots to make such comments about the tools you're using.
David-W-Fenton
@David - I have every right to express my subjective distaste for a product. Yes it has it's roles and uses, but I am happy to say I don't have to use it. Also who bashed it? I said ugh you have to use the designer which makes working with complicated queries a pain if it's even possible. Also sqlnoob at no point bashed it, but stated simple fact. I think your a little too high strung to be using the internet, but what does a childish idiot like myself know?
JoshBerke
@David that's the best method for getting people to agree with you i've ever seen. thanks!
oob
I'm not interested in people agreeing with me. I'm only interested in seeing them add useful information to the discusion, which the comments in question were not.
David-W-Fenton
@David - If you only want useful information then why even comment in the first place. You seem to get all bent out of shape over nothing.
JoshBerke
I'm "bent out of shape" over content-free comments that don't further the discussion. If you can't stand to be called out on that kind of childishness, then don't make those kinds of comments.
David-W-Fenton
@David - If you can't stand to let two people have a casual conversation about the requirements they are facing without resorting to name calling then I suggest you find someplace else to troll. You can bash a product, you can bash an idea, but to sit there and call a person childish and an idiot is unacceptable and uncalled for.
JoshBerke
\/\/hatever -- you two are the ones who started the uncalled-for bashing of Access. You can try to rewrite history, but it's right there for everybody to see, so it's not going to do you any good.
David-W-Fenton
@David: If "ugh you also have to use the designer?" is bashing then I think you are being hyper sensitive. Since it appears that you primarily deal with Access based on info from your site I am not surprised you would be hypersensitive. But the fact remains that the Query Designers are suboptimal for making complex queries. It doesn't matter if your talking access, SSRS or anything else.
JoshBerke
+1  A: 

Something like this:

SELECT * FROM table AS t1
JOIN ( SELECT id, max(count) as Id FROM table GROUP BY id ) AS t2
ON t1.id = t2.id AND t1.id = t2.id

This assumes that no idnum has the same max count or you'll get two idnums

Evan Carroll
A: 

Try this query:

SELECT * FROM my_table
GROUP BY IDNum
HAVING Count = MAX(Count)

It should work on Access, but I didn't test it.

Nick D