我一直致力于为我继承的金融系统撰写报告,而且代码真是一团糟。目前,当前代码执行多个查询并执行过程以构建公共列表,然后将其用作DevExpress网格控件的数据源。在这个应用程序的其他部分工作后,我发现在数据库中执行所有计算要高效得多,因为它们非常基础。检索和操纵数字非常简单,但我现在仍然试图完成报告的最后一个方面:分类帐。
基本上,我需要执行计算,并根据正面或负面的值,在两行(借方或贷方)中的一行中显示这些值。刚刚学习了存储过程,我一直坚持如何做到这一点。我有我的代码和所有声明的变量“设置”但我不知道如何将它们“分配”到行,因为没有这个表,我真的不确定我将如何去做这个
有人能引导我朝正确的方向前进吗?我不是要求有人为我编码,我不知道如何定义我需要做什么来研究方法。
下面是当前的select语句,它输出正确的数字,但都在一行中:
set @OpeningBalance = -3; /**some real calculations here**/
if @OpeningBalance is null
begin
set @OpeningBalance = 0
end;
set @ReserveAmount = 569119.60; /**in place of the real calculations **/
set @ReserveTotal = (@OpeningBalance + @ReserveAmount);
set @ReserveRate = 0.25; /**in place of the select statement calculations **/
set @RequiredReserve = (@ReserveAmount);
set @ReserveRefund = (@ReserveTotal - @RequiredReserve );
set @UnfinancedInvoices = 21026087.59; /**in place of the real calculations **/
set @ReserveBalance = (@RequiredReserve + @UnfinancedInvoices);
SELECT @OpeningBalance as 'OpeningBalance',
@ReserveAmount as 'ReserveAmount',
@ReserveTotal as 'ReserveTotal',
@ReserveRate as 'ReserveRate',
@RequiredReserve as 'RequiredReserve',
@ReserveRefund as 'ReserveRefund',
@UnfinancedInvoices as 'UnfinancedInvoices',
@ReserveBalance as 'ReserveBalance'
END
我更喜欢“0.00”值而不是空格
我想也许我可以为select语句提供一个ID,但我甚至不确定我在SQL中需要做什么(尽管如果不是我会感到惊讶)
请帮助!
答案 0 :(得分:0)
您只需在查询中添加一个附加列,为第一行分配一个值,为第二行分配另一个值,然后将其分组:
declare @t table(amt int, cust nvarchar(50));
insert into @t values(1,'c1'),(-5,'c1'),(4,'c1'),(7,'c2'),(-9,'c2');
select cd
,Customer
,TotalAmount
from(
select case when amt < 0 then 'c' else 'd' end as cd
,cust as Customer
,sum(amt) as TotalAmount
from @t
group by case when amt < 0 then 'c' else 'd' end
,cust
) a
order by Customer
,cd;
输出:
+----+----------+-------------+
| cd | Customer | TotalAmount |
+----+----------+-------------+
| c | c1 | -5 |
| d | c1 | 5 |
| c | c2 | -9 |
| d | c2 | 7 |
+----+----------+-------------+
答案 1 :(得分:0)
我仍然不确定您要查找的确切格式,但是下面将每个变量的值放入两行之一,基于它是正数还是负数。为清晰起见,我添加了Type
列,并假设第一列和最后一列始终显示在两行中。
SELECT 'Credit' as [Type],
@OpeningBalance as [OpeningBalance],
CASE WHEN @ReserveAmount >= 0 THEN @ReserveAmount ELSE 0.00 END as [ReserveAmount],
CASE WHEN @ReserveTotal >= 0 THEN @ReserveTotal ELSE 0.00 END as [ReserveTotal],
CASE WHEN @ReserveRate >= 0 THEN @ReserveRate ELSE 0.00 END as [ReserveRate],
CASE WHEN @RequiredReserve >= 0 THEN @RequiredReserve ELSE 0.00 END as [RequiredReserve],
CASE WHEN @ReserveRefund>= 0 THEN @ReserveRefund ELSE 0.00 END as [ReserveRefund],
CASE WHEN @UnfinancedInvoices >= 0 THEN @UnfinancedInvoices ELSE 0.00 END as [UnfinancedInvoices],
@ReserveBalance as [ReserveBalance]
UNION ALL
SELECT 'Debit' as [Type],
@OpeningBalance as [OpeningBalance],
CASE WHEN @ReserveAmount <= 0 THEN @ReserveAmount ELSE 0.00 END as [ReserveAmount],
CASE WHEN @ReserveTotal <= 0 THEN @ReserveTotal ELSE 0.00 END as [ReserveTotal],
CASE WHEN @ReserveRate <= 0 THEN @ReserveRate ELSE 0.00 END as [ReserveRate],
CASE WHEN @RequiredReserve <= 0 THEN @RequiredReserve ELSE 0.00 END as [RequiredReserve],
CASE WHEN @ReserveRefund <= 0 THEN @ReserveRefund ELSE 0.00 END as [ReserveRefund],
CASE WHEN @UnfinancedInvoices <= 0 THEN @UnfinancedInvoices ELSE 0.00 END as [UnfinancedInvoices],
@ReserveBalance as [ReserveBalance]