我在为表格
创建查询时遇到问题mouventDeStock
|| idMv || dateMv ||qteMv || typeMV(bit) || codeMv ||
如果0表示缺货,则feild类型为0或1,如果1表示股票输入
我想计算qte女巫等于
qte = ( sum(qteMv) where typeMv = 1 ) - ( sum(qteMv) where typeMv = 0 )
我怎么不能在sql中做到这一点
(select sum(mvQte) from mouvementDeStock
where (mvType = 1 and mvCode = 'ART_18'))
-
(select sum(mvQte) from mouvementDeStock
where (mvType = 0 and mvCode = 'ART_18')
但它根本不起作用
答案 0 :(得分:1)
试试这个......
Select Y.S - X.S As Sum from
(select sum(qteMv) S from mouventDeStock where typemv=1 and codemv = 'ART_18') Y,
(select sum(qteMv) S from mouventDeStock where typemv=0 and codemv = 'ART_18') X;
答案 1 :(得分:0)
您可以使用子查询来计算小计,但整个语句必须是有效的SQL语句,因此您需要在它们周围包裹另一个SELECT
:
SELECT (SELECT SUM(mvQte) FROM mouvementDeStock
WHERE mvType = 1 AND mvCode = 'ART_18')
-
(SELECT SUM(mvQte) FROM mouvementDeStock
WHERE mvType = 0 AND mvCode = 'ART_18')
如果你想要聪明一点,你可以用一笔钱来做到这一点,将所有缺货的mvQte
值乘以负值乘以-1:
SELECT SUM(mvQte * CASE mvType WHEN 0 THEN -1 ELSE 1 END)
FROM mouvementDeStock
WHERE mvCode = 'ART_18'