GADI ROSALES
GADI ROSALES

Reputation: 59

Group by single value in different columns in SQL Server

My query:

select 
    cue.cod_cli, sum(cue.sal_doc) as total, cue.num_doc count
from
    cxc_cuedoc cue
where 
    sal_doc > 0 and tip_doc = '010'
group by 
    cue.cod_cli

Expected result

cod_cli      total       num_doc 
--------------------------------
47474747    450,30         4      

The num_doc would be the total of number of documents that give me the total.

Upvotes: 1

Views: 35

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271151

I think you want sum(cue.num_doc).

select cue.cod_cli, sum(cue.sal_doc) as total,
       sum(cue.num_doc) as count
from cxc_cuedoc cue
where sal_doc > 0 and tip_doc = '010'
group by cue.cod_cli;

It is possible that you want count(*) instead. Without sample data it is hard to tell.

Upvotes: 1

Related Questions