获取具有分组SQL结果的最大总和值的记录

时间:2016-06-21 09:20:59

标签: mysql sql-server

我有一个包含三个字段的表:

PK     account     value
-----------------------
1      40010101    130
1      40010101    500
1      40010569    590
2      40010569    300
2      40010101    200

我想选择每个PK的值总和最高值的记录,按PK和Account分组,所以我的结果应该是这样的:

PK     account     value
-----------------------
1      40010101    630
2      40010569    300

我尝试了这个解决方案:

SELECT
 pn.*
FROM
 (select PK, account, sum(value) as value
 from table
 group by PK, account) pn
INNER JOIN 
 (select PK, MAX(value) AS maxvalue
 from (select PK, account, sum(value) as value
      from table
      group by PK, account) pn 
 group by PK) maxsum
ON pn.PK= maxsum.PK
AND pn.value = maxsum.maxvalue

它有效,但我问你一个更快的解决方案。我可以有数千条记录来匹配。我无法使用存储过程。 使用SQL服务器。

8 个答案:

答案 0 :(得分:2)

在SQL Server中

SELECT  PK,
        account, 
        sum_value AS [value]
FROM (
    SELECT  PK,
            account, 
            SUM([value]) as sum_value,
            ROW_NUMBER() OVER (PARTITION BY PK ORDER BY SUM([value]) DESC) as rn
    FROM [table]
    GROUP BY account, PK
) as p
WHERE rn = 1

输出:

PK  account     value
1   40010101    630
2   40010569    300

答案 1 :(得分:1)

实现目标的一种方法是使用一系列连接:

SELECT t1.PK, t1.account, t1.value
FROM
(
    SELECT PK, account, SUM(value) AS value
    FROM yourTable
    GROUP BY PK, account
) t1
INNER JOIN
(
    SELECT t.PK, MAX(t.value) AS value
    FROM
    (
        SELECT PK, account, SUM(value) AS value
        FROM yourTable
        GROUP BY PK, account
    ) t
    GROUP BY t.PK
) t2
    ON t1.PK = t2.PK AND t1.value = t2.value

点击以下链接查看正在运行的演示:

SQLFiddle

答案 2 :(得分:1)

你可以尝试以下查询它将起作用

SELECT  pk,
        SUBSTRING_INDEX(GROUP_CONCAT(account ORDER BY val DESC),',',1) AS account,
        MAX(val) AS val 
FROM (
    SELECT  pk,
            account,
            SUM(`value`) AS val  
    FROM test.temp_69 
    GROUP BY pk,account 
    ORDER BY pk
    )  AS t 
GROUP BY pk;

答案 3 :(得分:0)

SELECT C.* FROM 
(
    SELECT PK,account,SUM(VALUE) AS VALUE
    FROM TABLE1
    GROUP BY PK,account
) AS C INNER JOIN 
(
    SELECT A.PK, MAX(SUMVALUE) AS MAXVALUE
    FROM TABLE1 AS A INNER JOIN 
    (
        SELECT PK,account,SUM(VALUE) AS SUMVALUE
        FROM TABLE1
        GROUP BY PK,account

    ) AS B
    ON A.PK = B.PK
    AND A.account = B.account
    GROUP BY A.PK
) AS D
ON C.PK =D.PK
AND C.VALUE = D.MAXVALUE

答案 4 :(得分:0)

它们本质上是相互对应的(实际上这是一些数据库在引擎盖下实现DISTINCT的方式)。

如果其中一个更快,它将成为DISTINCT。这是因为,虽然两者是相同的,但查询优化器必须注意到您的GROUP BY没有利用任何组成员,只有他们的密钥。 DISTINCT使这个显式化,所以你可以使用稍微笨拙的优化器。

如有疑问,请测试

答案 5 :(得分:0)

使用子查询

这样做
select id,account,max(amount) as value
from 
(
select id,account,sum(value) as amount
from temp_max
group by account,id
order by id
)t
group by id

答案 6 :(得分:0)

你的解决方案绝对是正确的,易于理解,它也不是一个很糟糕的解决方案,我不知道是否有更好的解决方案可以得到你想要的东西,我提供给你的只是尽我所能为了使查询更简单,无论如何都要尝试。

select t.*, if(@pk = `PK`, @rowno := @rowno + 1, @rowno := 1) as vrowno, @pk := `PK` as vpk
from (
    select `PK`, `account`, sum(`value`) as `value`
    from table1
    group by `PK`, `account`
    order by `PK`, `value` desc) t
cross join (select @pk := '', @rowno := 0) v
having vrowno = 1

SQLFiddle DEMO HERE

希望这会对你有所帮助。 : - )

答案 7 :(得分:0)

我通过子查询得到另一个答案,并在mysql中限制1:

select PK, account, 
  (select Sum(value) FROM yourTable t2 where t1.PK=t2.PK group by PK,account order by Sum(value) desc limit 1) max_sum
from yourTable t1
where (PK, account)=
  (select PK,account FROM yourTable t2 where t1.PK=t2.PK group by PK,account order by Sum(value) desc limit 1) 
group by PK, account;

我不知道它的表现是好还是不好。请检查其表现并通知我们。

我也提供了Sqlfiddle链接。