当前位置: 代码迷 >> Sql Server >> case when.then.请问
  详细解决方案

case when.then.请问

热度:55   发布时间:2016-04-27 20:08:25.0
case when...then...请教
(CASE   WHEN   P.Payer   = '113001 '  
  THEN   SELECT   SUM(NVL(AcceptPrice,0))   FROM   IBS_T_BID_SURROGATE   AGT   WHERE   AGT.PROJECT_ID   =   P.PROJID
  ELSE
      SELECT   SUM(NVL(BC.ReceiveChargePrice,0))   FROM   IBS_T_BID_BIDCHARGE   BC
      WHERE   BC.PROJID   =   P.PROJID
  END
  )   AS   SER_AMOUNT,
上面是查询语句中的一段   但是有问题   报错缺少表达式   then后面可不可以是一个查询语句呢?

------解决方案--------------------
(CASE WHEN P.Payer = '113001 '
THEN (SELECT SUM(isnull(AcceptPrice,0)) FROM IBS_T_BID_SURROGATE AGT WHERE AGT.PROJECT_ID = P.PROJID)
ELSE
(SELECT SUM(isnull(BC.ReceiveChargePrice,0)) FROM IBS_T_BID_BIDCHARGE BC
WHERE BC.PROJID = P.PROJID)
END
) AS SER_AMOUNT,

------解决方案--------------------
--这样试下
(CASE WHEN P.Payer = '113001 '
THEN (SELECT SUM(NVL(AcceptPrice,0)) FROM IBS_T_BID_SURROGATE AGT WHERE AGT.PROJECT_ID = P.PROJID)
ELSE
(SELECT SUM(NVL(BC.ReceiveChargePrice,0)) FROM IBS_T_BID_BIDCHARGE BC
WHERE BC.PROJID = P.PROJID)
END
) AS SER_AMOUNT
------解决方案--------------------
(CASE WHEN P.Payer = '113001 '
THEN (SELECT SUM(isnull(AcceptPrice,0)) FROM IBS_T_BID_SURROGATE AGT WHERE AGT.PROJECT_ID = P.PROJID
ELSE
(SELECT SUM(isnull(BC.ReceiveChargePrice,0)) FROM IBS_T_BID_BIDCHARGE BC
WHERE BC.PROJID = P.PROJID
END
) AS SER_AMOUNT,
------解决方案--------------------
這樣改:then 後面可以跟sql語句的
不過要用括號括起來


(CASE WHEN P.Payer = '113001 '
THEN (SELECT SUM(NVL(AcceptPrice,0)) FROM IBS_T_BID_SURROGATE AGT WHERE AGT.PROJECT_ID = P.PROJID)
ELSE
(SELECT SUM(NVL(BC.ReceiveChargePrice,0)) FROM IBS_T_BID_BIDCHARGE BC
WHERE BC.PROJID = P.PROJID)
END
) AS SER_AMOUNT,
上面是查询语句中的一段 但是有问题 报错缺少表达式 then后面可不可以是一个查询语句呢?
  相关解决方案