Sql - 如何在每个用户ID的多行和多列中获取最大值? [英] Sql - how do get max value across multiple rows and multiple columns per user id?

查看:166
本文介绍了Sql - 如何在每个用户ID的多行和多列中获取最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

尝试将查询写入获取最大每个用户ID。

加入我的两个表会产生一个结果 set 其中我需要的值比较是跨多个列和多行。

我要比较的值是 5 列和多行。在每一行中,我有一个列(pname),表示每行具有最高数值的列。我希望只显示具有最高' pname'


我的初始查询和初始结果 set


SELECT
s.id,
s.pname,
s.p1,
s.p2,
s .p3,
s.p4,
s.p5

FROM sdata s
INNER JOIN customer c on c id = s. id

id pname P1 P2 P3 P4 P5
12344 P2 1 9 4 3 4
< span class =co de-digit> 12344 P4 2 5 4 9 5
12344 P3 3 4 9 3 6
5395 P1 9 4 6 4 3
5395 P5 2 1 5 1 9
390 P5 1 4 4 2 9





< b>我尝试过的事情:



SELECT

s.id,

s。 pname,

s.p1,

s.p2,

s.p3,

s.p4,

s.p5



FROMsdatas

INNER JOINcustomerc onc。id=s。id

解决方案

尝试GROUP BY: SQL GROUP BY语句 [ ^ ]作为内部查询返回 id MAX(pname)AS pname ,然后用你的原始表作为 WHERE s.id = grouped.id AND s.pname = grouped.pname


First加入总之,我建议改变表格的设计。不是在列p1 ... p5中存储多个值,而是将它们作为行存储在单独的表中。这将使数据处理变得更加容易。



现在问题是什么,即使你说你在PNAME中有列名我会相信它会更容易将列转换为行,然后找到最大值。



强力解决方案是使用union。请考虑以下事项:

  SELECT  a.id,MAX(a.val)
< span class =code-keyword> FROM (
SELECT sd.id,sd.p1 as val FROM sdata sd UNION ALL
SELECT sd.id,sd.p2 as val FROM sdata sd UNION ALL
SELECT sd.id,sd.p3 as val FROM sdata sd UNION ALL
SELECT sd.id,sd.p4 as val FROM sdata sd UNION ALL
SELECT sd.id ,sd.p5 as val FROM sdata sd) AS a
GROUP BY a.id


Hello,

Trying to write a query to get the max value per user id. 

Joining my two tables produces a result set where the values that I need to compare are across multiple columns and multiple rows.

The values that I am comparing are across 5 columns and multiple rows. And within each row I have a column (pname) that represents the column that has the highest numeric value per row. I am looking to display just the row that has the highest 'pname' value per user id


Below is my initial query and the initial result set it generates.


  SELECT
  s.id,
  s.pname,
  s.p1,
  s.p2,
  s.p3,
  s.p4,
  s.p5

FROM "sdata" "s"
INNER JOIN "customer" "c" on "c"."id" = "s"."id"

id	pname	P1	P2	P3	P4	P5
12344	P2	1	9	4	3	4
12344	P4	2	5	4	9	5
12344	P3	3	4	9	3	6
5395	P1	9	4	6	4	3
5395	P5	2	1	5	1	9
390	P5	1	4	4	2	9



What I have tried:

SELECT
s.id,
s.pname,
s.p1,
s.p2,
s.p3,
s.p4,
s.p5

FROM "sdata" "s"
INNER JOIN "customer" "c" on "c"."id" = "s"."id"

解决方案

Try a GROUP BY: SQL GROUP BY Statement[^] as an inner query to return the id and MAX(pname) AS pname, then JOIN that with your original table as a WHERE s.id = grouped.id AND s.pname = grouped.pname


First of all, I would suggest changing the design of the tables. Instead of storing multiple values in column p1...p5, store them as rows in a separate table. This would make the data handling really much easier.

Now what comes to the query, even though you said that you have the column name in PNAME I would believe that it would be easier to transform the columns to rows and then find the maximum.

A brute force solution would be to use union. Consider the following:

SELECT a.id, MAX(a.val)
FROM (
   SELECT sd.id, sd.p1 as val FROM sdata sd UNION ALL
   SELECT sd.id, sd.p2 as val FROM sdata sd UNION ALL
   SELECT sd.id, sd.p3 as val FROM sdata sd UNION ALL
   SELECT sd.id, sd.p4 as val FROM sdata sd UNION ALL
   SELECT sd.id, sd.p5 as val FROM sdata sd) AS a
GROUP BY a.id


这篇关于Sql - 如何在每个用户ID的多行和多列中获取最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆