gpt4 book ai didi

sql - 如何使用 sum 和 count 函数

转载 作者:行者123 更新时间:2023-12-04 13:43:20 25 4
gpt4 key购买 nike

我正在尝试获取每个销售人员的总数和总和,但它在我的 WITH 查询中不起作用

目前我有:

 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

结果是:
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

如何更改我的查询,以便我只获得一条记录,其中包含每个销售人员的总数和值(value)?
AM  1   13,011.84

最佳答案

更改GROUP BY

GROUP BY B.OTSMAN

就像是
;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

关于sql - 如何使用 sum 和 count 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2695962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com