Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 仅使用select和case语句,分别内联查找正数和负数的总和_Mysql_Sql Server - Fatal编程技术网

Mysql 仅使用select和case语句,分别内联查找正数和负数的总和

Mysql 仅使用select和case语句,分别内联查找正数和负数的总和,mysql,sql-server,Mysql,Sql Server,我有个问题。如果给我一张有正数和负数的表格,我想分别找到所有正数和所有负数的总和,并将其显示在一行中。我在下面想出了一个解决方案。我想检查是否有可能使用Select和CASE语句获得更好的解决方案 id amount 1 100 2 -10 3 50 4 -80 5 20 6 -20 positive negative 170 -110 我的解决方案: create table #temp (id int, amount int) inser

我有个问题。如果给我一张有正数和负数的表格,我想分别找到所有正数和所有负数的总和,并将其显示在一行中。我在下面想出了一个解决方案。我想检查是否有可能使用Select和CASE语句获得更好的解决方案

id  amount
1   100
2   -10
3   50
4   -80
5   20
6   -20

positive negative
170         -110
我的解决方案:

    create table #temp (id int, amount int)
insert into #temp values (1, 100)
insert into #temp values (2, -10)
insert into #temp values (3, 50)
insert into #temp values (4, -80)
insert into #temp values (5, 20)
insert into #temp values (6, -20)

with positive as 
(select sum(amount) as posNum from temp where amount > 0)
, negative as 
(select sum(amount) as negNum from temp where amount < 0)
select *, (select * from negative) from positive 

使用条件聚合:


使用条件聚合:

您还可以使用符号来确定它是正数还是负数

select  sum(case when sign(amount) = 1 then amount end),
        sum(case when sign(amount) = -1 then amount end)
from    #temp
您还可以使用符号来确定它是正数还是负数

select  sum(case when sign(amount) = 1 then amount end),
        sum(case when sign(amount) = -1 then amount end)
from    #temp
这将有助于:

1适用于SQL Server

SELECT
    SUM(IIF(amount > 0, amount, 0)) positive,
    SUM(IIF(amount < 0, amount, 0)) negative
FROM TABLENAME;
2适用于SQL Server和MySQL

SELECT
    SUM(CASE WHEN amount > 0 THEN amount END) positive,
    SUM(CASE WHEN amount < 0 THEN amount END) negative
FROM TABLENAME;
这将有助于:

1适用于SQL Server

SELECT
    SUM(IIF(amount > 0, amount, 0)) positive,
    SUM(IIF(amount < 0, amount, 0)) negative
FROM TABLENAME;
2适用于SQL Server和MySQL

SELECT
    SUM(CASE WHEN amount > 0 THEN amount END) positive,
    SUM(CASE WHEN amount < 0 THEN amount END) negative
FROM TABLENAME;

我需要等10分钟,然后才能做。我在一次面试中被问到这一点,面试官一心想得到这样的答案,我想不出这么简单的事情。谢谢你的帮助,你也能回答这个问题吗@kushalbhola,我已经回答了-你可以检查一下,我需要等10分钟才能回答。我在一次面试中被问到这个问题,面试官一心想得到这样的答案,我想不出这么简单的事情。谢谢你的帮助,你也能回答这个问题吗@kushalbhola,我已经回答了-你可以在那里查看,有两种完全不同的产品。你用哪一种?是两种完全不同的产品。你用哪一种?