sql server - How to SUM for Item1 and Item1 + 'char' -


i have question little sql problem.

for instance, have table table this:

|  item  |  value  | |a0001   |   3     | |a0001   |   2     | |a0002   |   2     | |a0003   |   7     | |a0003   |   3     | |a0003u  |   2     |

as can see, item can have may values. have done, sum() value attached each item, obtain sum of value.

what want now, adding values attached item , and item + 'u'

for example, sum of value item a0003u

7 (line 1 of item) + 3 (line 2 of item) + 2 (line 1 of item + 'u') = 12

i know can use function right(item, 1) return 'u' if endswith 'u', don't know how make calculation.

edit:

a resulset should be:

|  item  |  value  | |a0001   |   5     | |a0002   |   2     | |a0003   |   12     |

i over-thought problem before op added expected result-set. nevertheless here query if desired output like:

|  item  |  value  | |a0001   |   5     | |a0002   |   2     | |a0003   |   10    | |a0003u  |   12    | 

it might turn out useful:

select     a.item    ,case        when right(a.item,1)='u'          sum(b.value)          else sum(a.value)        end value table1 left join table1 b    on a.item = b.item + 'u'    or (right(a.item,1)='u' , a.item = b.item) group a.item 

sqlfiddle demo

edit:

query simplified (actual) version of problem:

select    case      when right(item,1) = 'u' substring(item,0,len(item))      else item    end item   ,sum(value) value table1 group     case      when right(item,1) = 'u' substring(item,0,len(item))      else item     end 

sqlfiddle demo


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -