tsql - T-SQL Select to compute a result row on preceeding group/condition -
how achieve result using t-sql select query. given sample table :
create table sample (a int, b int) insert sample values (999, 10) insert sample values (16, 11) insert sample values (10, 12) insert sample values (25, 13) insert sample values (999, 20) insert sample values (14, 12) insert sample values (90, 45) insert sample values (18, 34)
i'm trying achieve output:
b result ----------- ----------- ----------- 999 10 10 16 11 10 10 12 10 25 13 10 999 20 20 14 12 20 90 45 20 18 34 20
the rule simple: if column 'a' has special value of 999 result row , following rows (unless value of 'a' again 999) value of column 'b'. assume first record have 999 on column 'a'.
any hint how implement, if possible, select query without using stored procedure or function?
thank you.
antónio
you need have id column
select cn.id, cn.a , (select top (1) b sample sample.id <= cn.id , = 999 order id desc) sample cn order id
Comments
Post a Comment