473,412 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

newbie? SQL question

I have a table, called X, that looks like so.

key cpny val (plus other columns not shown)
1 A 1
2 B 3
3 C 4
4 A 7
5 B 9
6 C 2

I want to select the maximum of the val column for each
cpny, but I also want the key for that row.

If I didn't want the key, it would be trivial, it's just

select cpny,max(val) from X group by cpny;

And that gives me

A 7
B 9
C 4

But what I want is to include the key column.

4 A 7
5 B 9
3 C 4

Now I've managed to patch together a solution in terms of sub
queries, basically using the first query and then selecting rows
that have those values. Is there an easier way? My actual case
involves dates and a link to another table for the value, and
so on, so it winds up being a half page of gnarly SQL.
Socks
Jun 27 '08 #1
2 1050
bdj
try this

select *
from X, (select cpny as Gcpny, max(val) as Gmax from X group by cpny) T
where X.cpny = T.Gcpny
and X.val = T.Gmax
;

/Bjoern

<pu*********@hotmail.comskrev i en meddelelse
news:c7**************************@posting.google.c om...
I have a table, called X, that looks like so.

key cpny val (plus other columns not shown)
1 A 1
2 B 3
3 C 4
4 A 7
5 B 9
6 C 2

I want to select the maximum of the val column for each
cpny, but I also want the key for that row.

If I didn't want the key, it would be trivial, it's just

select cpny,max(val) from X group by cpny;

And that gives me

A 7
B 9
C 4

But what I want is to include the key column.

4 A 7
5 B 9
3 C 4

Now I've managed to patch together a solution in terms of sub
queries, basically using the first query and then selecting rows
that have those values. Is there an easier way? My actual case
involves dates and a link to another table for the value, and
so on, so it winds up being a half page of gnarly SQL.
Socks

Jun 27 '08 #2
"bdj" <B.********@gmx.netwrote in message news:<40*********************@dread11.news.tele.dk >...
try this

select *
from X, (select cpny as Gcpny, max(val) as Gmax from X group by cpny) T
where X.cpny = T.Gcpny
and X.val = T.Gmax
;
Yes thanks, that's equivalent to what I did. But the problem
was, the val column was actually a date range not a simple
value, and I needed the maximum date before the current working
date. And the dates were actually in another table linked by
the key. And etc. etc. whine whine. So what happened was, the
subquery got messy. It works, but it gets big enough that every
time I need to modify it I have to drink about 5 cups of coffee
to be alert enough to understand it again. Wound up being about
40 lines of SQL. Oh well, not a record I suppose.

I was hoping there was something like a "correspond" command
that could be used with the group by. Something like

select correspond(key),cpny,max(val) from X group by cpny;

and the correspond would pick out the key value that corresponded
with the max val value. But it looks like the subquery is the only
way to go.

But thanks for the help.
Socks
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Elger | last post by:
Dear Members, A newbie question How do I convert this XML example into HTML (using XSLT)? <DOCUMENT> <PARA> This <BOLD>is</BOLD> a <BOLD>test</BOLD> </PARA> </DOCUMENT>
4
by: DragonLord | last post by:
I have a custom user control that i am trying to understand and it has numbers before the line items. So here it comes the real newbie question What the heck are the numbers for?? 205: For...
5
by: kamikaze04 | last post by:
Hello. I have a very newbie question about Streams. The situation is that i have a function (that i cannot modify it's definition/call): public void F1(istream & in){ while( ...) { ...
5
by: Banibrata Dutta | last post by:
Hi, I've gone through the list of "language differences" between 2.3 / 2.4 & 2.5 of CPython. I've spend around 2 weeks now, learning v2.5 of CPython, and I consider myself still very very...
16
by: Raxit | last post by:
Hi, i was reading/learning some hello world program in python. I think its very simillar to Java/C++/C#. What's different (except syntax) ? what can i do easily with python which is not easy...
7
by: idiolect | last post by:
Hi all - Sorry to plague you with another newbie question from a lurker. Hopefully, this will be simple. I have a list full of RGB pixel values read from an image. I want to test each RGB band...
5
by: Randall | last post by:
I am a newbie trying to learn the DOM. Can someone tell me why the first alert statement returns null, and the second returns the value 33px (which was set using the style="top:33px;" in the DIV...
12
by: Philipp.Weissenbacher | last post by:
Hi all! This is most certainly a total newbie question, but why doesn't the following code cause a segfault? void insertion_sort(int a, int length) { int i; for (i=0; i < length; i++) { int...
3
Lokean
by: Lokean | last post by:
Sorry for this newbie question, this is not my realm of expertese. I have searched google, tried several applications that claim they can do this, such as Mapforce, which I found confusing, to...
5
by: Dave | last post by:
I am new to Visual Web Developer 2005 Expres. I am using absolute positioning and every time I add a button control to my web form its width extends all the way to the edge of the page. IOW I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.