Count with condition

Posted on 2009-04-20
Last Modified: 2012-05-06

I have a table below:


1. TrsNo.
2. TrsDate.
3. TrsAmount.
4. Paid                 (Bit)

I want to count Paid Column if Bit = 0 or Bit = 1


TrsDate  Total Amount      Paid    Not Paid

Note :
1. Total amount = sum(TrsAmount) for TrsDate Grouping.
2. Paid = count(Paid) for Paid Value =1
3. Paid = count(Paid) for Paid Value =0

How to do it?

Thank you.
Question by:emi_sastra
    LVL 25

    Accepted Solution

    Hi emi_sastra,

    try something like:

    SELECT trsdate, SUM(trsamount) as totalamount, SUM(DECODE WHEN Paid=1 THEN 1 ELSE 0 END) as paid, SUM(DECODE WHEN Paid=0 THEN 1 ELSE 0 END) as notpaid
    FROM yourtable
    GROUP BY trsdate

    LVL 1

    Author Comment

    Hi lwadwell,

    Where is the DECODE came from?

    Thank you.
    LVL 25

    Expert Comment

    sorry ... I mean CASE

    (I was just working on something in Oracle which supports the DECODE)
    LVL 1

    Author Comment


    Thank you very much for your help.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
    This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now