How to query results in debit and credit column

  • Hi SQL Gurus,

    I would like to run a query to display the amount in a debit and credit column from a FACT table. The value appearing in the debit or credit column depends on the account sign in the ACCOUNT table.

    FACT TABLE

    ACCOUNT | ORGANIZATION | YEAR | PERIOD | AMOUNT

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

    1001 | aaa | 2012 | 01 | 100

    1002 | aaa | 2012 | 01 | 50

    1003 | aaa | 2012 | 01 | -100

    1004 | aaa | 2012 | 01 | 90

    ACCOUNT TABLE

    ACCOUNT | DESC | SIGN

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

    1001 | 1001 | Dr

    1002 | 1002 | Cr

    1003 | 1003 | Dr

    1004 | 1004 | Cr

    How can I transform to this result?

    ACCOUNT | ORGANIZATION | YEAR | PERIOD | Dr | Cr

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

    1001 | aaa | 2012 | 01 | 100 |

    1002 | aaa | 2012 | 01 | | 50

    1003 | aaa | 2012 | 01 | -100 |

    1004 | aaa | 2012 | 01 | | 90

    Below is the DDL and sample data for this:

    DECLARE @account TABLE

    (

    ACCOUNT CHAR(4) NOT NULL,

    DESC CHAR(4) NOT NULL,

    SIGN CHAR(4) NOT NULL

    )

    DECLARE @FACT TABLE

    (

    ACCOUNT CHAR(4) NOT NULL,

    ORGANIZATION CHAR(3) NOT NULL,

    YEAR SMALLINT NOT NULL,

    PERIOD CHAR(2) NOT NULL,

    AMOUNT int NOT NULL

    )

    INSERT @account

    VALUES ('1001', '1001', 'Dr'),

    ('1002', '1002', 'Cr'),

    ('1003', '1003', 'Dr'),

    ('1004', '1004', 'Cr')

    INSERT @FACT

    VALUES ('1001', 'aaa', 2012, '01', 100),

    ('1002', 'aaa', 2012, '01', 50),

    ('1003', 'aaa', 2012, '01', -100),

    ('1004', 'aaa', 2012, '01', 90)

    Thanks.

  • Try this,

    SELECT Acc.Account,Fct.ORGANIZATION ,Fct .Year,Period,

    CASE WHEN Acc .[SIGN]='Cr' THEN SUM(Fct.AMOUNT ) ELSE 0 END AS Cr,

    CASE WHEN Acc.[SIGN]='Dr' THEN SUM(Fct.AMOUNT ) ELSE 0 END AS Dr

    FROM #ACCOUNT Acc

    INNER JOIN #FACT Fct ON Acc.ACCOUNT=Fct.ACCOUNT

    GROUP BY Acc.Account,Fct.ORGANIZATION,Fct.Year,Period,Acc .[SIGN]

  • Not sure why you need the SUM or GROUP BY.

    DECLARE @account TABLE

    (

    ACCOUNT CHAR(4) NOT NULL,

    [DESC] CHAR(4) NOT NULL,

    [SIGN] CHAR(4) NOT NULL

    );

    DECLARE @FACT TABLE

    (

    ACCOUNT CHAR(4) NOT NULL,

    ORGANIZATION CHAR(3) NOT NULL,

    [YEAR] SMALLINT NOT NULL,

    PERIOD CHAR(2) NOT NULL,

    AMOUNT int NOT NULL

    );

    INSERT @account

    VALUES ('1001', '1001', 'Dr'),

    ('1002', '1002', 'Cr'),

    ('1003', '1003', 'Dr'),

    ('1004', '1004', 'Cr');

    INSERT @FACT

    VALUES ('1001', 'aaa', 2012, '01', 100),

    ('1002', 'aaa', 2012, '01', 50),

    ('1003', 'aaa', 2012, '01', -100),

    ('1004', 'aaa', 2012, '01', 90);

    select

    f.ACCOUNT,

    f.ORGANIZATION,

    f.[YEAR],

    f.PERIOD,

    case when [SIGN] = 'Dr' then cast(AMOUNT AS VARCHAR) else '' end Dr,

    case when [SIGN] = 'Cr' then cast(AMOUNT AS VARCHAR) else '' end Cr

    from

    @account a

    inner join @FACT f

    on (a.ACCOUNT = f.ACCOUNT);

  • Lynn Pettis (6/14/2012)


    Not sure why you need the SUM or GROUP BY.

    I was going to say the same thing but I think Sony was assuming there might be duplicated rows (different transaction amounts) for account/org/year/month.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • dwain.c (6/15/2012)


    Lynn Pettis (6/14/2012)


    Not sure why you need the SUM or GROUP BY.

    I was going to say the same thing but I think Sony was assuming there might be duplicated rows (different transaction amounts) for account/org/year/month.

    That's okay. Now I am wondering if this was also a homework problem.

  • Lynn Pettis (6/15/2012) Now I am wondering if this was also a homework problem.

    SELECT SolutionFor

    FROM MyHomework

    WHERE Solutionby = 'Lynn' OR Solutionby = 'Sony'

    πŸ™‚


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St

  • Thanks to all the feedback. It is actually a work problem, not a homework problem πŸ™‚

  • The Sql Server Gurus are really fast here at SSC. πŸ˜€

    By the time I read the new thread, it was all done and dusted. πŸ˜›

    Nice work Lynn and Dwain.

    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] πŸ˜‰

  • yingchai (6/15/2012)


    Thanks to all the feedback. It is actually a work problem, not a homework problem πŸ™‚

    It has few of the characteristics of a homework problem. Folks who are too idle to do their homework and post here hoping for a free ride are also, usually, too idle to give a full description of the problem or provide DDL and DML.

    You did both πŸ˜‰

    β€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 9 posts - 1 through 8 (of 8 total)

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