0

我在下面运行一个查询,因为我不能将聚合放在 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 百分比的佣金。

有任何想法吗?

4

3 回答 3

1

这是我对您要做什么的最佳猜测。

WITH ic AS
(
SELECT 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
  FROM InvoiceCommissions ic
 GROUP BY primaryconsultantid
     , DATENAME(mm,invoiceissueddate)
     , DATEPART(yy,invoiceissueddate)
)    
SELECT u.username + ' ' + u.surname AS UserName
     , ic.primaryconsultantid
     , ic.starters
     , ic.month
     , ic.year
     , ic.totalvalue
     , cg.threshold 
     , cgv.percentage 
  FROM ic
 INNER JOIN Users u 
    ON u.UserId = ic.primaryconsultantid
 INNER JOIN commissionconsultants cc
    ON cc.UserId = ic.primaryconsultantid
  LEFT OUTER commissiongrades cg
    ON cg.gradeid = cc.gradeid
  LEFT OUTER commissiongradevalues cgv
    ON cgv.gradeid = cc.gradeid
   AND cgv.starters = ic.starters

但是,您的查询中有一些我不明白的地方。

  1. 为什么将阈值和百分比放在 SELECT 中?您是否期望缺少年级 ID?如果没有,请将 更改LEFT OUTERINNER
  2. 如果您已经有佣金,为什么还要获得佣金ic.primaryconsultantperc?这是一个或有佣金吗?如果是,为什么它没有添加到totalvalue?

总之,享受吧!

于 2012-10-31T03:06:16.777 回答
0

替换COUNT(*)为:

(select count(*) from commissionconsultants)
于 2012-10-30T16:57:01.150 回答
0

我建议在进行分组时只使用一个主子查询,然后进行所有连接。没有测试数据很难编写查询。
无论如何,据我所知-您正在使用两个子查询commissionconsultants,实际上您只需要在此表上进行一个联接。

select
    ic.UserName, 
    ic.primaryconsultantid, 
    ic.starters, 
    ic.[month],
    ic.[year],
    ic.totalvalue,
    cg.threshold as Threshold, 
    cgv.percentage as Percentage
from
(
    select
        u.username + ' ' + u.surname as UserName, 
        tt.primaryconsultantid, 
        count(tt.invoiceid) as starters,
        datename(mm, tt.invoiceissueddate) as [month], 
        datepartT(yy, tt.invoiceissueddate) as [year],
        cast(sum((tt.value / tt.exchangerate) * (tt.primaryconsultantperc / 100)) as numeric(8,2)) as totalvalue,
    from InvoiceCommissions as tt
        inner join Users as u on u.userid = tt.primaryconsultantid
    group by
        tt.primaryconsultantid, 
        datename(mm, tt.invoiceissueddate), datepart(yy, tt.invoiceissueddate),
        u.username + ' ' + u.surname
) as ic
    inner join commissionconsultants as cc on cc.userid = ic.primaryconsultantid
    inner join commissiongrades as cg on cg.gradeid = cc.gradeid
    inner join commissiongradevalues as cgv on cgv.gradeid = cc.gradeid and cgv.starters = ic.starters
于 2012-10-30T17:08:00.340 回答