How to use the amount and account functions

I am trying to get the total amount and total amount for the seller, but it doesn’t work in my WITH request

I currently have:

 WITH CALC1 AS (
   SELECT B.OTSMAN AS SALESMAN, COUNT(B.ONINUM) AS COUNT, SUM(B.OVSVAL) AS SVAL 
   FROM @LIB@.DGLOESUMD AS B 
   WHERE B.OCSGRP <> 'INT' AND 
         B.OCSGRP <> 'INX' AND 
         B.OCPSBR IN (@OCPSBR@) AND 
         B.ICMATX <> '705' AND 
         B.OCSGRP NOT LIKE 'S/%' 
  GROUP BY B.OTSMAN, B.ONINUM ORDER BY B.OTSMAN ASC
 )
 SELECT SALESMAN, COUNT,  SVAL FROM CALC1 ORDER BY SALESMAN

RESULT:

AM  1   79.03
AM  40  1337.70
AM  48  4627.15
AM  42  2727.40
AM  1   111.79
AM  1   1110.00
AM  52  3018.77

How can I change my request, so I get only one record with the total quantity and value for the seller?

AM  1   13,011.84
+3
source share
2 answers

You need to change your group as follows:

with calc1 as 
(select b.otsman as salesman, 
        count(distinct b.oninum) as count, 
        sum(b.ovsval) as sval 
   from @lib@.dgloesumd as b 
  where b.ocsgrp <> 'INT' 
    and b.ocsgrp <> 'INX' 
    and b.ocpsbr in (@ocpsbr@) 
    and b.icmatx <> '705' 
    and b.ocsgrp not like 'S/%' 
 group by b.otsman 
)
select salesman,
       count, 
       sval
  from calc1
order by salesman

Update: Updated to receive a separate number of sales orders.

0
source

Change GROUP BY to

GROUP BY B.OTSMAN

Sort of

;WITH CALC1 AS (
        SELECT  B.OTSMAN AS SALESMAN, 
                COUNT(B.ONINUM) AS COUNT, 
                SUM(B.OVSVAL) AS SVAL 
        FROM    @LIB@.DGLOESUMD AS B 
        WHERE   B.OCSGRP <> 'INT' 
        AND     B.OCSGRP <> 'INX' 
        AND     B.OCPSBR IN (@OCPSBR@) 
        AND     B.ICMATX <> '705' 
        AND     B.OCSGRP NOT LIKE 'S/%' 
        GROUP BY    B.OTSMAN
        ORDER BY    B.OTSMAN ASC
) 

SELECT  

SALESMAN, 
COUNT,  
SVAL 

FROM CALC1 

ORDER BY SALESMAN 
+1
source

Source: https://habr.com/ru/post/1742388/


All Articles