SQL Server 2008
您好
我有一个问题,我有两个相同的帐户,我想合并在一起,列表如下所示:
AcctID AcctType AcctSubType Curr TransType Amount
1 CCY SET EUR Opening 1000
1 CCY SET EUR BUY -100
1 CCY SET EUR SEL 100
1 CCY SET EUR Closing 1000
2 CCY SET EUR Opening 2000
2 CCY SET EUR SEL 100
2 CCY SET EUR Closing 2100
3 CCY INC EUR Opening 1000
3 CCY INC EUR SEL 200
3 CCY INC EUR BUY -100
3 CCY INC EUR Closing 1100
所以基本上我想合并它,所以它看起来像这样:
AcctID AcctType AcctSubType Curr TransType Amount
1 CCY SET EUR Opening 3000
1 CCY SET EUR BUY -100
1 CCY SET EUR SEL 100
2 CCY SET EUR SEL 100
1 CCY SET EUR Closing 3100
3 CCY INC EUR Opening 1000
3 CCY INC EUR SEL 200
3 CCY INC EUR BUY -100
3 CCY INC EUR Closing 1100
但是我无法想到在我的存储过程中在SQL中编写代码的最佳方法。我只能想到合并或工会,但我只希望合并有两个资本账户,资本账户由'CCY'AcctType和AcctSubType'Set'表示。
非常感谢正确方向上的一点。
由于
编辑:实际存储过程
CREATE TABLE #Workbook
(ID INT
,PortfolioID VARCHAR(20)
,PortfolioName VARCHAR(255)
,InstrumentID VARCHAR(20)
, IssueID VARCHAR(50)
, CashAccountInstrumentID VARCHAR(20)
, CashAccountName VARCHAR(255)
, CashAccountType CHAR(3)
, CashAccountSubType CHAR(3)
, TransactionClass VARCHAR(10)
, TransactionType VARCHAR(20)
, EffPostDate DATETIME
, TradeDate DATETIME
, SettleDate DATETIME
, CcySettle VARCHAR(3)
, DateItem DATETIME
, RunningTotalDate DATETIME
, Detail VARCHAR(300)
, ProceedsLocal FLOAT
, Total FLOAT
, RunningTotal FLOAT
, ReconValue FLOAT
, ReconNotes VARCHAR(50)
, RecordType VARCHAR(1)
, Sequence INT
, RecordNum INT
, TransactionID VARCHAR(20)
)
INSERT INTO #Workbook
EXEC dbo.usp_Generic_ReconCashAccount_NEW '2010-10-01', '2010-10- 07', '', 'OOGENHF', 'SS','','','','',''
DELETE FROM #Workbook
WHERE TransactionType = 'BBA'
OR (ProceedsLocal = 0 AND RecordType = 'T')
;;with needed as
(
select * from #Workbook w
WHERE w.CashAccountType = 'CCY'
AND w.CashAccountSubType = 'SET'
AND w.TransactionType In ('Opening','Closing')
AND w.CcySettle = w.CcySettle
)
select
min(ID)
, PortfolioID
, PortfolioName
, InstrumentID
, IssueID
, min(CashAccountInstrumentID) as CashAccountInstrumentID
, min(CashAccountName) as CashAccountName
, CashAccountType
, CashAccountSubType
, TransactionClass
, TransactionType
, EffPostDate
, TradeDate
, SettleDate
, CcySettle
, DateItem
, RunningTotalDate
, Detail
, sum(ProceedsLocal) as ProceedsLocal
, sum(Total) as Total
, sum(RunningTotal) as RunningTotal
, sum(ReconValue) as ReconValue
, ReconNotes
, RecordType
, Sequence
, RecordNum
, TransactionID
from needed
group by ID,PortfolioID,PortfolioName,InstrumentID,IssueID,CashAccountInstrumentID,CashAccountName,CashAccountType,CashAccountSubType,TransactionClass,TransactionType,EffPostDate,TradeDate,SettleDate
,CcySettle, DateItem,RunningTotalDate,Detail,ProceedsLocal,Total,RunningTotal,ReconValue,ReconNotes,RecordType,Sequence,RecordNum,TransactionID
UNION ALL
(
select * from #Workbook
except
select * from needed
)
drop table #Workbook
答案 0 :(得分:1)
这个怎么样:
;with test (AcctID, AcctType, AcctSubType, Curr, TransType, Amount)
as
(
select 1, 'CCY', 'SET', 'EUR', 'Opening', 1000
union all
select 1, 'CCY', 'SET', 'EUR', 'BUY', -100
union all
select 1, 'CCY', 'SET', 'EUR', 'SEL', 100
union all
select 1, 'CCY', 'SET', 'EUR', 'Closing', 1000
union all
select 2, 'CCY', 'SET', 'EUR', 'Opening', 2000
union all
select 2, 'CCY', 'SET', 'EUR', 'SEL', 100
union all
select 2, 'CCY', 'SET', 'EUR', 'Closing', 2100
union all
select 3, 'CCY', 'INC', 'EUR', 'Opening', 1000
union all
select 3, 'CCY', 'INC', 'EUR', 'SEL', 200
union all
select 3, 'CCY', 'INC', 'EUR', 'BUY', -100
union all
select 3, 'CCY', 'INC', 'EUR', 'Closing', 1100
union all
select 4, 'CCY', 'SET', 'SEK', 'Opening', 2000
union all
select 4, 'CCY', 'SET', 'SEK', 'SEL', 100
union all
select 4, 'CCY', 'SET', 'SEK', 'Closing', 2100
)
, needed as
(
select *
from test
where AcctType = 'CCY'
and AcctSubType = 'SET'
and TransType in ('Opening','Closing')
)
select min(AcctID) as AcctID
,AcctType
,AcctSubType
,Curr
,TransType
,sum(Amount) as Amount
from needed
group by
AcctType
,AcctSubType
,Curr
,TransType
union all
(
select *
from test
except
select *
from needed
)
编辑:已更新以支持多种货币。
唯一的变化是将最后的SELECT - EXCEPT
括在括号中。