Skip to content

Instantly share code, notes, and snippets.

@ConstantineK
Created August 15, 2019 18:49
Show Gist options
  • Save ConstantineK/14a9705ba584a2887555924436a178f3 to your computer and use it in GitHub Desktop.
Save ConstantineK/14a9705ba584a2887555924436a178f3 to your computer and use it in GitHub Desktop.
declare @eva table -- entity value attribute
(
id int identity, -- we could use date here either
type nvarchar(100),
value nvarchar(100),
student_identifier int
)
insert @eva (type, value, student_identifier)
select 'studentid', '1', 3
union all
select 'studentid', '2', 3
union all
select 'startdate', CONVERT(NVARCHAR(100), getdate()), 3
union all
select 'startdate', CONVERT(NVARCHAR(100), ( select getdate()-1)), 3
select
ev.student_identifier,
MAX ( CASE WHEN TYPE ='startdate' then value else null end ) as start_date,
MAX ( CASE WHEN TYPE ='studentid' then value else null end ) as student_id
from @eva AS ev
WHERE EXISTS
(
select *
from
(
select max(e.id) AS id , e.type
from @eva as e
group by e.type
) AS m
WHERE
m.id = ev.id
)
GROUP BY
ev.student_identifier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment