我有以下查询,它返回客户的用户名、年份和半年、第一个半年的投注计数、第二个半年的投注计数和利润计算。
过程 sql;
create table avg
as
select
username as username,
year(datepart(betdate))*10 + floor( (qtr(datepart(betdate))+1)/2) as yearsemiyear,
count(bet_id) as betcount,
sum( case when floor( (qtr(datepart(betdate))+1)/2) = 1 then 1 else 0 end ) as firstHalfBetcount,
sum( case when floor( (qtr(datepart(betdate))+1)/2) = 2 then 1 else 0 end ) as secondHalfBetcount,
round(mean((winnings - stake) / stake) * 100, 0.01) as averageMargin
from &dsn
WHERE datepart(BETDATE) > '31DEC2010'd AND datepart(BETDATE) <'01JAN2012'd
AND bet_type = 'SGL'
group by 1,2
/* HAVING sum( case when floor( (qtr(datepart(betdate))+1)/2) = 1 then 1 else 0 end ) >= 4 */
order by username desc, yearsemiyear asc;
退出;
我如何将其限制在样本前半部分至少有 4 次投注的客户?如果我取消注释我在那里的 HAVING 子句,它似乎只返回前半部分的行,而没有返回后半部分的行。我尝试将限制放入 WHERE 子句,但显然您不能在该 CLAUSE 中放置摘要函数。
我该怎么做?
谢谢