带有 CASE 条件和 SUM() 的 SELECT 查询

编程入门 行业动态 更新时间:2024-10-17 19:29:43
本文介绍了带有 CASE 条件和 SUM() 的 SELECT 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在使用这些 sql 语句.我的表有 CPaymentType 字段,其中包含现金"或支票".我可以通过执行如下所示的 2 条 SQL 语句来总结付款金额.在这种情况下,用户甚至不会注意到执行 2 条 sql 语句或仅 1 条时的速度差异,但是,我不喜欢我的方式,我只想要 1 条 sql 语句.如何使用 CASE 条件将这些重构为 1 条语句?我无法弄清楚,因为在线示例导致 1 或 0 或布尔值.我不希望包含远期支票付款.非常感谢.

I'm currently using these sql statements. My table has the field CPaymentType which contains "Cash" or "Check". I can sum up the amount of payments by executing 2 SQL statements as shown below. In this case, the user won't even notice the speed difference when executing 2 sql statements or just 1, however, I don't like my way, I just want 1 sql statement. How do I reconstruct these into 1 statement with CASE conditions? I can't figure it out since examples online result in either 1 or 0 or boolean. I don't want the postdated Check payments to be included. Thank you very much.

Select SUM(CAmount) as PaymentAmount from TableOrderPayment where CPaymentType='Cash' and CStatus='Active'; Select SUM(CAmount) as PaymentAmount from TableOrderPayment where CPaymentType='Check' and CDate<=SYSDATETIME() and CStatus='Active';

推荐答案

Select SUM(CASE When CPayment='Cash' Then CAmount Else 0 End ) as CashPaymentAmount, SUM(CASE When CPayment='Check' Then CAmount Else 0 End ) as CheckPaymentAmount from TableOrderPayment Where ( CPayment='Cash' Or CPayment='Check' ) AND CDate<=SYSDATETIME() and CStatus='Active';

更多推荐

带有 CASE 条件和 SUM() 的 SELECT 查询

本文发布于:2023-11-22 06:01:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616237.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   CASE   SELECT   SUM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!