2012-10-11 62 views
2

因此,我已經在#table1中計算了一個數字,我想在另一個表中顯示該數字。 所以對於第一計數語法使用另一個表中的一個表的計數

select COUNT(*) 
into #table1 
from #test8 
where account_number1 is null 
GROUP BY varmonth,MONYEAR 
ORDER BY varmonth, MONYEAR 

那麼第二個表是

SELECT MONYEAR, 
COUNT(*) AS TOTAL, 
SUM(CURRENT_BALANCE_AMOUNT) REH_BAL, 
FROM #table1 
WHERE ROWNUMBER = 1 
GROUP BY varmonth,MONYEAR 
ORDER BY varmonth, MONYEAR 

不過,我想,首先計數到第二個表的計數和總和之間的滑動。 有沒有辦法做到這一點,所有的數字輸出在一個決賽桌? 感謝

+0

請提供樣本數據和所需的輸出。 – RedFilter

回答

1

你可以這樣做:

SELECT 
    t2.MONYEAR, 
    t2.Total, 
    t1.tcount, 
    t2.REH_BAL 
FROM 
(
    select varmonth, MONYEAR, COUNT(*) tcount 
    from #test8 
    where account_number1 is null 
    GROUP BY varmonth,MONYEAR 
) t1 
INNER JOIN 
(
    SELECT MONYEAR, 
    COUNT(*) AS TOTAL, 
    SUM(CURRENT_BALANCE_AMOUNT) REH_BAL, 
    FROM #table1 
    WHERE ROWNUMBER = 1 
    GROUP BY varmonth,MONYEAR 
) t2 ON t1.MONYEAR = t2.MONYEAR 
ORDER BY t1.varmontth, t2.MONYEAR 
相關問題