我在下面运行一个查询,因为我不能将聚合放在 where 子句中,所以它不起作用,但鉴于它在子查询中,我不完全确定从这里开始的最佳方法是什么......!
不起作用的行是:
(select gradeid from commissionconsultants where UserId = ic.primaryconsultantid)
and starters = COUNT(*) ) from InvoiceCommissions ic
完整的查询如下:
select u.username + ' ' + u.surname as UserName,
ic.primaryconsultantid,
COUNT(ic.invoiceid) as starters,
DATENAME(mm,ic.invoiceissueddate) AS [month],
DATEPART(yy,ic.invoiceissueddate) as [year],
cast(SUM((ic.value / ic.exchangerate) * (ic.primaryconsultantperc / 100)) as numeric(8,2)) AS totalvalue,
(select threshold from commissiongrades where gradeid =
(select gradeid from commissionconsultants where UserId = ic.primaryconsultantid) ) AS Threshold,
(select percentage from commissiongradevalues where gradeid =
(select gradeid from commissionconsultants where UserId = ic.primaryconsultantid)
and starters = COUNT(*) ) from InvoiceCommissions ic
inner join commissionconsultants cc on cc.userid = ic.primaryconsultantid
inner join Users u on u.UserId = ic.primaryconsultantid
group by primaryconsultantid, DATENAME(mm,invoiceissueddate), DATEPART(yy,invoiceissueddate), u.username + ' ' + u.surname
本质上,我想做的是根据顾问的等级(在佣金顾问中找到)和给定月份的展示次数从表(commissiongradevalues)中查找佣金百分比。
例如,如果顾问在一个月内进行了一次展示,他们将获得 XX 百分比的佣金,如果他们在一个月内进行了两次展示,他们将获得 YY 百分比的佣金。
有任何想法吗?