Data available in one column should display some values in one column and some values in 2nd colums

  • Data available in one column should display some values in one column and some values in 2nd colums

    Ex:

    Account Amount

    Dr          5000

    Cr          6000

    Dr           4000

    should display like this

    Acct  Dramt Cramt

    Dr     5000    null

    Cr      null     6000

    Dr      4000    null

     

     

  • SELECT

    Account Acct,

    Amount Dramt,

    NULL Cramt

    FROM [TableName]

    WHERE Account = 'Dr'

    UNION ALL

    SELECT

    Account Acct,

    NULL Dramt,

    Amount Cramt

    FROM [TableName]

    WHERE Account = 'Cr'

    K. Brian Kelley
    @kbriankelley

  • Select Account,

    Case when Account = 'Cr' Then Amount Dramt ,

    Case When Account = 'Dr' Then Amount Cramt

    from [TableName ]

  • Yuvraj Powar's approach is nice an simple (but probably left out a couple of keywords to save time):

    Select Account,

    Case when Account = 'Cr' Then Amount End AS Dramt ,

    Case When Account = 'Dr' Then Amount End AS Cramt

    from [TableName ]


    Best Regards,

    Barry Cummings

  • Thank you for giving reply,

    but at the same time can we display sum of those values at the bottom.

    like:

    Acct  Dramt Cramt

    Dr     5000    null

    Cr      null     6000

    Dr      4000    null

            9000    6000

     

  • Select Account,

    Case when Account = 'Cr' Then Amount End   Dramt ,

    Case When Account = 'Dr' Then Amount End Cramt

    from [TableName ]

    UNION

    Select NULL Account,

    SUM(Case when Account = 'Cr' Then Amount End ) Dramt ,

    SUM(Case When Account = 'Dr' Then Amount End  ) Cramt

    from [TableName ]

     

     

Viewing 6 posts - 1 through 5 (of 5 total)

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