我已经查看了其他类似的问题,但是我的查询中没有一个Where
子句,这让我对如何解决它有点困惑。
INSERT INTO #tmpCombined
SELECT A.Region
, 'MyLocation' AS 'Location'
, c.Id AS 'CompID'
, c.Name AS 'CompName'
, 'Cash' AS 'Context'
, isnull( sum( CASE WHEN T.TranAge >= 0 and CASE WHEN T.TranAge < 1 THEN 1 ELSE 0 END,0 )) AS 'ZeroDays0'
, isnull( sum( CASE WHEN T.TranAge >= 1 and CASE WHEN T.TranAge < 8 THEN 1 ELSE 0 END ), 0 ) AS '1to7Days'
, isnull( sum( CASE WHEN T.TranAge >= 8 and CASE WHEN T.TranAge < 16 THEN 1 ELSE 0 END ), 0 ) AS '8to15Days'
, isnull( sum( CASE WHEN T.TranAge >= 16 and CASE WHEN T.TranAge < 20 THEN 1 ELSE 0 END ), 0 ) AS '16to20Days'
, isnull( sum( CASE WHEN T.TranAge >= 20 THEN 1 ELSE 0 END ), 0 ) AS '20DaysPlus'
, COUNT(1) AS 'Total'
, A.Level as 'Level'
, sum(T.USDDifference) AS 'USDRevaluation'
from MyLocation.dbo.Company C
inner join #tmpCashTBResults T on T.CompanyId = C.Id
inner join #tmpCashAccounts A on T.CompanyId = A.CompanyId
group by A.Region, C.Id, C.Name, A.Level