Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:23:57+00:00 2026-05-30T19:23:57+00:00

SQL Server 2008 Hi I have an issue where i have two of the

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:23:59+00:00Added an answer on May 30, 2026 at 7:23 pm

    How about this:

    ;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
    )
    

    EDIT: Updated to support multiple currencys.

    The only change is to wrap the final SELECT - EXCEPT in parantheses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple SQL Server 2008 database with two tables like this: TableA:
Does SQL Server 2008 have a a data-type like MySQL's enum ?
In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
I have SharePoint 2010 and SQL server 2008 setup on two machines. I have
I'm working with a db (SQL server 2008), and have an interesting issue with
I have an issue similar to this one: SQL Server 2008: ODBC connection problems
I've been using the MERGE operation in SQL Server 2008, but have now come
I am using SQL Server 2008 and I have this issue. I have a
I have an issue converting dates in SQL Server 2008 The value I am
I'm trying to put together a database in SQL Server Management Studio 2008 which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.