这是我的查询
SELECT
client_id,
TimesTested,
CAST(COUNT(*) AS varchar(30)) AS count,
CAST(AVG(testfreq) as varchar(30)) as TestFreq,
CAST(STDEV(TestFreq) as varchar(30)) Stdv
FROM counted
GROUP BY
client_id,
TimesTested
有效;但是,我需要过滤AVG和STDEV,其中testfreq> 0但我需要计数(*)不要过滤。
最接近的是:
SELECT
client_id,
TimesTested,
CAST(COUNT(*) AS varchar(30)) AS count,
CAST(AVG(testfreq) as varchar(30)) as TestFreq,
CAST(STDEV(TestFreq) as varchar(30)) Stdv
FROM counted
where testfreq>0 --however I don't want this filtered applied to count(*)
GROUP BY
client_id,
TimesTested
非常感谢你的指导!
答案 0 :(得分:2)
您应该能够在聚合中使用CASE
语句,类似于:
SELECT client_id,
TimesTested,
CAST(COUNT(*) AS varchar(30)) AS count,
CAST(AVG(case when testfreq>0 then testfreq end) as varchar(30)) as TestFreq,
CAST(STDEV(case when testfreq>0 then TestFreq end) as varchar(30)) Stdv
FROM counted
GROUP BY client_id, TimesTested
我不确定为什么你需要这些作为varchars,是否有理由需要将这些值转换为varchar
?
我只是按原样保留值:
SELECT client_id,
TimesTested,
COUNT(*) AS count,
AVG(case when testfreq>0 then testfreq end) as TestFreq,
STDEV(case when testfreq>0 then TestFreq end) Stdv
FROM counted
GROUP BY client_id, TimesTested