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:

original

i in below form:

required

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

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -