如何在sqlserver中将一个表减去另一个表

时间:2016-08-31 09:15:57

标签: sql-server sql-server-2008

我有一张桌子Fee_Payable_to_Students。要组织此表的值,我将其编码为

create PROC [dbo].[Fee_Fee_Demand](@S_Adm_No NVARCHAR(50))
AS
BEGIN
    SELECT cls_SecId, S_Adm_No, Installment, 
    SUM(Amount) AS Amount, CASE 

    WHEN  Installment = 'Quarter-1 (April, May & June)' THEN 'Apr 15, 2017'     
    WHEN  Installment = 'Quarter-2 (July, August & September)' THEN 'Jul 15, 2017'      
    WHEN  Installment = 'Quarter-3 (October, November & December)' THEN 'Oct 15, 2017'      
    WHEN  Installment = 'Quarter-4 (January, February & March)' THEN 'Jan 15, 2018' END AS Payable_Date 

    FROM (  SELECT cls_SecId, S_Adm_No, 
            Apr + May + Jun AS [Quarter-1 (April, May & June)],     
            Jul + Aug + Sep AS [Quarter-2 (July, August & September)],  
            Oct + Nov + Dec AS [Quarter-3 (October, November & December)],  
            Jan + Feb + Mar AS [Quarter-4 (January, February & March)]  
    FROM Fee_Payable_to_Students where S_Adm_No=@S_Adm_No) AS Pvt UNPIVOT 
            (Amount FOR Installment IN 
                                    ([Quarter-1 (April, May & June)], 
                                    [Quarter-2 (July, August & September)], 
                                    [Quarter-3 (October, November & December)], 
                                    [Quarter-4 (January, February & March)])) AS unPvt 
        GROUP BY cls_SecId, S_Adm_No, Installment
end

值显示为

Installment                             | Amount | Payable_Date 
Quarter-1 (April, May & June)           |        | Apr 15, 2017
Quarter-2 (July, August & September)    |        | Jul 15, 2017
Quarter-3 (October, November & December)|        | Oct 15, 2017
Quarter-4 (January, February & March)   |        | Jan 15, 2018

除此之外,还有一张名为Fee_Receipt的桌子。

现在我的问题是Fee_receipt是否具有类似

的值
S_Amn_No |Fhead | Apr | May |Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar
1001   |1     | 100 | 100 |100 |  0  |  0  |  0  |   0 |   0 |   0 |   0 |   0 |  0
1001   |2     | 100 | 100 |100 |   0 |  0  |  0  |   0 |   0 |   0 |   0 |   0 |  0
1001   |3     | 100 | 100 |100 |   0 |  0  |  0  |   0 |   0 |   0 |   0 |   0 |  0

然后该值应显示为

Installment                             | Amount         | Status  | Payable_Date 
Quarter-1 (April, May & June)           | assumed(900)   | FeePaid | Apr 15, 2017
Quarter-2 (July, August & September)    |                | Pending | Jul 15, 2017
Quarter-3 (October, November & December)|                | Pending | Oct 15, 2017
Quarter-4 (January, February & March)   |                | Pending | Jan 15, 2018

如果Fee_receipt的值类似

S_Amn_No |Fhead | Apr | May |Jun | Jul | Aug | Sep | Oct | Nov | Dec | Jan | Feb | Mar
1001   |1     | 100 | 100 |100 | 100 | 100 |100  |   0 |   0 |   0 |   0 |   0 |  0
1001   |2     | 100 | 100 |100 | 100 | 100 |  0  |   0 |   0 |   0 |   0 |   0 |  0
1001   |3     | 100 | 100 |100 | 100 | 0   |  0  |   0 |   0 |   0 |   0 |   0 |  0

然后该值应显示为

Installment                             | Amount         | Status  | Payable_Date 
Quarter-1 (April, May & June)           | assumed(900)   |  Paid   | Apr 15, 2017
Quarter-2 (July, August & September)    | assumed(1200)  |  600    | Jul 15, 2017
Quarter-3 (October, November & December)| assumed(1200)  | 1200    | Oct 15, 2017
Quarter-4 (January, February & March)   | assumed(1200)  | 1200    | Jan 15, 2018

明智地再两个季度。

0 个答案:

没有答案