简体   繁体   English

SQL查询以使用最大聚合选择行并将它们放在一起

[英]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. 计数的值是正确的,即该特定key_id的计数的最大值,但是key_value仅对于第一个key_id正确,对于其他两个重复。 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). 有人可以告诉我如何更改此查询,以便从具有相应key_id和max(count)的行中获取key_value。

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. 您遇到IO错误。 This could be because of the isnull should be ifnull in SQLITE. 这可能是因为在SQLITE中,isnull应该为ifnull。 sorry for that. 对此表示抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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