在视图中通过 id 选择 MAX 值?
Choosing MAX value by id in a view?
我根据数据库中的几列创建了一个简单的视图
ALTER VIEW [BI].[v_RCVLI_Test] AS
Select distinct
Borger.CPRnrKort as CPR,
(...)
IndsatsDetaljer.VisitationId as VisitationsId,
Indsats.KatalogNavn as IndsatsNavn,
(case
when
(
Indsats.Model = 'SMDB2 Tilbudsmodel' or
Indsats.Model = 'SMDB2 Samtalemodel' or
Indsats.Model = 'Tilbudsmodel' or
Indsats.Model = 'NAB Tilbudsmodel'
)
then IndsatsDetaljer.ServicePeriodeStart
else IndsatsDetaljer.Ikrafttraedelsesdato
end
) as StartDato,
(case
when
(
Indsats.Model = 'SMDB2 Tilbudsmodel' or
Indsats.Model = 'SMDB2 Samtalemodel' or
Indsats.Model = 'Tilbudsmodel'
)
then (case when IndsatsDetaljer.VisitationSlut = '9999-12-31' then convert(varchar(10), getdate(), 23) else IndsatsDetaljer.VisitationSlut end)
when
Indsats.Model = 'NAB Tilbudsmodel'
then (case when IndsatsDetaljer.NABehandlingSlutDato = '9999-12-31' then convert(varchar(10), getdate(), 23) else IndsatsDetaljer.NABehandlingSlutDato end)
else (case when IndsatsDetaljer.VisitationSlut = '9999-12-31' then convert(varchar(10), getdate(), 23) else IndsatsDetaljer.VisitationSlut end)
end
) as StopDato,
Refusion.Handlekommune as Handlekommune,
replace(Refusion.Betalingskommune, 'Ukendt', 'Kendt') Betalingskommune
from nexus2.Fact_VisiteretTid as Fact
join nexus2.Dim_Paragraf Paragraf
on Fact.DW_SK_Paragraf = Paragraf.DW_SK_Paragraf
join nexus2.Dim_Indsats Indsats
on Fact.DW_SK_Indsats = Indsats.DW_SK_Indsats (...)
存在 StartDato 和 StopDato 的情况,因为这些日期来自不同的列。我已经将日期 '9999-12-31' 转换为当前日期,因为我们稍后会进行一些时间计算,这样更方便。
CPR 是一个人的 ID,VisitationsId 是这个人接受的服务的 ID。
理论上,每个 VisitationsId 应该只有一个 StartDato 和一个 StopDato,但由于文档系统中的一个故障,我们有时会得到两个 StopDato:一个是正确的,一个是 '9999-12-31'(现在转换为当前日期)。
所以我需要按 VisitationsId 分组,然后只取 StopDato 的 MIN 值,但我有点不确定该怎么做?
CPR
VisitationsId
StartDato
StopDato
Something Else
123
56
2019-01-01
2019-12-12
Something
123
56
2019-01-01
9999-12-31
Something
123
58
2019-01-01
2019-12-14
Something
345
59
2018-11-01
9999-12-31
Something
345
55
2017-01-02
2017-11-12
Something
345
55
2017-01-02
9999-12-31
Something
在上面 table 我需要删除第 2 行和第 6 行,因为 VisitationsId 与上一行相同,但它们在 StopDato 上不同。
在查询中的任何地方使用 group by 在另一个(看似随机的)列上给我一个错误,告诉我该列是:
invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
关于我如何着手做这件事有什么建议吗?
添加一个过滤器来测试这个条件?
with cte (
{your current query}
)
select *
from cte T
where not (
StopDato = '9999-12-31'
and exists (
select 1
from cte T1
where T1.VisitationsId = T.VisitationsId
and StopDato != '9999-12-31'
)
);
而且您似乎正在将 StopDato
转换为 varchar,这很糟糕 - 您应该将日期视为日期,直到需要显示它们。
我根据数据库中的几列创建了一个简单的视图
ALTER VIEW [BI].[v_RCVLI_Test] AS
Select distinct
Borger.CPRnrKort as CPR,
(...)
IndsatsDetaljer.VisitationId as VisitationsId,
Indsats.KatalogNavn as IndsatsNavn,
(case
when
(
Indsats.Model = 'SMDB2 Tilbudsmodel' or
Indsats.Model = 'SMDB2 Samtalemodel' or
Indsats.Model = 'Tilbudsmodel' or
Indsats.Model = 'NAB Tilbudsmodel'
)
then IndsatsDetaljer.ServicePeriodeStart
else IndsatsDetaljer.Ikrafttraedelsesdato
end
) as StartDato,
(case
when
(
Indsats.Model = 'SMDB2 Tilbudsmodel' or
Indsats.Model = 'SMDB2 Samtalemodel' or
Indsats.Model = 'Tilbudsmodel'
)
then (case when IndsatsDetaljer.VisitationSlut = '9999-12-31' then convert(varchar(10), getdate(), 23) else IndsatsDetaljer.VisitationSlut end)
when
Indsats.Model = 'NAB Tilbudsmodel'
then (case when IndsatsDetaljer.NABehandlingSlutDato = '9999-12-31' then convert(varchar(10), getdate(), 23) else IndsatsDetaljer.NABehandlingSlutDato end)
else (case when IndsatsDetaljer.VisitationSlut = '9999-12-31' then convert(varchar(10), getdate(), 23) else IndsatsDetaljer.VisitationSlut end)
end
) as StopDato,
Refusion.Handlekommune as Handlekommune,
replace(Refusion.Betalingskommune, 'Ukendt', 'Kendt') Betalingskommune
from nexus2.Fact_VisiteretTid as Fact
join nexus2.Dim_Paragraf Paragraf
on Fact.DW_SK_Paragraf = Paragraf.DW_SK_Paragraf
join nexus2.Dim_Indsats Indsats
on Fact.DW_SK_Indsats = Indsats.DW_SK_Indsats (...)
存在 StartDato 和 StopDato 的情况,因为这些日期来自不同的列。我已经将日期 '9999-12-31' 转换为当前日期,因为我们稍后会进行一些时间计算,这样更方便。
CPR 是一个人的 ID,VisitationsId 是这个人接受的服务的 ID。 理论上,每个 VisitationsId 应该只有一个 StartDato 和一个 StopDato,但由于文档系统中的一个故障,我们有时会得到两个 StopDato:一个是正确的,一个是 '9999-12-31'(现在转换为当前日期)。
所以我需要按 VisitationsId 分组,然后只取 StopDato 的 MIN 值,但我有点不确定该怎么做?
CPR | VisitationsId | StartDato | StopDato | Something Else |
---|---|---|---|---|
123 | 56 | 2019-01-01 | 2019-12-12 | Something |
123 | 56 | 2019-01-01 | 9999-12-31 | Something |
123 | 58 | 2019-01-01 | 2019-12-14 | Something |
345 | 59 | 2018-11-01 | 9999-12-31 | Something |
345 | 55 | 2017-01-02 | 2017-11-12 | Something |
345 | 55 | 2017-01-02 | 9999-12-31 | Something |
在上面 table 我需要删除第 2 行和第 6 行,因为 VisitationsId 与上一行相同,但它们在 StopDato 上不同。
在查询中的任何地方使用 group by 在另一个(看似随机的)列上给我一个错误,告诉我该列是:
invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
关于我如何着手做这件事有什么建议吗?
添加一个过滤器来测试这个条件?
with cte (
{your current query}
)
select *
from cte T
where not (
StopDato = '9999-12-31'
and exists (
select 1
from cte T1
where T1.VisitationsId = T.VisitationsId
and StopDato != '9999-12-31'
)
);
而且您似乎正在将 StopDato
转换为 varchar,这很糟糕 - 您应该将日期视为日期,直到需要显示它们。