sql server - Include query result on separate line using SELECT CASE or similar -
i include queries on separate lines using select case. instead of having field, tape capture value , tape value repeated on same row, great have on separate line. please this? if can!
here current query:
select t.[loan identifier], tc.reviewer, 'valuation date' [field], case when tc.[valuation date] <> t.[valuation date] tc.[valuation date] end [tape capture value], t.[valuation date] [tape value], 'underwriter name' [field], case when tc.[underwriter name] <> t.[underwriter name] tc.[underwriter name] end [tape capture value], t.[valuation date] [tape value] [dbo].[tape] t left join [dbo].[tape capture] tc on t.[loan identifier] = tc.[loan identifier] tc.reviewer not null , tc.[primary_review_complete?] = 1
instead of having result in current form:
i in below form:
the simplest method outer apply
:
select t.[loan identifier], tc.reviewer, v.* [dbo].[tape] t left join [dbo].[tape capture] tc on t.[loan identifier] = tc.[loan identifier] outer apply (values ('valuation date', case when tc.[valuation date] <> t.[valuation date] tc.[valuation date] end, null) ('underwriter name', null, case when tc.[underwriter name] <> t.[underwriter name] tc.[underwriter name] end) ) v(field, [tape capture value], [tape value]) tc.reviewer not null , tc.[primary_review_complete?] = 1
oops. instead of table value constructor, use select
union all
:
select t.[loan identifier], tc.[underwriter name], v.* [dbo].[tape] t left join [dbo].[tape capture] tc on t.[loan identifier] = tc.[loan identifier] outer apply (select 'valuation date', case when tc.[valuation date] <> t.[valuation date] tc.[valuation date] end, null union select 'underwriter name', null, case when tc.[underwriter name] <> t.[underwriter name] tc.[underwriter name] end ) v([field], [tape capture value], [tape value])
the logic demonstrated in sql fiddle, mah not exaclty want.
Comments
Post a Comment