简体   繁体   中英

SQL query to select rows using max aggregate and putting them together

I have a table in which the first column is the key value. I have to select one row for each of the key values (select the one with maximum "count" field ) and then arrange them adjacent to each other. I wrote this query for this purpose:

SELECT name as "name",
MAX(CASE WHEN key_id=8 THEN count ELSE 0.0 END) AS "key_1",
key_value as "key_1_value",
MAX(CASE WHEN key_id=9 THEN count ELSE 0.0 END) AS "key_2",
key_value as "key_2_value",
MAX(CASE WHEN key_id=10 THEN count ELSE 0.0 END) AS "key_3",
key_value as "key_2_value"
FROM table1 GROUP BY  name;

The result I get is :

name1      281000018371    0.881841        247000421624    0.881841        285000032094    0.881841

The values for the count is correct ie the maximum value of count for that particular key_id but the key_value is correct only for the first key_id which is repeated for the other two. Can someone please tell me how to change this query so that I get the key_value from the row with corresponding key_id and max(count).

Is this what you're trying to do?

SELECT 
    name as "name",
    MAX(CASE WHEN key_id=8 THEN count ELSE 0.0 END) AS "key_1",
    MAX(CASE WHEN key_id=8 THEN key_value END) AS "key_1_value",
    MAX(CASE WHEN key_id=9 THEN count ELSE 0.0 END) AS "key_2",
    MAX(CASE WHEN key_id=9 THEN key_value END) AS "key_2_value",
    MAX(CASE WHEN key_id=10 THEN count ELSE 0.0 END) AS "key_3",
    MAX(CASE WHEN key_id=10 THEN key_value END) AS "key_3_value"
FROM table1 
GROUP BY  name;

Without sample data, I'm assuming that you're looking at multiple rows and trying to get certain keys onto columns. Your original query is pulling the same value for every column.

EDIT: Performance would be horrible on this if you have any major amounts of data, but you could try doing this ...

SELECT 
    name as "name",
    MAX(CASE WHEN key_id=8 THEN count ELSE 0.0 END) AS "key_1",
    (SELECT TOP 1 key_value FROM Table1 k1 WHERE k1.name = Table1.name AND key_id = 8 ORDER BY Count DESC) as "key_1_value",
    MAX(CASE WHEN key_id=9 THEN count ELSE 0.0 END) AS "key_2",
    (SELECT TOP 1 key_value FROM Table1 k2 WHERE k2.name = Table1.name AND key_id = 9 ORDER BY Count DESC) as "key_2_value",
    MAX(CASE WHEN key_id=10 THEN count ELSE 0.0 END) AS "key_3",
    (SELECT TOP 1 key_value FROM Table1 k3 WHERE k3.name = Table1.name AND key_id = 10 ORDER BY Count DESC) as "key_3_value"
FROM table1 
GROUP BY  name;

Hmm...maybe:

SELECT distinct name, b.MaxCount, b.Key_Value, c.MaxCount, c.Key_Value, d.MaxCount, d.Key_Value
from table1 a
left join 
(SELECT key_Value, MAX(ifnull((count,0)) as MaxCount from table1 where key_id =8 group by key_Value) b
on a.name = b.name
left join 
(SELECT key_Value, MAX(ifnull((count,0)) as MaxCount from table1 where key_id =9 group by key_Value) c
on a.name = b.name
left join 
(SELECT key_Value, MAX(ifnull((count,0)) as MaxCount from table1 where key_id =10 group by key_Value) d
on a.name = b.name
group by name

I assume your join condition.

You got an IO error. This could be because of the isnull should be ifnull in SQLITE. sorry for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM