Forum Moderators: open

Message Too Old, No Replies

Grouping a field min/max

         

uebers

5:47 pm on Sep 27, 2008 (gmt 0)

10+ Year Member



Hi there,
I do have query result of some joint tables for example:

id name myCode myvalue
1 Peter BBB 20
2 George AAA 30
2 George BBB 80
4 John BBB 90
4 John AAA 20

Additionaly I do only want the lowest value in myvalue column . my only Idea was to group by and put min() around the myvalue. The result was:

id name myCode myvalue
1 Peter BBB 20
2 George AAA 30
4 John BBB 20

As you can see by grouping it the first record of the mycode coulumn was choosen which is wrong or shooudnd.

The Original Query is here:

SELECT cb.cb_type, cb.cb_id, cb.cb_name, s.bil_s_bez,

min(CASE WHEN bks.bil_k_single > bks.bil_k_double
THEN bks.bil_k_double
ELSE bks.bil_k_single
END ) AS lowestprice

FROM bil_coursebook cb,
bil_season s,
(SELECT bil_k_code, bil_k_single, bil_k_double
FROM bil_key
WHERE SUBSTRING( bil_k_code, 1, 2 ) = 'SA') bks

WHERE cb.cb_type = 'U'
AND cb.cb_year = 2008
AND bks.bil_k_code = s.bil_s_bez
AND (cb.cb_from_date BETWEEN s.bil_s_von AND s.bil_s_bis
OR cb.cb_to_date BETWEEN s.bil_s_von AND s.bil_s_bis )

GROUP BY cb.cb_name
ORDER BY cb.cb_id

Any Ideas?

UEBSR