Experts,
I am trying to bring query which match following condition (where count(distinct SDDoc) = 1 and count(distinct case when Netvalue = '0' then 1 else null end) = 1) is it correct?
my code is
SELECT
[Soldtopt],
[tradingname],
[DlvDate],
SUM(try_cast(Netvalue as float)) as Netvalue,
count(distinct SDDoc) as Salesdoc ,
count(distinct case when Netvalue = '0' then 1 else null end) as ZeroValue ,
count(distinct SDDoc) - count(distinct case when Netvalue = '0' then 1 else null end) As Result
FROM [FOC].[dbo].[foc]
where count(distinct SDDoc) = 1 and count(distinct case when Netvalue = '0' then 1 else null end) = 1
GROUP by Soldtopt,tradingname,DlvDate ORDER BY count (distinct SDDoc) DESC; received following error ,
Msg 147, Level 15, State 1, Line 20
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.