I am a beginner in sql and I wanted to know how to get for each id of two joined tables the max of a couple of integer?
what I have done so far:
SELECT
Reduced_T_Rappro_N.[Rente RES] AS Rente_RES,
Reduced_T_Rappro_N.[Montant capital constitutif] AS Montant_Capital_Consitutif_N,
[Reduced_T_Rappro_N - 1].[Montant capital constitutif] AS [ "Montant_Capital_Constitutif_N-1" ],
IIf(
[Reduced_T_Rappro_N - 1].[Montant capital constitutif] = 0,
0,
(
[Reduced_T_Rappro_N].[Montant capital constitutif] - [Reduced_T_Rappro_N - 1].[Montant capital constitutif]
)/ [Reduced_T_Rappro_N - 1].[Montant capital constitutif]
)* 100 AS [ "Evolution capital constitutif" ],
[Reduced_T_Rappro_N].[Montant Sous Rente],
[Reduced_T_Rappro_N - 1].[Montant Sous Rente],
(
IIf(
[Reduced_T_Rappro_N - 1].[Montant Sous Rente] = 0,
0,
(
[Reduced_T_Rappro_N].[Montant Sous Rente] - [Reduced_T_Rappro_N - 1].[Montant Sous Rente]
)/ [Reduced_T_Rappro_N - 1].[Montant Sous Rente]
)
)* 100 AS [ "Evolution montant sous rente" ],
Reduced_T_Rappro_N.[Montant rente initial] AS Montant_Rente_Initial_N,
[Reduced_T_Rappro_N - 1].[Montant rente initial] AS [ "Montant_Rente_Initial_N-1" ],
(
IIf(
[Reduced_T_Rappro_N - 1].[Montant rente initial] = 0,
0,
(
[Reduced_T_Rappro_N].[Montant rente initial] - [Reduced_T_Rappro_N - 1].[Montant rente initial]
)/ [Reduced_T_Rappro_N - 1].[Montant rente initial]
)
)* 100 AS [ "Evolution rente initial" ],
MAX(
[Reduced_T_Rappro_N - 1].[Montant Sous Rente] [Reduced_T_Rappro_N].[Montant Sous Rente]
) AS [ "Addition" ]
FROM
Reduced_T_Rappro_N
INNER JOIN [Reduced_T_Rappro_N - 1] ON (
Reduced_T_Rappro_N.[Rente RES] = [Reduced_T_Rappro_N - 1].[Rente RES]
)
AND (
Reduced_T_Rappro_N.[Société] = [Reduced_T_Rappro_N - 1].[Société]
)
GROUP BY
Reduced_T_Rappro_N.[Rente RES],
Reduced_T_Rappro_N.[Montant capital constitutif],
[Reduced_T_Rappro_N - 1].[Montant capital constitutif],
[Reduced_T_Rappro_N].[Montant Sous Rente],
[Reduced_T_Rappro_N - 1].[Montant Sous Rente],
Reduced_T_Rappro_N.[Montant rente initial],
[Reduced_T_Rappro_N - 1].[Montant rente initial];
The divisions in my query correspond to calculations of evolution in percentage, moreover I use MS ACCESS from where the call of methods iif
Results:
Rente_RES | Montant_Capital_Consitutif_N | Montant_Capital_Consitutif_N-1 | Evolution capital constitutif | Montant Sous Rente | Montant Sous Rente N-1 | Evolution montant sous rente | Montant Rente Initial N | Montant Rente initial N-1 | Evolution rente initial | Addition |
---|---|---|---|---|---|---|---|---|---|---|
00000002-01 | 0 | 0 | 0 | 200,34 | 198,35 | 1,00327703554324 | 195,61 | 195,61 | 0 | 398,69 |
00000002-01 | 0 | 0 | 0 | 200,34 | 200,34 | 0 | 195,61 | 195,61 | 0 | 400,68 |
00000002-01 | 0 | 0 | 0 | 202,34 | 198,35 | 2,0115956642299 | 195,61 | 195,61 | 0 | 400,68 |
00000002-01 | 0 | 0 | 0 | 202,34 | 200,34 | 0,998302885095338 | 195,61 | 195,61 | 0 | 402,68 |
00000002-03 | 25070,68 | 25070,68 | 0 | 1583,2 | 1583,2 | 0 | 1472,18 | 1472,18 | 0 | 3166,4 |
00000003-04 | 0 | 0 | 0 | 1358,5 | 1340,93 | 1,31028465318845 | 1266,7 | 1266,7 | 0 | 2699,43 |
00000003-04 | 0 | 0 | 0 | 1358,5 | 1358,5 | 0 | 1266,7 | 1266,7 | 0 | 2717 |
What I want: Obtain only one result per ID (Rente RES) corresponding to the maximum of the addition between [Montant sous rente N] and [Montant sous rente N-1].
Rente_RES | Montant_Capital_Consitutif_N | Montant_Capital_Consitutif_N-1 | Evolution capital constitutif | Montant Sous Rente | Montant Sous Rente N-1 | Evolution montant sous rente | Montant Rente Initial N | Montant Rente initial N-1 | Evolution rente initial | Addition |
---|---|---|---|---|---|---|---|---|---|---|
00000002-01 | 0 | 0 | 0 | 202,34 | 200,34 | 0,998302885095338 | 195,61 | 195,61 | 0 | 402,68 |
00000002-03 | 25070,68 | 25070,68 | 0 | 1583,2 | 1583,2 | 0 | 1472,18 | 1472,18 | 0 | 3166,4 |
00000003-04 | 0 | 0 | 0 | 1358,5 | 1358,5 | 0 | 1266,7 | 1266,7 | 0 | 2717 |
CodePudding user response:
SELECT Reduced_T_Rappro_N.Rente_RES, Max(([Montant_Capital_Consitutif_N-1] [Montant_Capital_Consitutif_N])) AS Addition FROM Reduced_T_Rappro_N GROUP BY Reduced_T_Rappro_N.Rente_RES;