我有这个查询:
select ROW_NUMBER() OVER ( PARTITION BY codtiers ORDER BY case when isnull(cumulapport, 0) > isnull(cumulretrait, 0) then cumulapport
else cumulretrait
end DESC ) AS classement,
codTiers
from dbo.TiersComptesLocal
group by codTiers
返回此结果:
classement | codTiers
-------------------------
1 | 1XXXXX
2 | 1XXXXX
1 | 1YYXXX
2 | 1YYXXX
3 | 1YYXXX
1 | 1XXXYY
我想显示每个分区的第一行,我想得到这样的结果:
classement | codTiers
-------------------------
1 | 1XXXXX
1 | 1YYXXX
1 | 1XXXYY
任何想法将不胜感激。
这是我的完整查询:
select ROW_NUMBER() OVER ( PARTITION BY tcl_codtiers ORDER BY case when isnull(cumulapport, 0) > isnull(cumulretrait, 0) then cumulapport
else cumulretrait
end DESC ) AS classement,
TCL_CodTiers,
'le montant max des ' + Senscumul + ' ( '
+ convert(varchar(50), '') + ' € ) du tiers titulaire '
+ TCL_CodTiers + ' ( ' + ''
+ ' ) est supérieur au seuil fixé dans l''alerte : '
+ '75000'
from dbo.CLI_TCL_TiersComptesLocal
INNER JOIN dbo.CLI_GCO_GeneriquesComptes ON TCL_CodTiers = GCO_CodTiersPrincipal
AND TCL_NumLien IN ( 0, 1 )
inner join ( select TCL_CodTiers CodTiers,
sum(case when ESO_MntValoRetenuEnEuros > 0
then ESO_MntValoRetenuEnEuros
else 0
end) cumulapport,
sum(case when ESO_MntValoRetenuEnEuros < 0
then abs(ESO_MntValoRetenuEnEuros)
else 0
end) cumulretrait,
case when isnull( sum(case when ESO_MntValoRetenuEnEuros > 0
then ESO_MntValoRetenuEnEuros
else 0
end), 0) > isnull( sum(case when ESO_MntValoRetenuEnEuros < 0
then abs(ESO_MntValoRetenuEnEuros)
else 0
end), 0)
then 'Apports'
else 'Retraits'
end as Senscumul
from CRO_ESO_EntreeSortie
inner join CLI_TCL_TiersComptesLocal on TCL_CodCompte = ESO_CodCompte
and TCL_NumLien in ( 0, 1 )
group by TCL_CodTiers
having sum(case when ESO_MntValoRetenuEnEuros > 0
then ESO_MntValoRetenuEnEuros
else 0
end) > 75000
or sum(case when ESO_MntValoRetenuEnEuros < 0
then abs(ESO_MntValoRetenuEnEuros)
else 0
end) > 75000
) surveillance on GCO_CodTiersPrincipal = CodTiers
提前致谢。