Sql-从oracle Sql中的结果集获取最大值
我试图从结果集中获得产品的最大值,但我只得到一行值。我想显示结果集中每个产品的最大值 请查找结果集:Sql-从oracle Sql中的结果集获取最大值,sql,oracle,oracle11g,Sql,Oracle,Oracle11g,我试图从结果集中获得产品的最大值,但我只得到一行值。我想显示结果集中每个产品的最大值 请查找结果集: |Col1 |col2 |col3| col4 |sum_cnt| +---------+------------+---------+----------+-------+ | 1003| 2018/03| PC | Prod1| 105984| | 1003| 2018/03| PC | Prod2| 3|
|Col1 |col2 |col3| col4 |sum_cnt|
+---------+------------+---------+----------+-------+
| 1003| 2018/03| PC | Prod1| 105984|
| 1003| 2018/03| PC | Prod2| 3|
| 1003| 2018/03| PC | Prod3| 695|
| 1003| 2018/03| PC | Prod4| 8489|
| 1003| 2018/02| PC | Prod1| 101894|
| 1003| 2018/02| PC | Prod4| 7758|
| 1003| 2018/02| PC | Prod3| 780|
| 1003| 2018/02| PC | Prod2| 1|
| 1003| 2018/01| PC | Prod4| 7665|
| 1003| 2018/01| PC | Prod3| 708|
| 1003| 2018/01| PC | Prod2| 5|
| 1003| 2018/01| PC | Prod1| 104557|
| 1003| 2017/12| PC | Prod2| 2|
| 1003| 2017/12| PC | Prod1| 106896|
| 1003| 2017/12| PC | Prod3| 857|
| 1003| 2017/12| PC | Prod4| 8177|
| 1003| 2017/11| PC | Prod2| 1|
| 1003| 2017/11| PC | Prod1| 102664|
| 1003| 2017/11| PC | Prod3| 724|
| 1003| 2017/11| PC | Prod4| 7661|
+---------+------------+---------+----------+-------+
我想显示最新日期的每个产品的最大金额
我希望我的输出为:
|Col1 |col2 |col3| col4 |sum_cnt|
+---------+------------+---------+----------+-------+
| 1003| 2018/03| PC | Prod1| 106896|
| 1003| 2018/03| PC | Prod2| 5|
| 1003| 2018/03| PC | Prod3| 857|
| 1003| 2018/03| PC | Prod4| 8489|
我已尝试在下面的查询中获取数据,但只得到一条记录
代码如下:
select * from tab2 a where sum_cnt = (select max(sum_cnt) from tab2 b where a.col1= b.col1)
请帮助我如何做到这一点
非常感谢您的帮助。我们可以在此处尝试使用
行号
:
SELECT Col1, col2, col3, col4, sum_cnt
FROM
(
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY col1, col4 ORDER BY col2 DESC, sum_cnt DESC) rn
FROM yourTable t
) t
WHERE rn = 1;
这里的逻辑是按产品划分,然后按第一个日期降序排列,以获得最近的日期,然后按计数降序排列,以获得最近日期的最高计数。我们可以尝试使用
行数
这里:
SELECT Col1, col2, col3, col4, sum_cnt
FROM
(
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY col1, col4 ORDER BY col2 DESC, sum_cnt DESC) rn
FROM yourTable t
) t
WHERE rn = 1;
这里的逻辑是按产品划分,然后按第一个日期降序,以获得最近的日期,然后按计数降序,以获得最近日期的最高计数。考虑加入聚合子查询:
select t.col1, t.col2, t.col3, t.col4, agg.max_cnt
from tab2 t
inner join
(
select sub_t.col1, sub_t.col4, max(sub_t.col2) AS max_date,
max(sub_t.sum_cnt) as max_cnt
from tab2 sub_t
group by sub_t.col1, sub_t.col4
) agg
on t.col1 = agg.col1 and t.col4 = agg.col4 and t.col2 = agg.max_date
order by t.col4
或使用CTE:
WITH agg AS
(
select sub_t.col1, sub_t.col4, max(sub_t.col2) AS max_date,
max(sub_t.sum_cnt) as max_cnt
from tab2 sub_t
group by sub_t.col1, sub_t.col4
)
select t.col1, t.col2, t.col3, t.col4, agg.max_cnt
from tab2 t
inner join agg
on t.col1 = agg.col1 and t.col4 = agg.col4 and t.col2 = agg.max_date
order by t.col4
考虑加入聚合子查询:
select t.col1, t.col2, t.col3, t.col4, agg.max_cnt
from tab2 t
inner join
(
select sub_t.col1, sub_t.col4, max(sub_t.col2) AS max_date,
max(sub_t.sum_cnt) as max_cnt
from tab2 sub_t
group by sub_t.col1, sub_t.col4
) agg
on t.col1 = agg.col1 and t.col4 = agg.col4 and t.col2 = agg.max_date
order by t.col4
或使用CTE:
WITH agg AS
(
select sub_t.col1, sub_t.col4, max(sub_t.col2) AS max_date,
max(sub_t.sum_cnt) as max_cnt
from tab2 sub_t
group by sub_t.col1, sub_t.col4
)
select t.col1, t.col2, t.col3, t.col4, agg.max_cnt
from tab2 t
inner join agg
on t.col1 = agg.col1 and t.col4 = agg.col4 and t.col2 = agg.max_date
order by t.col4
@Tim,非常感谢你的帮助。当我尝试对所有产品进行上述查询时,我没有得到最大值。我错过什么了吗?请让我知道。
col1
的意义是什么?您想要每个col1
值的结果吗?您好,col1是指Clientid。是的,蒂姆。“没错。”蒂姆,非常感谢你的帮助。当我尝试对所有产品进行上述查询时,我没有得到最大值。我错过什么了吗?请让我知道。col1
的意义是什么?您想要每个col1
值的结果吗?您好,col1是指Clientid。是的,蒂姆。这是正确的。在所需的输出中,总和的值与日期不一致。例如,带有106896
的记录的col2为2017/12
,而不是2018/03
。在所需的输出中,总和的值与日期不一致。例如,使用106896
记录的col2为2017/12
,而不是2018/03
。非常感谢您的帮助。非常感谢您的帮助。