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 8022297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:13:40+00:00 2026-06-04T22:13:40+00:00

i have a single table called journal. Journal has 4 columns: Date (date) Account(int)

  • 0

i have a single table called journal.
Journal has 4 columns:

  1. Date (date)
  2. Account(int)
  3. Type(varchar(1) (Can accept chars D or C)
  4. Amount(decimal(18,2))

JOURNAL

Date         Account    Amount    Type
2012-05-31   20001      300       D
2012-05-31   20002      700       C
2012-05-31   20003      600       D
2012-05-31   20004      900       C

The type column can only take 2 types of character values: D or C.
So i need a query that will give me 4 columns, such that the resulting columns are:

Date        Account       D       C
2012-05-31  200101        300     0
2012-05-31  200102        0       700
2012-05-31  200103        600     0
2012-05-31  200104        0       900

with the D and C columns filled with values from Amount, whether they are null or not.

  • 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-06-04T22:13:43+00:00Added an answer on June 4, 2026 at 10:13 pm

    You can accomplish this with a PIVOT:

    create table journal
    (
        date datetime,
        account int,
        amount money,
        type varchar(1)
    )
    
    insert into journal values ('05/31/12', 20001, 300, 'D')
    insert into journal values ('05/31/12', 20002, 700, 'C')
    insert into journal values ('05/31/12', 20003, 600, 'D')
    insert into journal values ('05/31/12', 20004, 900, 'C')
    
    select date
        , account
        , isnull([D], 0) as D
        , isnull([C], 0) as C
    from 
    (
        select *
        from journal
    ) x
    pivot
    (
        sum(amount)
        for type in ([D], [C])
    ) p
    
    drop table journal
    

    See a SQL Fiddle with a Demo

    If you want to join the account names, then you will want to perform a JOIN on that table. See update SQL Fiddle with a demo:

    select date
        , account
        , name
        , isnull([D], 0) as D
        , isnull([C], 0) as C
    from 
    (
        select *
        from journal j
        inner join account a
          on j.account = a.id
    ) x
    pivot
    (
        sum(amount)
        for type in ([D], [C])
    ) p
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a single table called orders. It has 3 fields I care about:
i have 2 tables 1st table called services has id_service, name, date. description 2nd
I have a single table called Indexes, it contains one nvarchar and three ntext
I have table called page which represents every single page in my website. page_id
I have a dataset (called dataSet below) with a single table and some records
I have a database with a table called 'Roles', with 'Id' and 'RoleName' columns.
We're designing a database that has a simple table called Person. Each person can
Say I have a table called xml that stores XML files in a single
I have a table called People with a columns of datatype xml called properties
I have Table-A with a column called serial_number , and Table-B has the same

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.