Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/70.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
Sql 需要帮助求和/分组值吗_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 需要帮助求和/分组值吗

Sql 需要帮助求和/分组值吗,sql,sql-server,tsql,Sql,Sql Server,Tsql,我不知道我是否应该在SQL中这样做。它是SSIS包的一部分,所以也许我应该使用一个脚本任务,但在我走这条路之前,我想确定这是必需的 我有一把小提琴- 以下是我的数据: create table test ( id int IDENTITY (1,1), account int, amount decimal(9,2), code varchar(10) ); insert into test (account, amount, code) values

我不知道我是否应该在SQL中这样做。它是SSIS包的一部分,所以也许我应该使用一个脚本任务,但在我走这条路之前,我想确定这是必需的

我有一把小提琴-

以下是我的数据:

create table test (
    id int IDENTITY (1,1),
    account int,
    amount decimal(9,2),
    code varchar(10)
);

insert into test (account, amount, code)
values 
      (0, 100, 'A')
      ,(0, 100, 'B')

      ,(1, 100, 'A')
      ,(1, -50, 'B')
      
      ,(2, 100, 'A')
      ,(2, 200, 'B')
      ,(2, -100, 'C')
      
      ,(3, 100, 'A')
      ,(3, -200, 'B')
      ,(3, 50, 'C')
      
      ,(4, -500, 'B')
      ,(4, -500, 'C')
      
      ,(5, -1200, 'B')
      ,(5, 1000, 'C')
      
      ,(6, 150, 'A')
      ,(6, -100, 'B')
      ,(6, 200, 'C')
      
      ,(7, 150, 'A')
      ,(7, 200, 'B')
      ,(7, -100, 'C')
      
      ,(8, 550, 'A')
      ,(8, -700, 'B')
      ,(8, 300, 'C')
      ,(8, -100, 'D');
这是我想要的输出:

-- desired output
-- 0 100.00 A
-- 0 100.00 B
-- 1 50.00 A
-- 2 200.00 B
-- 3 -50.00 A
-- 4 -500.00 B
-- 4 -500.00 C
-- 5 -200.00 B
-- 6 50.00 A
-- 6 200.00 B (currently wrong)
-- 7 50.00 A
-- 7 200.00 B (currently wrong)
-- 8 50.00 C (right now this is showing as C, but I would be okay if it was A)
基本上,如果一个帐户的所有记录都是正数,那么我应该显示所有记录,如果所有记录都是负数,则显示相同的记录,但是如果存在混合,我希望将值净值化,并将它们放入第一个代码桶中

这是我到目前为止尝试过的,尽管我一直在旋转轮子,所以一种全新的方法可能是最好的


;with cte as (
    select *
        ,sum(amount) over (PARTITION by account) as accounttotal
        ,sum(amount) over (PARTITION by account order by case when amount < 0 then 0 else 1 end, code) as runningtotal
        ,case when (select count(1) from test b where b.account = test.account and b.amount < 0) > 0 then 1 else 0 end as hasnegative
        ,ROW_NUMBER() over(partition by account order by case when amount < 0 then 0 else 1 end, code) as rownum
    from test
)
--select *  from cte 
,cte2 as (
    select 
        *
        ,max(rownum) over(partition by account ) as maxrownum 
        ,FIRST_VALUE(code) over(partition by account order by case when runningtotal > 0 then 0 else 1 end, code) as firstcode
        ,sum(case when amount < 0 then 1 else 0 end) over(partition by account) as negativecount
    from cte
)
--select * from cte2
select 
    --*,
    account as accountnumber
    ,case when hasnegative = 0 or negativecount = maxrownum then amount else runningtotal end as val
    ,case when hasnegative = 0 or negativecount = maxrownum then code else firstcode end as code

from cte2
where 
    runningtotal > 0 
    or rownum = maxrownum 
    or negativecount = maxrownum
order by account, code

不确定,但这可能是您正在寻找的答案:

SELECT  account , SUM(amount) amount , min(code) code FROM (
SELECT *, dense_RANK() OVER (PARTITION BY account , CASE WHEN amount> 0 THEN 1 ELSE -1 END ORDER BY code) rn2 FROM (
    SELECT account , SUM(amount) amount , min(code) code FROM (
        SELECT *, dense_RANK() OVER (PARTITION BY account , CASE WHEN amount> 0 THEN 1 ELSE -1 END ORDER BY id) rn FROM test) tt
    GROUP BY account , rn
    HAVING SUM(amount) <> 0 
    ) tt2
) tt3
GROUP BY account , rn2
ORDER BY account

我不明白:你怎么知道该对哪一行下网?例如,account 7在所需结果中有两行,但account 3只有一行,您希望代码的值是多少?@Charlieface感谢您的回复。我对此还不够清楚。如果负值未完全抵消第一个正值,则应将这两个值合并,并保留第三个值。所以对于7,代码A和C将被合并,但B将保持不变。但是为什么A和C,为什么B和C不呢?C是负值,它应该抵消第一个正值。或者,如果更容易添加B和C,那也很好,但现有的中断流程使用第一个代码。哦,3只有一个结果,因为负数完全删除了A和C中的所有内容。如果代码B中保留了负数,那也很好,实际上,这可能更好。我会深入研究并做一些额外的测试,但到目前为止,这看起来很棒。你是个救生员!非常感谢您的帮助。@nickfinity不客气,让我知道,我只是好奇它是否真的对您有用!