根据条件获取计数

时间:2013-05-25 07:00:15

标签: sql-server sql-server-2008 sql-server-2008-r2

我正在使用SQL Server 2008 R2。我有一张表BrokerTable

-----------------------------------
  BrokerCode  |  Rank  | BrokerId
-----------------------------------
   1527339    |   1    |  3880
   1527488    |   1    |  7550
   1527366    |   1    |  3854
   1527400    |   1    |  1519
   1527358    |   1    |  3862
   1527357    |   1    |  3863

以下是我在上表中计算经纪业务的查询部分。例如BrokerId 3880

SELECT CONVERT(DECIMAL(18, 2), SUM(T.Amount11))
FROM ( SELECT ISNULL(( CASE WHEN mb.PlanType = 'MULTIPLE'
THEN CASE WHEN mb.Mode = 'MLY' THEN ( ( ( SUM(SelfAmount) + SUM(UnitAmount) )
* ( SELECT bcm.PromoteeQuota FROM dbo.BusinessCalcMaster AS bcm
WHERE bcm.PlanType = mb.PlanType AND bcm.PlanName = mb.PlanName
AND bcm.Year = mb.Year AND mb.Mode = 'MLY'
AND bcm.MLY_From <= mb.InstallmentNo AND bcm.MLY_To >= mb.InstallmentNo
) ) / 100 )  WHEN mb.Mode = 'QLY' THEN ( ( ( SUM(SelfAmount)
+ SUM(UnitAmount) ) * ( SELECT bcm.PromoteeQuota FROM
dbo.BusinessCalcMaster AS bcm  WHERE  bcm.PlanType = mb.PlanType
AND bcm.PlanName = mb.PlanName AND bcm.Year = mb.Year
AND mb.Mode = 'QLY' AND bcm.MLY_From <= mb.InstallmentNo
AND bcm.MLY_To >= mb.InstallmentNo ) ) / 100 )
WHEN mb.Mode = 'HLY' THEN ( ( ( SUM(SelfAmount) + SUM(UnitAmount) )
* ( SELECT bcm.PromoteeQuota FROM dbo.BusinessCalcMaster AS bcm WHERE
bcm.PlanType = mb.PlanType AND bcm.PlanName = mb.PlanName
AND bcm.Year = mb.Year AND mb.Mode = 'HLY' AND bcm.MLY_From <= mb.InstallmentNo
AND bcm.MLY_To >= mb.InstallmentNo ) ) / 100 )
WHEN mb.Mode = 'YLY' THEN ( ( ( SUM(SelfAmount) + SUM(UnitAmount) )
* ( SELECT bcm.PromoteeQuota  FROM  dbo.BusinessCalcMaster AS bcm
WHERE bcm.PlanType = mb.PlanType AND bcm.PlanName = mb.PlanName
AND bcm.Year = mb.Year AND mb.Mode = 'YLY' AND bcm.MLY_From <= mb.InstallmentNo
AND bcm.MLY_To >= mb.InstallmentNo ) ) / 100 ) END
WHEN mb.PlanType = 'SINGLE' THEN ( ( SUM(SelfAmount) + SUM(UnitAmount)
* ( SELECT   bcm.PromoteeQuota FROM dbo.BusinessCalcMaster
AS bcm WHERE    bcm.PlanType = mb.PlanType
AND bcm.PlanName = mb.PlanName AND bcm.Year = mb.Year ) ) / 100 )
END ), 0) AS Amount11 , mb.InstallmentNo
FROM dbo.MemberBusiness AS mb WHERE mb.BrokerId = 3880
AND ( ( mb.PlanType = 'MULTIPLE' AND ( ( mb.Mode = 'HLY'
AND mb.InstallmentNo = 2 ) OR ( mb.Mode = 'QLY' AND mb.InstallmentNo >= 2
AND mb.InstallmentNo <= 4 ) OR ( mb.Mode = 'MLY' AND mb.InstallmentNo >= 2
AND mb.InstallmentNo <= 12 ) OR ( mb.InstallmentNo = 1 ) ))
OR ( mb.PlanType = 'SINGLE' ) )AND mb.Date >= '2013-02-01 00:00:00.000'
AND mb.Date <= '2013-02-28 00:00:00.000' GROUP BY  mb.Mode , mb.Year ,
mb.PlanName , mb.PlanType , mb.InstallmentNo ) AS T

BrokerTable中,可能有任意数量的经纪人。我想从BrokerTable计算每个经纪商的业务量,并获得业务量大于15000的经纪商数量。

我可以使用游标,但正如我所说,表中可能有任意数量的代理,查询需要更多时间来执行。如何以更少的时间或最简单的方式获得结果?感谢

1 个答案:

答案 0 :(得分:1)

尝试:

SELECT B.BrokerId,
       MAX(B.BrokerCode) BrokerCode,
       CONVERT(DECIMAL(18, 2), SUM(T.Amount11)) BrokerAMount
FROM BrokerTable B
JOIN (SELECT CASE WHEN mb.PlanType = 'MULTIPLE' AND mb.Mode in ('MLY', 'QLY', 'HLY', 'YLY')
                  THEN ( SUM(SelfAmount) + SUM(UnitAmount) ) * 
                       ( SELECT bcm.PromoteeQuota 
                         FROM dbo.BusinessCalcMaster AS bcm
                         WHERE bcm.PlanType = mb.PlanType AND bcm.PlanName = mb.PlanName AND bcm.Year = mb.Year 
                           AND bcm.MLY_From <= mb.InstallmentNo AND bcm.MLY_To >= mb.InstallmentNo) 
                       / 100 
                  WHEN mb.PlanType = 'SINGLE' 
                  THEN ( SUM(SelfAmount) +   SUM(UnitAmount) * 
                         ( SELECT bcm.PromoteeQuota 
                           FROM dbo.BusinessCalcMaster AS bcm 
                           WHERE bcm.PlanType = mb.PlanType AND bcm.PlanName = mb.PlanName AND bcm.Year = mb.Year ) 
                       ) / 100 
             END AS Amount11,
             mb.BrokerId,
             mb.InstallmentNo
      FROM dbo.MemberBusiness AS mb
      WHERE ( ( mb.PlanType = 'MULTIPLE' AND 
                ( ( mb.Mode = 'HLY' AND mb.InstallmentNo = 2 ) OR 
                  ( mb.Mode = 'QLY' AND mb.InstallmentNo >= 2 AND mb.InstallmentNo <= 4 ) OR 
                  ( mb.Mode = 'MLY' AND mb.InstallmentNo >= 2 AND mb.InstallmentNo <= 12 ) OR 
                  mb.InstallmentNo = 1
                )
              ) OR 
              mb.PlanType = 'SINGLE'
            ) AND 
           mb.Date >= '2013-02-01 00:00:00.000' AND mb.Date <= '2013-02-28 00:00:00.000' 
      GROUP BY  mb.BrokerId , mb.Mode , mb.Year , mb.PlanName , mb.PlanType , mb.InstallmentNo 
     ) AS T
  ON B.BrokerId = T.BrokerId
GROUP BY B.BrokerId
HAVING SUM(T.Amount11) > 15000