我試圖讓每個銷售人員的總數和總金額,但它不是我的工作與查詢如何使用sum和count函數
目前我有:
WITH CALC1 AS (
SELECT B.OTSMAN AS SALESMAN, COUNT(B.ONINUM) AS COUNT, SUM(B.OVSVAL) AS SVAL
FROM @[email protected] AS B
WHERE B.OCSGRP <> 'INT' AND
B.OCSGRP <> 'INX' AND
B.OCPSBR IN (@[email protected]) 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
我該如何更改我的查詢,以便每個銷售人員只能獲得一個記錄總數和價值?
AM 1 13,011.84
我不相信您的查詢有效。你把「ORDER BY B.OTSMAN ASC」放在cte中,我猜sql server會拋出一個錯誤。 – zsong 2010-04-23 03:14:16