How to Merge two rows into one?

  • Hi

    I'm trying to write a query that combines two rows into one with specific columns.

    I'm using SQL 2000

    Here is the SQL

    select orig_branch_no,

    acct_no,

    amt,

    description,

    create_dt,

    charge_code,

    tran_code

    from history

    where tran_code in (262, 157)

    and description like '%DDAC%'

    and create_dt = '20110802'

    and acct_no = '000000000'

    Below are the results.

    Branch---Acct #----------Amt--------Description--------------Charge_code---Tran_code

    410------00000000000----9050.00-------DDACC KABILI-----------756------------262

    410------00000000000----5.00----------FEE ON DDACC KABILI----756------------157

    410------00000000000----7250.00-------DDACC LIMBIKANI--------756------------262

    410------00000000000----7.5-----------FEE ON DDAC LIMBIKANI--756------------157

    And after merging the results should look like this

    Branch---Acct #----------Amt--------Description--------------Charge_code---Tran_code---Chargeamount

    410------00000000000----9050.00-------DDACC KABILI------------157-------------262----------5.00

    410------00000000000----7250.00-------DDACC LIMBIKANI---------157-------------262---------7.5

    Instead of returning four rows it should only return two rows, is this possible?

    Regards,

  • Sorry Guys i got it right

    Here is the query

    select orig_branch_no,

    acct_no,

    amt,

    description,

    create_dt,

    charge_code,

    tran_code, (select t2.tran_code from history as t2

    where t2.tran_code in (157)

    and t2.description like '%DDAC%'

    and t2.create_dt = '20110802'

    and t2.acct_no = t1.acct_no) as 'Charge Code',

    (select t3.amt from history as t3

    where tran_code in (157)

    and t3.description like '%DDAC%'

    and t3.create_dt = '20110802'

    and t3.acct_no = t1.acct_no) as 'Charge Amount'

    from history as t1

    where tran_code in (262)

    and description like '%DDAC%'

    and create_dt = '20110802'

    and acct_no = 000000000

    And how do I delete a post if I posted it by mistake?

  • shani19831 (4/24/2012)


    Sorry Guys i got it right

    Here is the query

    select orig_branch_no,

    acct_no,

    amt,

    description,

    create_dt,

    charge_code,

    tran_code, (select t2.tran_code from history as t2

    where t2.tran_code in (157)

    and t2.description like '%DDAC%'

    and t2.create_dt = '20110802'

    and t2.acct_no = t1.acct_no) as 'Charge Code',

    (select t3.amt from history as t3

    where tran_code in (157)

    and t3.description like '%DDAC%'

    and t3.create_dt = '20110802'

    and t3.acct_no = t1.acct_no) as 'Charge Amount'

    from history as t1

    where tran_code in (262)

    and description like '%DDAC%'

    and create_dt = '20110802'

    and acct_no = 000000000

    And how do I delete a post if I posted it by mistake?

    You can click the "EDIT" button and edit your post.

    Please post your results once more. I don't think what you posted was the right result.

    Vinu Vijayan

    For better and faster solutions please check..."How to post data/code on a forum to get the best help" - Jeff Moden[/url] 😉

  • Please post your results once more. I don't think what you posted was the right result.

    Ya its giving me weird results but I'm working around it, i will also appreciate any kind of input in order to get this sorted out.

    Here is my result at the moment..

    orig_branch_no- acct_no- amt- description- charge_code- tran_code

    410- 010300333001- 50000.000000- KASONKOMONA -756- 157

  • shani19831 (4/24/2012)


    Please post your results once more. I don't think what you posted was the right result.

    Ya its giving me weird results but I'm working around it, i will also appreciate any kind of input in order to get this sorted out.

    Can you post Create table statements and sample data for the tables involved. i would hazard a guess you dont need the 2 queries as columns but with out being able to see the data i cant say for sure.


    For faster help in answering any problems Please read How to post data/code on a forum to get the best help - Jeff Moden[/url] for the best way to ask your question.

    For performance Issues see how we like them posted here: How to Post Performance Problems - Gail Shaw[/url]

    Need to Split some strings? Jeff Moden's DelimitedSplit8K[/url]
    Jeff Moden's Cross tab and Pivots Part 1[/url]
    Jeff Moden's Cross tab and Pivots Part 2[/url]

  • If you only have maximum one "Charge" record per transaction and description always the same prefix with "FEE ON ", you can use:

    select hA.orig_branch_no,

    hA.acct_no,

    hA.amt,

    hA.description,

    hA.create_dt,

    hA.charge_code,

    hA.tran_code,

    hC.tran_code AS ChargeCode,

    hC.amt AS ChargeAmount,

    from history as hA

    left join history as hC

    on hC.tran_code = 157

    and hC.orig_branch_no = hA.orig_branch_no

    and hC.acct_no = hA.acct_no

    and hC.create_dt = hA.create_dt

    and hC.Description = 'FEE ON ' + hA.Description

    where hA.tran_code = 262

    hA.description like '%DDAC%'

    hA.create_dt = '20110802'

    hA.acct_no = '000000000'

    However, it may be very slow and not very reliable query as transactions should be really joined on something better than description. May be you have some shared transaction id?

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • CREATE TABLE History

    (

    branch int,

    acct_no int,

    amt double,

    description varchar(255),

    Create_dt date,

    charge_code int,

    tran_code int

    )

    INSERT INTO History

    VALUES (410,1234567890,6000000.00,'DDACC IFO KAWASAKI','2011-08-02 00:00:00',756,262)

    INSERT INTO History

    VALUES (410,1234567890,50000.00,FEE ON DDACC IFO KAWASAKI','2011-08-02 00:00:00',756,157)

  • select hA.orig_branch_no,

    hA.acct_no,

    hA.amt,

    hA.description,

    hA.create_dt,

    hA.charge_code,

    hA.tran_code,

    hC.tran_code AS ChargeCode,

    hC.amt AS ChargeAmount,

    from history as hA

    left join history as hC

    on hC.tran_code = 157

    and hC.orig_branch_no = hA.orig_branch_no

    and hC.acct_no = hA.acct_no

    and hC.create_dt = hA.create_dt

    and hC.Description LIKE '%' +hA.Description

    where hA.tran_code = 262

    hA.description like '%DDAC%'

    hA.create_dt = '20110802'

    hA.acct_no = '000000000'

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin,

    Just out Curiosity, what’s the difference between the two queries as they return the exact same set of results?

    select t1.orig_branch_no,

    t1.acct_no,

    t1.amt as 'Transaction Amount',

    RoutingNo = NULL,

    t1.description,

    t1.tran_code, (select t2.tran_code from history as t2

    where t2.tran_code in (157)

    and t2.description like '%COMM ON DDAC%'

    and t2.create_dt = '20110802'

    and t2.acct_no = t1.acct_no) as 'Charge Code',

    (select t3.amt from history as t3

    where tran_code in (157)

    and t3.description like '%COMM ON DDAC%'

    and t3.create_dt = '20110802'

    and t3.acct_no = t1.acct_no) as 'Charge Amount'

    from history as t1

    where tran_code in (262,163)

    and description like '%DDAC%'

    and create_dt = '20110802'

    --and acct_no = '010300333001'

    --------------------------------------------------------------------

    select hA.orig_branch_no,

    hA.acct_no,

    hA.amt,

    hA.description,

    hA.create_dt,

    hA.charge_code,

    hA.tran_code,

    hC.tran_code AS ChargeCode,

    hC.amt AS ChargeAmount

    from history as hA

    left join history as hC

    on hC.tran_code = 157

    and hC.orig_branch_no = hA.orig_branch_no

    and hC.acct_no = hA.acct_no

    and hC.create_dt = hA.create_dt

    and hC.Description LIKE '%' +hA.Description

    where hA.tran_code in (262,163) and

    hA.description like '%DDAC%' and

    hA.create_dt = '20110802' and

    --hA.acct_no = '010300333001'

  • Check the query plan (Ctrl + M)

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Wow, you taught me something new, though its been there and never used it before.

    But your query does not return all data, as my query returns a few records with charge amount while yours only the first transaction and it ignores the rest,

    Should I provide you with an insert query of the two transactions and you will see that it only retrieves the first but not the second even though it meets the search criteria

  • ...

    But your query does not return all data, as my query returns a few records with charge amount while yours only the first transaction and it ignores the rest...

    The query does only return records which satisfy WHERE conditions.

    You can relax conditions under WHERE clause and it will start to return more records.

    The join between "amount" and "charge" type historical records is based on the code. It may be wrong, but it's what you gave us.

    As I said before, trying to match "charge" to "amount" record just based on description is not very good idea. You should have some sort of correlating transaction id.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply