sql - Move Values into next Column -
i have table (the structure fixed) contains 24 fields h0-h23 representing hours of day. in 1 of these columns there 'm'. once 'm' has been found (say in h13) want swap value in h13 value in h14 (the field next it) , swap value in h14 h13. in example (i have limited 6 fields clarity)
null, null, t, t, m, s, f
would change to
null, null, t, t, s, m, f
what efficient way this? thank you
note: understand update queries, main problem need find position of 'm' without doing 24 case statements if possible
as can't change data-structure, you're stuck lot of case statements...
update yourtable set h00 = case m_hour when 00 h01 else h00 end, h01 = case m_hour when 00 h00 when 01 h02 else h01 end, h02 = case m_hour when 01 h01 when 02 h03 else h02 end, h03 = case m_hour when 02 h02 when 03 h04 else h03 end, ... h23 = case m_hour when 22 h22 when 23 ??? else h23 end ( select primary_key, case when h00 = 'm' 00 when h01 = 'm' 01 ... end m_hour yourtable ) lookup lookup.primary_key = yourtable.primary_key
you shorten sub-query case doing instead:
charindex('m', h00 + h01 + h02 + ... + h23, 0) - 1 m_hour
but add cpu load price shortening code, , doesn't rid of 24 case statements in update.
Comments
Post a Comment