這是查詢:在SQL中使用案例進行行計算
SELECT CASE Count(RADLD.request_id)
WHEN 0 THEN (SELECT Sum(Datediff(dd, rad.clarity_dt, Getdate())))
ELSE (SELECT Sum(Datediff(dd, radld.clarity_dt, Getdate())))
END
FROM ppa.request_add_details rad,
ppa.request_add_details_loop_details radld,
ppa.request req
WHERE radld.request_id = rad.request_id
AND radld.request_id = req.request_id
AND rad.clarity_dt IS NOT NULL
AND (rad.recomm_dt IS NULL)
AND rad.modified_by !=- 1
AND rad.request_id = req.request_id
AND ((radld.loop_sequence = CASE
WHEN radld.loop_sequence IS NOT NULL THEN 1
END)
OR (radld.loop_sequence IS NULL))
這是我怎麼想它的工作:如果一個REQUEST_ID,在radld THN條目被發現,它應該去
select sum(datediff(dd,radld.clarity_dt,getdate()))
,如果沒有相應的條目在radld發現那麼就應該去
select sum(datediff(dd,rad.clarity_dt,getdate()))
..
實際發生的事情是:如果對於任何請求記錄被發現在radld ..那麼它會將select sum(datediff(dd,radld.clarity_dt,getdate()))
應用於所有記錄。
請幫
'mysql' *和*'SQL server'? –
sql server 2008 – user3345655
'CASE'是SQL Server是一個**表達式**,它可以返回一個值 - 它是** NOT **可以從ppa.request執行代碼 –