SELECT ProjectName = p.Code ,LotName = lp.Nom , PhaseActive = ph.Nom ,Item = l.Code, Value = c.Note, Cycle = cdv.Nom, Pole = lp.Pole, Note = lp.Note, idEdition = lp.IdEdition, c.DateCreation
FROM Controles c
Join LotProjets lp ON c.IdLotProjet = lp.IdLotProjet
JOIN Projets p ON lp.IdProjet = p.IdProjet
JOIN Referentiels r ON c.IdReferentiel = r.IdReferentiel
JOIN Livrables l ON r.LivrableId = l.IdLivrable
JOIN Phases ph ON lp.IdPhase = ph.IdPhase
JOIN CycleDeVies cdv ON ph.CycleDeVieId = cdv.IdCycleDeVie
WHERE lp.IdEdition = 3 AND c.DateSuppression IS NULL
其中给出以下结果
# | ProjectName | LotName | PhaseActive | Item | Value | Cycle | Pole | Note | idEdition | DateCreation
1 | P00935 | 06 | Fin de cycle | CVAI | 0 | Autre | TRV | 47 | 3 | 2016-12-21 13:42:54.867
2 | P00935 | 06 | Fin de cycle | DCN-T | 100 | Autre | TRV | 47 | 3 | 2016-12-21 13:42:55.500
3 | P00935 | 06 | Fin de cycle | PTA | 33 | Autre | TRV | 47 | 3 | 2016-12-21 13:42:58.493
4 | P00935 | 06 | Fin de cycle | DPE | 100 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:00.903
5 | P00935 | 06 | Fin de cycle | BTA | 98 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:01.617
6 | P00935 | 06 | Fin de cycle | FPH | 72 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:06.987
7 | P00935 | 06 | Fin de cycle | QSE | 0 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:10.013
8 | P00935 | 06 | Fin de cycle | DPE | 0 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:15.313
9 | P00935 | 06 | Fin de cycle | DPE | 98 | Autre | TRV | 47 | 3 | 2017-01-11 12:13:44.660
10 | P00935 | 06 | Fin de cycle | DPE | 77 | Autre | TRV | 47 | 3 | 2017-01-11 12:16:32.557
11 | P00935 | 05 | Fin de cycle | DPE | 75 | Autre | TRV | 47 | 3 | 2017-01-11 12:16:32.557
对于每个LotName中的每个项目,我希望最近的值具有以下结果。
# | ProjectName| LotName | PhaseActive | Item | Value | Cycle | Pole | Note | idEdition | DateCreation
1 | P00935 | 06 | Fin de cycle | CVAI | 0 | Autre | TRV | 47 | 3 | 2016-12-21 13:42:54.867
2 | P00935 | 06 | Fin de cycle | DCN-T | 100 | Autre | TRV | 47 | 3 | 2016-12-21 13:42:55.500
3 | P00935 | 06 | Fin de cycle | PTA | 33 | Autre | TRV | 47 | 3 | 2016-12-21 13:42:58.493
4 | P00935 | 06 | Fin de cycle | DPE | 100 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:00.903
5 | P00935 | 06 | Fin de cycle | BTA | 98 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:01.617
6 | P00935 | 06 | Fin de cycle | FPH | 72 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:06.987
7 | P00935 | 06 | Fin de cycle | QSE | 0 | Autre | TRV | 47 | 3 | 2016-12-21 13:43:10.013
8 | P00935 | 06 | Fin de cycle | DPE | 77 | Autre | TRV | 47 | 3 | 2017-01-11 12:16:32.557
9 | P00935 | 05 | Fin de cycle | DPE | 75 | Autre | TRV | 47 | 3 | 2017-01-11 12:16:40.435
我以为我应该使用GROUP BY子句但是我找不到它工作的配置。你有解决方案吗?
答案 0 :(得分:1)
对数据进行分区,对每个分区中的行进行编号,并获取行号为1的行:
with cte as (
SELECT ProjectName = p.Code ,LotName = lp.Nom , PhaseActive = ph.Nom ,Item = l.Code, Value = c.Note, Cycle = cdv.Nom, Pole = lp.Pole, Note = lp.Note, idEdition = lp.IdEdition, c.DateCreation,
row_number() over (partition by LotName, Item order by DateCreation desc) as rn
FROM Controles c
Join LotProjets lp ON c.IdLotProjet = lp.IdLotProjet
JOIN Projets p ON lp.IdProjet = p.IdProjet
JOIN Referentiels r ON c.IdReferentiel = r.IdReferentiel
JOIN Livrables l ON r.LivrableId = l.IdLivrable
JOIN Phases ph ON lp.IdPhase = ph.IdPhase
JOIN CycleDeVies cdv ON ph.CycleDeVieId = cdv.IdCycleDeVie
WHERE lp.IdEdition = 3 AND c.DateSuppression IS NULL
)
select *
from cte
where rn = 1