c# - sql中2个选择之间的数学

标签 c# mysql sql

我正在为一个 C# 应用程序做一个简单的任务,我想用 SQL 来做所有的数学运算。

我有 1 个名为 xx_MainOeuvre 的表,我对其执行了 2 次选择。 :

select xx_TypeMO, xx_pu from xx_MainOeuvre mo 
where mo.xx_TypeDuree != 1 and mo.xx_pu != 0 
and xx_sysParentId like 'CC1500107'

给我这个:

xx_TypeMO   |   xx_pu   |
==========================
MOI         |     45    |
MOV         |     34    |
MOE         |     50    |

select xx_TypeMO, sum(xx_Duree) as TimeSpend
from xx_MainOeuvre
where xx_TypeDuree = 1 and xx_sysParentId like 'CC1500107'
group by xx_TypeMO

女巫给我这个:

xx_TypeMO   |   TimeSpend   |
=============================
MOI         |     4         |
MOV         |     12        |

xx_pu 是单价

我想要 timeSpend * unitPrice 的结果。

像这样:

xx_TypeMO   |   RealPrice    |
==============================
MOI         |     180 (4*45) |
MOV         |     408 (12*34)|

有可能吗?还是应该继续使用我的 C# 方法?

谢谢。

最佳答案

您可以使用一个更简单的查询来完成此操作:

select xx_TypeMO,
       (sum(case when xx_TypeDuree = 1 then xx_Duree else 0 end) *
        sum(mo.xx_TypeDuree <> 1 and mo.xx_pu <> 0 )
       )
from xx_MainOeuvre
where xx_sysParentId like 'CC1500107'
group by xx_TypeMO

关于c# - sql中2个选择之间的数学,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48404067/

相关文章:

c# - 绑定(bind)会在 WPF 中造成内存泄漏吗?

c# - 在 C# 中聚合接口(interface)

c# - Botframework v4 所有用户的状态相同 C#

php - 如何最大化 Get 查询长度?

php - 找到长 TTFB 的罪魁祸首

mysql - 查询 MySQL 表中用户还没有的项目 'scored'

c# - 如何更改 DatagridviewCheckboxCell 中的复选框大小

mysql - 不是mysql中的group组函数

sql - 将表数据从一个 SQL Server 导出到另一台 SQL Server

sql - 尝试查找不同的数据时返回相同的结果