SQL选择第二列最大的唯一列

时间:2019-07-10 10:26:21

标签: sql-server tsql greatest-n-per-group

请如何结合最大和唯一性以始终具有唯一ID和该唯一ID的列的最大值?

例如,如果我有,

OrdinaceId Priloha2Id
5            1
5            2
6            2
6            4
7            1

结果将

OrdinaceId Priloha2Id
5             2
6             4
7             1

如何仅返回最大具有Priloha2Id的1行(具有相同的ID)?

select * from (select 
distinct ord.Id as OrdinaceId,
ord.CleneniPzsId,
  posk.Id,
  posk.ICO as Ico,
  zar.ICZ as Icz,
  posk.NazevZkraceny as Zkratka,
  case when cle.Primariat = 0 then cle.Icp end as Icp,
  IIF(cle.Primariat = 1, pri.OborKod , cle.OdbornostKod) as OdbornostKod,
  ord.Nazev as OrdinaceNazev,
  (select Street + N' ' + DescriptiveNo + N', ' + PostCode +  N' ' + City from ICIS_Repl.repl.Address where Code = ord.NavAddressCode and Type = N'ORDINACE' and (ValidFrom is null or ValidFrom <= GETDATE()) and (ValidTill is null or ValidTill >= GETDATE() or ValidTill = N'1753-01-01 00:00:00')) as OrdinaceAdresaCela,
  pril.Id as Priloha2Id,
  cle.Smluvni,
  cisP2KomunikaceStav.Nazev as EP2,
  cisPzs.Nazev as StavPzp,
  pril.Status,
  pril.Info,
  pril.PriPlatnostOd as PlatnostOd,
  pril.PriPlatnostDo as PlatnostDo
from Ordinace ord
  left join CleneniPzs cle on cle.Id = ord.CleneniPzsId
  left join ZarizeniPZS zar on cle.ZarizeniPzsId = zar.Id
  left join PoskytovatelZS posk on zar.PoskytovatelZsId = posk.Id
  left join Primariat pri on cle.PrimariatId = pri.Id
  left join Pril2Formular pr on pr.CleneniPzsId = cle.Id
  left join Priloha2 pril on pril.Id = pr.Priloha2Id and (pril.PriPlatnostOd <= GETDATE() or pril.PriPlatnostOd is null) and (pril.PriPlatnostDo is null or pril.PriPlatnostDo >= GETDATE()) 
  join CisCiselnik cisP2KomunikaceStav on posk.P2KomunikaceStavKod = cisP2KomunikaceStav.Kod AND cisP2KomunikaceStav.Oblast = N'P2KomunikaceStav' and cisP2KomunikaceStav.PlatnostOd <= GETDATE() and (cisP2KomunikaceStav.PlatnostDo is null or cisP2KomunikaceStav.PlatnostDo >= GETDATE()) 
  left join P2Formular form on form.Icp = cle.Icp and form.Aktivni = 1
  left join CisCiselnik cisPzs on form.P2StavPzpKod = cisPzs.Kod AND cisPzs.Oblast = N'P2StavPZP' and cisPzs.PlatnostOd <= GETDATE() and (cisPzs.PlatnostDo is null or cisPzs.PlatnostDo >= GETDATE())  
  left join P2ZarizeniPZS z on form.P2ZarizeniPzsId = z.Id and z.Icz = zar.ICZ and z.PoskytovatelZsId = posk.Id
  join Smlouva smlv on smlv.Id = pril.SmlouvaId
  left join SmluvniVykon smlVyk on smlVyk.CleneniPzsId = cle.Id and smlVyk.PlatnostOd <= GETDATE() and (smlVyk.PlatnostDo is null or smlVyk.PlatnostDo >= GETDATE())
  left join SmlVykonVyjadreniRL vyjadreni on vyjadreni.Id = smlVyk.VyjadreniRLId ) a 

enter image description here

2 个答案:

答案 0 :(得分:1)

从表组中按ID选择ID,max(Priloha2Id)

答案 1 :(得分:1)

您可以使用CTE来获取每个id的max(Priloha2Id)。然后加入它。

类似的东西:

;with Pril2Formular_max_per_id as (
    select ord.Id as OrdinaceId, max(pr.Priloha2Id) as max_Priloha2Id
    from Ordinace ord
      left join CleneniPzs cle on cle.Id = ord.CleneniPzsId
      left join Pril2Formular pr on pr.CleneniPzsId = cle.Id
    group by ord.Id
)
select * from (select 
distinct ord.Id as OrdinaceId,[![enter image description here][1]][1]
ord.CleneniPzsId,
  posk.Id,
  posk.ICO as Ico,
  zar.ICZ as Icz,
  posk.NazevZkraceny as Zkratka,
  case when cle.Primariat = 0 then cle.Icp end as Icp,
  IIF(cle.Primariat = 1, pri.OborKod , cle.OdbornostKod) as OdbornostKod,
  ord.Nazev as OrdinaceNazev,
  (select Street + N' ' + DescriptiveNo + N', ' + PostCode +  N' ' + City from ICIS_Repl.repl.Address where Code = ord.NavAddressCode and Type = N'ORDINACE' and (ValidFrom is null or ValidFrom <= GETDATE()) and (ValidTill is null or ValidTill >= GETDATE() or ValidTill = N'1753-01-01 00:00:00')) as OrdinaceAdresaCela,
  pril.Id as Priloha2Id,
  cle.Smluvni,
  cisP2KomunikaceStav.Nazev as EP2,
  cisPzs.Nazev as StavPzp,
  pril.Status,
  pril.Info,
  pril.PriPlatnostOd as PlatnostOd,
  pril.PriPlatnostDo as PlatnostDo
from Ordinace ord
  left join CleneniPzs cle on cle.Id = ord.CleneniPzsId
  left join ZarizeniPZS zar on cle.ZarizeniPzsId = zar.Id
  left join PoskytovatelZS posk on zar.PoskytovatelZsId = posk.Id
  left join Primariat pri on cle.PrimariatId = pri.Id
  left join Pril2Formular_max_per_id pr on pr.OrdinaceId = ord.Id
  left join Priloha2 pril on pril.Id = pr.max_Priloha2Id and (pril.PriPlatnostOd <= GETDATE() or pril.PriPlatnostOd is null) and (pril.PriPlatnostDo is null or pril.PriPlatnostDo >= GETDATE()) 
  join CisCiselnik cisP2KomunikaceStav on posk.P2KomunikaceStavKod = cisP2KomunikaceStav.Kod AND cisP2KomunikaceStav.Oblast = N'P2KomunikaceStav' and cisP2KomunikaceStav.PlatnostOd <= GETDATE() and (cisP2KomunikaceStav.PlatnostDo is null or cisP2KomunikaceStav.PlatnostDo >= GETDATE()) 
  left join P2Formular form on form.Icp = cle.Icp and form.Aktivni = 1
  left join CisCiselnik cisPzs on form.P2StavPzpKod = cisPzs.Kod AND cisPzs.Oblast = N'P2StavPZP' and cisPzs.PlatnostOd <= GETDATE() and (cisPzs.PlatnostDo is null or cisPzs.PlatnostDo >= GETDATE())  
  left join P2ZarizeniPZS z on form.P2ZarizeniPzsId = z.Id and z.Icz = zar.ICZ and z.PoskytovatelZsId = posk.Id
  join Smlouva smlv on smlv.Id = pril.SmlouvaId
  left join SmluvniVykon smlVyk on smlVyk.CleneniPzsId = cle.Id and smlVyk.PlatnostOd <= GETDATE() and (smlVyk.PlatnostDo is null or smlVyk.PlatnostDo >= GETDATE())
  left join SmlVykonVyjadreniRL vyjadreni on vyjadreni.Id = smlVyk.VyjadreniRLId ) a ;
相关问题