locked
Doing changes in the select statement results before displaying it RRS feed

  • Question

  • User273232671 posted
    Hello all,

    I have a question, if the result of the select statement is as follows:


    Product | result1| result 2
    ---------------------------------
    A | 12 | 5
    ----------------------------------
    B | 15 | 4
    ----------------------------------
    C | 10 | 8


    And if i want to display in the results only A and B but in B results i want the total of B and C which means the results that i want it to show are:

    Product | result1| result 2
    ---------------------------------
    A | 12 | 5
    ----------------------------------
    B | 25 | 12
    ---------------------------------


    Can that be done or no, and if yes then how to do it!?
    Friday, January 29, 2016 5:59 PM

All replies

  • User-808054615 posted

    Hi,

    Try something like this:

    select
        case when Product = 'C' then 'B' else Product end as Product,
        sum(result1) as result1,
        sum(result2) as result2
    from MyTable
    group by
        case when Product = 'C' then 'B' else Product end

    Hope this help

    Friday, January 29, 2016 6:42 PM
  • User273232671 posted
    i didnt really understabd how ur code works i apologize, can inget more explanation!?
    Friday, January 29, 2016 7:49 PM
  • User-808054615 posted

    With the Case when the Product is equal to "C" is replaced by "B", making it possible grouping of these rows.

    Friday, January 29, 2016 10:26 PM
  • User273232671 posted
    Ok, so what i understood from the explanation is that we will replace the C with B, but that doesnt add the value of C to B right!?
    Saturday, January 30, 2016 6:02 AM
  • User273232671 posted
    Just to be nore clear about what i need, i need it when
    1-case equal C to be equal to B
    2-the result of b to be bothe the result of b + c


    Which means both results of b and c to be added to the name b and get rid of the name c
    Saturday, January 30, 2016 6:37 AM
  • User-808054615 posted

    Check out the following test:

    http://sqlfiddle.com/#!3/d9a81/1

    Hope this help

    Saturday, January 30, 2016 11:47 AM
  • User77042963 posted

    You can use the alias in the group by clause:

    select
        case when Product = 'C' then 'B' else Product end as Product,
        sum(result1) as result1,
        sum(result2) as result2
    from MyTable
    group by Product
    

    Monday, February 1, 2016 4:56 PM