如何根据子查询中的计数(*)选择值

时间:2012-10-30 16:55:26

标签: sql sql-server-2008 aggregate-functions

我正在运行下面的查询,因为我无法将聚合放在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 

从本质上讲,我要做的是根据顾问的等级(在佣金顾问中找到)以及在给定月份中的投放数量,从表格(委员会评估值)中查找佣金百分比。

e.g。如果顾问在一个月内进行了一次安置,他们将拥有XX佣金百分比,如果他们在一个月内进行了两次安置,他们将拥有YY佣金百分比。

有什么想法吗?

3 个答案:

答案 0 :(得分: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 OUTER更改为INNER
  2. 如果您已经ic.primaryconsultantperc,为什么需要获得佣金?这是一个偶然佣金吗?如果是,为什么它没有添加到totalvalue
  3. 无论如何,享受!

答案 1 :(得分:0)

COUNT(*)替换为:

(select count(*) from commissionconsultants)

答案 2 :(得分: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