如何从同一列,同一表中减去值? [英] how to subtract value from same column, same table?
问题描述
您好b $ b
我有这两列。
Hi
I have these 2 columns.
code | total ($)
GROSS | 2500
BASIC | 2000
NET | 2250
我想从减法中创建一个名为差异的新列。
结果我希望通过减去来自同一列的GROSS和NET来获得。
结果是 - > 2500 - 2250 = 250
I want to create a new column called "difference" from the subtraction.
the results I want to have is obtained by subtracting "GROSS" and "NET" which is from the same column.
Which results to be --> 2500 - 2250 = 250
推荐答案
)
GROSS | 2500
BASIC | 2000
NET | 2250
) GROSS | 2500 BASIC | 2000 NET | 2250
我想从减法中创建一个名为差异的新列。
结果我希望通过减去来自同一列的GROSS和NET来获得。
结果是 - > 2500 - 2250 = 250
I want to create a new column called "difference" from the subtraction.
the results I want to have is obtained by subtracting "GROSS" and "NET" which is from the same column.
Which results to be --> 2500 - 2250 = 250
如果有两列总数n
if there are two columns gross n net then
select GROSS, NET, 'DIFFRENCE' = GROSS-NET From tableName
如果记录在不同的行中,
if record in different rows,
select SUM(GROSS) as GROSS, SUM(NET) as NET, 'DIFFERENCE' = SUM(GROSS)-SUM(NET) FROM
(
select Total as GROSS, 0 as NET From tableName where Code='GROSS'
union all
select 0 as GROSS, TOTAL as NET From tableName where Code='NET'
) as a
快乐编码!
:)
Happy Coding!
:)
SELECT
(SELECT t1.total FROM tablename t1 WHERE t1.code='GROSS') AS GROSS,
(SELECT t2.total FROM tablename t2 WHERE t2.code='NET') AS NET,
(SELECT t3.total FROM tablename t3 WHERE t3.code='GROSS') -
(SELECT t4.total FROM tablename t4 WHERE t4.code='NET') AS difference
这篇关于如何从同一列,同一表中减去值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!