简体   繁体   中英

SQL Server -Multiple rows in Sub Query

This query working of single row but, i want to subtract multiple rows

SELECT(
(SELECT num1 from table1 where T_Type=2) - 
(SELECT num2 from table1 where T_Type=3))as
[SUBSTRACTION]

尝试

SELECT t1.num1-t2.num2 from table1 t1,table1 t2 where t1.T_Type=2 and t2.T_Type=3

How about SUMming the num's based on the types and subtracting these results, like this:

SELECT (  
(SELECT SUM(num1) FROM table1 WHERE T_Type = 2) -  
(SELECT SUM(num2) FROM table1 WHERE T_Type = 3))  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM