0

我正在使用 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 的经纪人的数量。

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

4

1 回答 1

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
于 2013-05-25T11:08:33.957 回答