Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using MAX (I think...)

Status
Not open for further replies.

azzazzello

Technical User
Jan 25, 2005
297
US
quick question, guys

I have a table with, say, 3 values

DATE, COMP_VAL, VALUE

COMP_VAL and VALUE are integer fields and DATE is date

I want to select VALUE where COMP_VAL is the maximum integer for a given set of ranges. How do I do this?
 
Something like this...

SELECT Date,Comp_Value,Value FROM mytable A
Where A.Comp_Value In (Select Top 1 Comp_Value From mytable B Where A.Value=B.Value Order by Comp_Value DESC)

-DNG
 
yea...that would be nice...but by DB is 4.0.8...So no nested selects :(
 
how about this:

SELECT A.Date, A.Comp_Value, A.Value
FROM mytable A INNER JOIN (
SELECT Date, Max(Comp_Value) AS Max_Comp FROM MyTable B
) AS M ON A.Value = M.Value

not tested...just tweak it...

-DNG
 
any and all date ranges, rudy.

In fact, you can ignore the DATE field. I just need the value of VALUE where COMP_VAL is the maximum of all the records in the table
 
I just need the value of VALUE where COMP_VAL is the maximum of all the records in the table
Code:
select t1.COMP_VAL
     , t1.VALUE
  from daTable as t1
cross
  join daTable as t2
group
    by t1.COMP_VAL
     , t1.VALUE
having t1.COMP_VAL = max(t2.COMP_VAL)

r937.com | rudy.ca
 
thanks rudy,

unfortunately, this is another case of me being too general for my own good :(. I was hoping I could extend the answer figure out the rest myself, but I cannot :(. Here is the situation:

I have 2 tables

One of the consists of
ID, attempts, goals

the other consists of

ID, COMP_VALUE, VALUE

The tables are tied by ID

The first table has multiple entries per ID

The second table can have multiple entries per ID.

I need to show a sum of goals, sum of attempts, and the VALUE (from second table) for every ID in the first table
where COMP_VAL is the greatest

so I have this...

Code:
Select t1.ID, sum(t1.ATTEMPTS), sum(t1.GOALS), t2.VALUE FROM 
 t1, t2 WHERE t1.ID = t2.ID GROUP BY t1.ID;

and I am not sure how to integrate what you suggested, rudy, into this mess... I tried sticking some sort of a MAX comparison into a HAVING clause, but it's not jiving...

I really appreciate the help
 
Code:
TABLE 1 

ID | Attempts | Goals

1  |   11     |   5
1  |   9      |   8
2  |   3      |   1
2  |   7      |   4

TABLE 2

ID | COMP_VAL |   VAL

1  |    4     |   5
1  |    3     |   6
1  |    9     |   15
2  |    1     |   7
2  |    3     |   4

The output should be

Code:
ID |  SUM(attempts) | SUM (goals) | VALUE

1  |   20           |    13       |   15
2  |   10           |     5       |   4
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top