我每个月都有一组查询。他们打了一组帐号,但当他们返回时,他们显示出错开的结果:
554000 355 Transportation COS-Fuel NULL 68652.230000000000
554000 355 Transportation COS-Fuel 48923.270000000000 NULL
我希望他们能够联合起来(那只是两个月,屁股12会变得混乱)
554000 355 Transportation COS-Fuel 48923.270000000000 68652.230000000000
以下是我设置的方法:
---JAN Actuals
set @month = '1'
SET @1Start = (SELECT StartDate FROM MiscReportTables.dbo.FiscalCalendar WHERE @year = [Year] AND Period = 1)
SET @1End = (SELECT EndDate FROM MiscReportTables.dbo.FiscalCalendar WHERE @year = [Year] AND Period = 1)
Select distinct
LEDGERTRANS.Dimension as 'Department',
DIMENSIONS.DESCRIPTION as 'Division',
LedgerTable.AccountName as 'GL Description',
Case When @month = '1' Then SUM(AMOUNTMST) END as '1',
Case When @month = '2' Then SUM(AMOUNTMST) END as '2',
Case When @month = '3' Then SUM(AMOUNTMST) END as '3',
Case When @month = '4' Then SUM(AMOUNTMST) END as '4',
Case When @month = '5' Then SUM(AMOUNTMST) END as '5',
Case When @month = '6' Then SUM(AMOUNTMST) END as '6',
Case When @month = '7' Then SUM(AMOUNTMST) END as '7',
Case When @month = '8' Then SUM(AMOUNTMST) END as '8',
Case When @month = '9' Then SUM(AMOUNTMST) END as '9',
Case When @month = '10' Then SUM(AMOUNTMST) END as '10',
Case When @month = '11' Then SUM(AMOUNTMST) END as '11',
Case When @month = '12' Then SUM(AMOUNTMST) END as '12'
Into #tempJan
from LEDGERTRANS
Join LedgerTable on LedgerTable.Accountnum = Ledgertrans.AccountNum
Join Dimensions on NUM = LEDGERTRANS.Dimension2_
where transdate BETWEEN @1Start AND @1End and LedgerTrans.Dimension2_ in (@division)
Group By LEDGERTRANS.AccountNum,LEDGERTRANS.Dimension,LEDGERTRANS.Dimension2_,LedgerTable.AccountName,DIMENSIONS.DESCRIPTION
---FEB Actuals
set @month = '2'
SET @2Start = (SELECT StartDate FROM MiscReportTables.dbo.FiscalCalendar WHERE @year = [Year] AND Period = dbo.fnGetPeriod(2,@month))
SET @2End = (SELECT EndDate FROM MiscReportTables.dbo.FiscalCalendar WHERE @year = [Year] AND Period = dbo.fnGetPeriod(2,@month))
Select distinct
LEDGERTRANS.Dimension as 'Department',
DIMENSIONS.DESCRIPTION as 'Division',
LedgerTable.AccountName as 'GL Description',
Case When @month = '1' Then SUM(AMOUNTMST) END as '1',
Case When @month = '2' Then SUM(AMOUNTMST) END as '2',
Case When @month = '3' Then SUM(AMOUNTMST) END as '3',
Case When @month = '4' Then SUM(AMOUNTMST) END as '4',
Case When @month = '5' Then SUM(AMOUNTMST) END as '5',
Case When @month = '6' Then SUM(AMOUNTMST) END as '6',
Case When @month = '7' Then SUM(AMOUNTMST) END as '7',
Case When @month = '8' Then SUM(AMOUNTMST) END as '8',
Case When @month = '9' Then SUM(AMOUNTMST) END as '9',
Case When @month = '10' Then SUM(AMOUNTMST) END as '10',
Case When @month = '11' Then SUM(AMOUNTMST) END as '11',
Case When @month = '12' Then SUM(AMOUNTMST) END as '12'
Into #tempFeb
from LEDGERTRANS
Join LedgerTable on LedgerTable.Accountnum = Ledgertrans.AccountNum
Join Dimensions on NUM = LEDGERTRANS.Dimension2_
where transdate BETWEEN @2Start AND @2End and LedgerTrans.Dimension2_ in (@division)
Group By LEDGERTRANS.AccountNum,LEDGERTRANS.Dimension,LEDGERTRANS.Dimension2_,LedgerTable.AccountName,DIMENSIONS.DESCRIPTION
SELECT distinct LedgerTable.AccountNum,
Department,
Division,
[GL Description],
[1],
[2]
from #tempFeb join LedgerTable on LedgerTable.AccountName = #tempFeb.[GL Description]
union
SELECT distinct LedgerTable.AccountNum,
Department,
Division,
[GL Description],
[1],
[2]
from #tempJan join LedgerTable on LedgerTable.AccountName = #tempJan.[GL Description]
drop table #tempJan
drop table #tempFeb
答案 0 :(得分:2)
其他选项可能是聚合:
SELECT
T.AccountNum,
T.Department,
T.Division,
SUM(ISNULL(T.JAN, 0)) AS JAN,
SUM(ISNULL(T.FEB, 0)) AS FEB
FROM (
SELECT distinct
LedgerTable.AccountNum,
Department,
Division,
[GL Description],
[1] AS JAN,
[2] AS FEB
FROM
#tempFeb
JOIN LedgerTable
ON LedgerTable.AccountName = #tempFeb.[GL Description]
UNION
SELECT distinct
LedgerTable.AccountNum,
Department,
Division,
[GL Description],
[1] AS JAN,
[2] AS FEB
FROM
#tempJan
JOIN LedgerTable
ON LedgerTable.AccountName = #tempJan.[GL Description]
) T
GROUP BY
T.AccountNum,
T.Department,
T.Division
答案 1 :(得分:1)
您需要使用联接而不是联合。 类似的东西:
select
t1.department,
t1.division,
t1.[GL Description],
t1.1,
t2.1
...
from
(
SELECT distinct LedgerTable.AccountNum,
Department,
Division,
[GL Description],
[1],
[2]
from #tempFeb join LedgerTable on LedgerTable.AccountName = #tempFeb.[GL Description]
) t1
inner join
(
SELECT distinct LedgerTable.AccountNum,
Department,
Division,
[GL Description],
[1],
[2]
from #tempJan join LedgerTable on LedgerTable.AccountName = #tempJan.[GL Description]
) t2
on t1.departement = t2.department
and t1.division = t2.division
and t1.[GL Description] = t2.[GL Description]
这不是确切的代码,特别是我不理解您的别名(您的列名为1,2)。