gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:32:50 25 4
gpt4 key购买 nike

我正在为一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com