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

但是我希望第一个计数在第二个表中的计数和总和之间滑动。有没有办法做到这一点,所以所有的数字都输出到一个决赛桌?谢谢

4

1 回答 1

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
于 2012-10-11T13:18:39.243 回答