聚合查询并显示非聚合列的最大值
Aggregate Query and display highest value of non-aggregated columns
你好吗?
我有这个问题:是否可以在 #col1 上使用 GroupBy 聚合查询,然后显示具有最高日期的 #col2 的值?
这个问题我遇到过很多次,我总是通过在我的代码中使用一些逻辑来解决这个问题,但如果可能的话,我很高兴知道。
举个实际例子:
这个查询非常简单:它获取给定人员的所有薪水元素
SELECT dipElem.CodDip,
dipElem.CodElemRetributivo as salElem,
FORMAT(dipElem.DataInizioValidita, 'yyyy-MM-dd') AS datainizio,
CONVERT(DECIMAL(10, 2), dipElem.ElemRetributivo) AS importo
FROM JBADipendElemRetrib dipElem
INNER JOIN JBAElemRetr elemRetr ON elemRetr.CodElemRetributivo =
dipElem.CodElemRetributivo
WHERE dipElem.CodDitta = :CodDitta
AND elemRetr.CodContratto = :CodContratto
AND dipElem.CodDipendente = :CodDipendente
AND dipElem.DBGruppo = 'GG'
ORDER BY dipElem.CodDipendente,
dipElem.CodElemRetributivo ASC,
dipElem.DataInizioValidita ASC;
而return是这样的:
CodDip
salElem
datainizio
importo
9
1
2019-11-04
989.18
9
1
2020-01-01
989.18
9
1
2020-11-01
1201.14
9
1
2021-11-01
1314.19
9
2
2019-11-04
0.00
9
5
2019-11-04
0.00
9
6
2019-11-04
0.00
但是,我想要 return 类似的东西:
按 salElem 分组,其中 importo
最高 datainizio
可能 salElem
.
CodDip
salElem
datainizio
importo
9
1
2021-11-01
1314.19
9
2
2019-11-04
0.00
9
5
2019-11-04
0.00
9
6
2019-11-04
0.00
这是一个通用的 greatest-n-per-group 查询。在 MySQL 8 或更高版本中,您可以使用 row_number
window 函数:
with cte as (
select t.*, row_number() over (partition by salElem order by datainizio desc) as rn
from t
)
select *
from t
where rn = 1
为了结束这个问题并遵循 Salman A 的回答,这里是解决问题的代码(基于我的问题):
WITH tmpTable AS (
SELECT dipElem.CodElemRetributivo,
FORMAT(dipElem.DataInizioValidita, 'yyyy-MM-dd') AS datainizio,
CONVERT(DECIMAL(10, 2), dipElem.ElemRetributivo) AS importo,
ROW_NUMBER() OVER (PARTITION BY dipElem.CodElemRetributivo
ORDER BY dipElem.DataInizioValidita DESC) AS my_rank
FROM JBADipendElemRetrib dipElem
INNER JOIN JBAElemRetr elemRetr ON elemRetr.CodElemRetributivo = dipElem.CodElemRetributivo
WHERE dipElem.CodDitta = :CodDitta
AND elemRetr.CodContratto = :CodContratto
AND dipElem.CodDipendente = :CodDipendente
AND dipElem.DBGruppo = 'GG'
)
SELECT *
FROM tmpTable
WHERE my_rank = 1;
你好吗? 我有这个问题:是否可以在 #col1 上使用 GroupBy 聚合查询,然后显示具有最高日期的 #col2 的值?
这个问题我遇到过很多次,我总是通过在我的代码中使用一些逻辑来解决这个问题,但如果可能的话,我很高兴知道。
举个实际例子: 这个查询非常简单:它获取给定人员的所有薪水元素
SELECT dipElem.CodDip,
dipElem.CodElemRetributivo as salElem,
FORMAT(dipElem.DataInizioValidita, 'yyyy-MM-dd') AS datainizio,
CONVERT(DECIMAL(10, 2), dipElem.ElemRetributivo) AS importo
FROM JBADipendElemRetrib dipElem
INNER JOIN JBAElemRetr elemRetr ON elemRetr.CodElemRetributivo =
dipElem.CodElemRetributivo
WHERE dipElem.CodDitta = :CodDitta
AND elemRetr.CodContratto = :CodContratto
AND dipElem.CodDipendente = :CodDipendente
AND dipElem.DBGruppo = 'GG'
ORDER BY dipElem.CodDipendente,
dipElem.CodElemRetributivo ASC,
dipElem.DataInizioValidita ASC;
而return是这样的:
CodDip | salElem | datainizio | importo |
---|---|---|---|
9 | 1 | 2019-11-04 | 989.18 |
9 | 1 | 2020-01-01 | 989.18 |
9 | 1 | 2020-11-01 | 1201.14 |
9 | 1 | 2021-11-01 | 1314.19 |
9 | 2 | 2019-11-04 | 0.00 |
9 | 5 | 2019-11-04 | 0.00 |
9 | 6 | 2019-11-04 | 0.00 |
但是,我想要 return 类似的东西:
按 salElem 分组,其中 importo
最高 datainizio
可能 salElem
.
CodDip | salElem | datainizio | importo |
---|---|---|---|
9 | 1 | 2021-11-01 | 1314.19 |
9 | 2 | 2019-11-04 | 0.00 |
9 | 5 | 2019-11-04 | 0.00 |
9 | 6 | 2019-11-04 | 0.00 |
这是一个通用的 greatest-n-per-group 查询。在 MySQL 8 或更高版本中,您可以使用 row_number
window 函数:
with cte as (
select t.*, row_number() over (partition by salElem order by datainizio desc) as rn
from t
)
select *
from t
where rn = 1
为了结束这个问题并遵循 Salman A 的回答,这里是解决问题的代码(基于我的问题):
WITH tmpTable AS (
SELECT dipElem.CodElemRetributivo,
FORMAT(dipElem.DataInizioValidita, 'yyyy-MM-dd') AS datainizio,
CONVERT(DECIMAL(10, 2), dipElem.ElemRetributivo) AS importo,
ROW_NUMBER() OVER (PARTITION BY dipElem.CodElemRetributivo
ORDER BY dipElem.DataInizioValidita DESC) AS my_rank
FROM JBADipendElemRetrib dipElem
INNER JOIN JBAElemRetr elemRetr ON elemRetr.CodElemRetributivo = dipElem.CodElemRetributivo
WHERE dipElem.CodDitta = :CodDitta
AND elemRetr.CodContratto = :CodContratto
AND dipElem.CodDipendente = :CodDipendente
AND dipElem.DBGruppo = 'GG'
)
SELECT *
FROM tmpTable
WHERE my_rank = 1;