条件分支SQL语句<一> Case When

SELECT

SUM(CASE WHEN dbo.CP_Withdrawal.CateId=5 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END ) AS MoneyIn,

SUM(CASE WHEN dbo.CP_Withdrawal.CateId=6 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END ) AS MoneyOut,

SUM(CASE WHEN dbo.CP_Withdrawal.CateId=1 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END ) AS BetMoney,

SUM(CASE WHEN dbo.CP_Withdrawal.CateId=4 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END ) AS PctMoney,

SUM(CASE WHEN dbo.CP_Withdrawal.CateId=3 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END ) AS WinMoney,

(SUM(CASE WHEN dbo.CP_Withdrawal.CateId=3 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END )+SUM(CASE WHEN dbo.CP_Withdrawal.CateId=4 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END )-SUM(CASE WHEN dbo.CP_Withdrawal.CateId=1 THEN dbo.CP_Withdrawal.Moneyoperate ELSE 0 END )) AS ProfitLoss,

dbo.CP_Users.Id AS UID,

dbo.CP_Users.Username AS UserID,

dbo.CP_Users.Role AS RoleName

 FROM dbo.CP_Users LEFT JOIN dbo.CP_Withdrawal ON dbo.CP_Users.Id=dbo.CP_Withdrawal.Uid

 WHERE dbo.CP_Users.Id={0}   AND dbo.CP_Withdrawal.Time BETWEEN '{1}'
上一篇:c/c++ 大于等于 大于 时间效率比较


下一篇:Codeforces739E Gosha is hunting