How Can I Compare and Hold Some Value : SQL SERVER 2008 R2 -


i have table looks this:

item    |   date    |  price | new_price | reduction | pencil  | 01/10/14  |    25  |    21     |     4     | pencil  | 01/12/14  |    21  |    21     |     0     | pencil  | 01/30/14  |    21  |    21     |     0     | pencil  | 02/05/14  |    21  |    18     |     3     | pencil  | 02/17/14  |    18  |    18     |     0     | pencil  | 03/20/14  |    18  |    18     |     0     | pencil  | 06/08/14  |    18  |    16     |     2     | pencil  | 07/14/14  |    16  |    16     |     0     | pencil  | 07/28/14  |    16  |    16     |     0     | 

how can select can result this:

item    |   date    |  price | new_price | reduction | reduction_hold | pencil  | 01/10/14  |    25  |    21     |     4     |       4        | pencil  | 01/12/14  |    21  |    21     |     0     |       4        | pencil  | 01/30/14  |    21  |    21     |     0     |       4        | pencil  | 02/05/14  |    21  |    18     |     3     |       3        | pencil  | 02/17/14  |    18  |    18     |     0     |       3        | pencil  | 03/20/14  |    18  |    18     |     0     |       3        | pencil  | 06/08/14  |    18  |    16     |     2     |       2        | pencil  | 07/14/14  |    16  |    16     |     0     |       2        | pencil  | 07/28/14  |    16  |    16     |     0     |       2        | 

thanks in advance.

try sub query:

select item = 'pencil', date = '01/10/14', price  = 25, new_price = 21 , reduction = 4 #table union select 'pencil'   ,'01/12/14'      ,21      ,21         ,0 union select 'pencil'   ,'01/30/14'      ,21      ,21         ,0 union select 'pencil'   ,'02/05/14'      ,21      ,18         ,3 union select 'pencil'   ,'02/17/14'      ,18      ,18         ,0 union select 'pencil'   ,'03/20/14'      ,18      ,18         ,0 union select 'pencil'   ,'06/08/14'      ,18      ,16         ,2 union select 'pencil'   ,'07/14/14'      ,16      ,16         ,0 union select 'pencil'   ,'07/28/14'      ,16      ,16         ,0  go  select *  , reduction_hold = (select top 1 reduction #table b a.date >= b.date , b.reduction > 0 order b.date desc) #table order date 

result:

enter image description here


Comments

Popular posts from this blog

javascript - Wordpress slider, not displayed 100% width -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -