Group By

  • Hi:

    I have a query that return data in 8 columns. I need to group then, depend on parameter pased to a store procedure, for the 6 columns or not group data, but, when I pass a constant in columns 5 and 6, the group by does nos work.

    This is the 2 querys:

    When I pass the value 0 in parameter:

    select id, descrip, cuenta, subcuenta, 0 as tipo_mon, 'unica' as descr_mon, sum(saldo_ini) saldo_ini, sum(saldo_act) saldo_act from #ale group by id, descrip, cuenta, subcuenta, tipo_mon, as descr_mon order by cuenta, subcuenta

    NOT WORK

    If I pass the value 1:

    select id, descrip, cuenta, subcuenta, tipo_mon, descr_mon, saldo_ini, saldo_act from #ale order by id

    WORK

    The problem is thath I have values in the columns tipo_mon and descr_mon, and I need ignore them in the first option.

    Thank's for your help.

  • Can you give us a table definition script and some sample data?

    Please read the article linked in my signature line and you will find out how to post both.

    -- Gianluca Sartori

  • All you have to do is remove tipo_mon from the group by clause in the first case when you specified 0 for its value.

  • Anytime you specify a static value in a SELECT clause, you do not include it in your GROUP BY clause. I hope that helps you out.

  • There is also an 'as' between there

    'tipo_mon, 'as' descr_mon '

    this might also be the cause of ur problem

  • Thank´s William. Your solution works perfectly.

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply