473,545 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[SQL] Problem with group by and max

Hi NG,

I have problem... I'm currently using UDB v8.1 for Linux.

Here is the table "test":

ID1 ID2 ID3 VALUE
-----------------
1 0 1 23
1 0 2 9
1 1 1 34
1 1 2 12
2 0 1 56
2 0 2 13
2 2 1 98
2 2 2 24

For each id1 the query should return the biggest id2 and calculate the sum
of VALUES over all id3.

This would be the result:

ID1 ID2 SUM
-----------
1 1 46
2 2 122

For id1=1 is id2=1 the biggest id2 value. The sum is 34+12=46.
For id1=2 is id2=2 the biggest id2 value. The sum is 98+24=122.

I tried it with this query:

select t1.id1, t1.id2, sum(t1.value)
from test t1
group by t1.id1
having t1.id2=(select max(t2.id2) from test t2 where t1.id1=t2.id2)

But I failed...

I do appreciate everyone's help!

S.B.
Nov 12 '05 #1
4 10917
Test this / Probier mal das

select t1.id1
, t1.id2
, sum ( t1.value ) as value

from test as t1

where ( t1.id1, t1.id2 ) in ( select t2.id1
, max ( t2.id2 ) as id2
from test as t2
group by t2.id1
)

group by t1.id1
, t1.id2
;

Regards / Gruß

----------------------------------

Stefan M. Mihokovic

email: st***@stemi.de

----------------------------------
"Stefan Bauer" <st***********@ yahoo.de> schrieb im Newsbeitrag
news:bo******** *****@ID-209925.news.uni-berlin.de...
Hi NG,

I have problem... I'm currently using UDB v8.1 for Linux.

Here is the table "test":

ID1 ID2 ID3 VALUE
-----------------
1 0 1 23
1 0 2 9
1 1 1 34
1 1 2 12
2 0 1 56
2 0 2 13
2 2 1 98
2 2 2 24

For each id1 the query should return the biggest id2 and calculate the sum
of VALUES over all id3.

This would be the result:

ID1 ID2 SUM
-----------
1 1 46
2 2 122

For id1=1 is id2=1 the biggest id2 value. The sum is 34+12=46.
For id1=2 is id2=2 the biggest id2 value. The sum is 98+24=122.

I tried it with this query:

select t1.id1, t1.id2, sum(t1.value)
from test t1
group by t1.id1
having t1.id2=(select max(t2.id2) from test t2 where t1.id1=t2.id2)

But I failed...

I do appreciate everyone's help!

S.B.

Nov 12 '05 #2
You can also try either one of these two statements:

with maxval as
(
select id1, max(id2) as id2
from test
group by id1
)
select maxval.id1, maxval.id2, sum(value)
from maxval, test
where test.id1 = maxval.id1
and test.id2 = maxval.id2
group by maxval.id1, maxval.id2;

or

select maxval.id1, maxval.id2, sum(value)
from test, (select id1, max(id2) as id2
from test
group by id1) as maxval
where test.id1 = maxval.id1
and test.id2 = maxval.id2
group by maxval.id1, maxval.id2;

Enjoy,
Mauro.
"Stefan M. Mihokovic" <ne**@stemi.d e> wrote in message
news:bo******** *****@news.t-online.com...
Test this / Probier mal das

select t1.id1
, t1.id2
, sum ( t1.value ) as value

from test as t1

where ( t1.id1, t1.id2 ) in ( select t2.id1
, max ( t2.id2 ) as id2
from test as t2
group by t2.id1
)

group by t1.id1
, t1.id2
;

Regards / Gruß

----------------------------------

Stefan M. Mihokovic

email: st***@stemi.de

----------------------------------
"Stefan Bauer" <st***********@ yahoo.de> schrieb im Newsbeitrag
news:bo******** *****@ID-209925.news.uni-berlin.de...
Hi NG,

I have problem... I'm currently using UDB v8.1 for Linux.

Here is the table "test":

ID1 ID2 ID3 VALUE
-----------------
1 0 1 23
1 0 2 9
1 1 1 34
1 1 2 12
2 0 1 56
2 0 2 13
2 2 1 98
2 2 2 24

For each id1 the query should return the biggest id2 and calculate the sum of VALUES over all id3.

This would be the result:

ID1 ID2 SUM
-----------
1 1 46
2 2 122

For id1=1 is id2=1 the biggest id2 value. The sum is 34+12=46.
For id1=2 is id2=2 the biggest id2 value. The sum is 98+24=122.

I tried it with this query:

select t1.id1, t1.id2, sum(t1.value)
from test t1
group by t1.id1
having t1.id2=(select max(t2.id2) from test t2 where t1.id1=t2.id2)

But I failed...

I do appreciate everyone's help!

S.B.


Nov 12 '05 #3
Other statements:
SELECT id1 , id2 , SUM(value) AS sum
FROM test T1
WHERE T1.id2 = (SELECT MAX(id2)
FROM test T2
WHERE T1.id1 = T2.id1
)
GROUP BY id1 , id2
;

and

SELECT id1 , id2 , SUM(value) AS sum
FROM (SELECT id1 , id2 , value
, DENSE_RANK() OVER(PARTITION BY id1 ORDER BY id2 desc) as drank
FROM test
) T
WHERE drank = 1
GROUP BY id1 , id2
;
Nov 12 '05 #4
Some more statements.
Just my cuoriosity.
SELECT id1 , id2 , SUM(value) AS sum
FROM test T1
WHERE NOT EXISTS
(SELECT *
FROM test T2
WHERE T1.id1 = T2.id1
AND T1.id2 < T2.id2
)
GROUP BY id1 , id2
;

and

SELECT T1.id1 , T1.id2 , SUM(T1.value) AS sum
FROM test T1
LEFT OUTER JOIN
test T2
ON T1.id1 = T2.id1
AND T1.id2 < T2.id2
WHERE T2.id2 IS NULL
GROUP BY T1.id1 , T1.id2
;

and

SELECT id1 , id2 , sum_value
FROM (SELECT id1 , id2 , sum_value
, ROWNUMBER() OVER(PARTITION BY id1 ORDER BY id2 desc) AS rownum
FROM (SELECT id1 , id2 , SUM(value) AS sum_value
FROM test
GROUP BY id1 , id2
) AS X
) AS Y
WHERE rownum = 1
;

(correction(or addition) to my previous post)
RANK() can be used instead of DENSE_RANK().
SELECT id1 , id2 , SUM(value) AS sum
FROM (SELECT id1 , id2 , value
, RANK() OVER(PARTITION BY id1 ORDER BY id2 desc) as rank
FROM test
) T
WHERE rank = 1
GROUP BY id1 , id2
;
Nov 12 '05 #5

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

Similar topics

3
17354
by: Robby McGehee | last post by:
I need this to work: SELECT FROM WITH (NOLOCK) where ='a' GROUP BY , HAVING COUNT () > 1 The problem is that I get an error that needs to be in the GROUP BY clause or aggregate function. if I put it in there, I will get no duplicates (because it is the identity field). The whole point of this is to find dups. Thanks for any help.
2
15210
by: Ken Lindner | last post by:
I have a need to become familiar with SQL Server 2000 for work. Needless to say I am new to SQL Server any version, but not IT in general. My employer has provided me with the SQL Server 2000 Personal disk from the SQL Server 2000 Enterprise kit as this is reported here on the MSDN web site to be the version that is supported on Windows XP. ...
5
2723
by: Grim Reaper | last post by:
My work let me put SQL Server 7.0 Enterprise Edition on my laptop. I have never setup a server from the beginning, so I am a little new at creating server groups. Alright, I am trying to create a server group. I right click on the "SQL Server Group" and make a name of "TEST" and put in the subgroup of "SQL Server Group". Next, I try to...
10
2130
by: Blake | last post by:
I have created an Access2K front end application that connects to a SQLServer2K backend. I use this vba code to create the connection from the Access app: Dim strConnect As String 'make sure all previous connections are closed: CurrentProject.OpenConnection "Provider=" 'create new connection string to server: strConnect =...
3
8093
by: Memduh Durmaz | last post by:
Hi, I'm using DB2 UDB 7.2. Also I'm doing some tests on SQL Server 2000 for some statements to use efectively. I didn't find any solution on Sql Server about WITH ... SELECT structure of DB2. Is there any basic structure on Sql Server like WITH ... SELECT structure?
4
28814
by: Chad Richardson | last post by:
I've always been mistified why you can't use a column alias in the group by clause (i.e. you have to re-iterate the entire expression in the group by clause after having already done it once in the select statement). I'm mostly a SQL hobbiest, so it's possible that I am not doing this in the most efficient manner. Anyone care to comment on...
14
5415
by: Serge Rielau | last post by:
Shameless self promotion ;-) http://www-106.ibm.com/developerworks/db2/library/techarticle/dm-0501rielau/ I've now reached the end of my "mini-series" around SQL Procedures. If anyone has ideas on SQL related topics that need illuination I'm all ears. MERGE has come up a couple times so far.... Anything else? Cheers
6
2017
by: Richard Holliingsworth | last post by:
Hello: Thanks for reading this. I would like to build a report (Access 2002) that uses SQL queries to count records in specific groups. I've tried several methods and so far no luck. Could someone please point me in the right direction on this one. It
24
19874
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every renewal in the history of the policyholder. The information is in 2 tables, policy and customer, which share the custid data. The polno changes...
9
3298
by: billmiami2 | last post by:
I was playing around with the new SQL 2005 CLR functionality and remembered this discussion that I had with Erland Sommarskog concerning performance of scalar UDFs some time ago (See "Calling sp_oa* in function" in this newsgroup). In that discussion, Erland made the following comment about UDFs in SQL 2005: >>The good news is that in SQL...
0
7470
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7659
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7811
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7428
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7760
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5975
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4949
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1019
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
709
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.