2017-04-11 21 views
1

我一直在爲一個我繼承的財務系統的報告工作,而且代碼真的是一團糟。目前,當前的代碼執行多個查詢並執行過程來構建公用列表,然後將其用作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 

使用上述領域,我需要類似模擬了以下的輸出: Mockup of the desired output

我寧願「0.00」值而不是空格

我想也許我可以有一個ID爲選擇語句,但我甚至不知道我需要做什麼是可能的SQL(雖然我'如果不是,我會感到驚訝)

請幫助!

+1

請你能提供一個你想如何看輸出的樣本?我無法想象這個問題的預期結果。 – LunarSage

+0

是的,你一定需要提供樣本數據。你是說你需要返回兩行而不是一行嗎? –

+0

@LunarSage請回顧一下,我已經添加了輸出的樣機。 – Daniel

回答

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] 
+0

謝謝農曆,這是我最初想到的,但無法確定它的工作原理。當我使用你的select語句時,在'[Type]'後面的'@ OpeningBalance'中出現以下錯誤:'@OpeningBalance'附近語法不正確。'[Type]' 'Msg 102,Level 15,State 1,Line 59。 Msg 102,Level 15,State 1,Line 69 '@ OpeningBalance'附近語法不正確。' – Daniel

+0

我錯過了一個逗號,再給它一次 – LunarSage

+0

謝謝,這看起來很完美 – Daniel

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   | 
+----+----------+-------------+ 
+0

謝謝,這在我在查詢編輯器中執行時起作用,您可以在這裏解釋邏輯,以便將其應用於我的場景。我想我理解它,但我會很感激像兩列這樣的例子 – Daniel

+0

@Daniel你基本上是在你的輸出中添加一個計算列,它是根據你的金額值設置的。如果您在問題中添加一些虛擬數據,我可以更具體地向您展示一個更合適的示例。我編輯了我的答案,試圖更好地展示發生的事情。 – iamdave

+0

我已經更新了我的問題,但是LunarSage的答案似乎完全按照要求工作。除非你有理由說明爲什麼這不是最有效的方法,謝謝你的努力! – Daniel