sql - Get values from a row based on some rules -
i trying solve how handle sql query aggregate data together. here sample data set.
site status vendor 1234 rejected joe 1234 rejected john 1234 approved jane 1234 rejected jeb 5644 rejected joe 5644 rejected john 5644 in process jane 8877 rejected joe 8877 rejected john i resulting data set following
1234 approved jane 5644 in process jane 8877 rejected there can anywhere between 1 , 10 records same site. there hierarchy status. example if of statuses on site approved of others not matter.
i realize similar question other questions have been asked before. first post. if find post seems match looking feel free share it.
here example of query had written:
select distinct [site] ,(select case when min(status) = 'completed' 'completed' when min(status) = 'in process' 'shopping' when min(status) = 'rejected' 'rejected' when min(status) = 'withdrawn' 'withdrawn' when min(status) = 'hold' 'hold' else 'no status' end status [dbo].[test] subtest test.site = subtest.site , subtest.overallstatus = 'a' group site) test_status ,vendor [dbo].[test] test overallstatus = 'a' order site sorry editing code environment , might have missed changes. overallstatus value in table not shown.
it bit strange there no vendor on rejected. but, 1 way prioritization using row_number() fancy order by:
select site, status, (case when status <> 'rejected' vendor end) vendor (select t.*, row_number() on (partition site order (case when status = 'approved' 1 when status = 'in process 2 else 3 end) ) seqnum t ) t seqnum = 1
Comments
Post a Comment