sql server - SQL pivot rows to columns -


i have query returns 2 types agency "vente" , "recouv". want request displays 1 line sum of column "commission".

here picture shows have , want:

 select codeagence,case when (typeoperation =0) 'vente' else 'recouvrement' end  'type',  count(*) 'nb',sum(pxventeht ) 'c.a',sum(acompte) 'montant',sum(montantcommission ) 'commission' commission_archive   group codeagence,typeoperation   order codeagence 

enter image description here

i think might want, question lacks proper sample source data haven't tested it.

select      codeagence,     count(case when (typeoperation = 0) 'vente' else null end) 'nb vente',     sum(case when (typeoperation = 0) pxventeht else 0 end) 'c.a',     count(case when (typeoperation <> 0) 'recouve' else null end) 'nb recouve',     sum(case when (typeoperation <> 0) acompte else 0 end) 'montant recouve',     sum(montantcommission) 'commission'  commission_archive  group codeagence order codeagence  

the key apply conditional aggregation technique aggregated columns should split on nb/recouve , change grouping.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -