SQL Server 2008
Hi
I have an issue where i have two of the same accounts which i would like to merge together, the list looks like below:
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
So basically i would like to merge it so it looks like this:
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
However i can’t think of the best way to code this in SQL in my stored proc. I can only really think of a merge or a union, but i only want it to merge where there are two capital accounts, a capital account is signified by the ‘CCY’ AcctType and AcctSubType ‘SET’.
A point in the right direction would be much appreciated.
Thanks
EDIT: Actual stored proc
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
How about this:
EDIT: Updated to support multiple currencys.
The only change is to wrap the final
SELECT - EXCEPTin parantheses.