【发布时间】:2021-04-06 05:54:53
【问题描述】:
with cte as
(
select trd_nbr,[date],sum(case when txn_typ='A' then abs(amount) else 0 end)
amountforA,
sum(case when txn_typ='B' then abs(amount) else 0 end) amountforB,
sum(case when txn_typ='C' then abs(amount) else 0 end) amountforC
from table1
group by trd_nbr,[date]
)
select trd_nbr,[date],
(case when amountforA=amountforB and amountforB=amountforC then amountforC
else amountforA-amountforC end) Amount,
(case when amountforA=amountforB and amountforB=amountforC then 'M' else
'NM' end) Matched
from cte WHERE amountforA>0 and amountforB>0 and amountforC>0
如何在存储过程中使用上述查询?
create proc proc name()
begin
select amount, matched ,...from table1
where condition..
union all
select column1, column2.... from table2
where condition..
end;
这里的数量和匹配列取自 with 子句。怎么用啊。。
【问题讨论】:
-
请向我们展示您的尝试并解释您遇到的问题。看起来非常相似,如果与您最近关闭的问题不同的话。你不应该再问同样的问题,而是改进你的问题并等待它被重新打开。
-
你earlier question的答案有什么问题?
-
你想要的输出是什么? trd_nbr|日期|金额| |:----:|:----:|:---- |1 |2/5/21| 4000 |2 |8/12/21 |5000 ?
标签: sql sql-server tsql