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

  • Home
  • SEARCH
  • 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 9205703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:00:49+00:00 2026-06-18T00:00:49+00:00

I have a table that is designed like this: tbl_User id Name tbl_User_Payment id

  • 0

I have a table that is designed like this:

tbl_User
id
Name

tbl_User_Payment
id
user_id
amount
status

tbl_User
id      Name
001     John Doe
002     Juan Dela Cruz


tbl_User_Payment
id        user_id      amount        status
001         001          10         Successful
002         002          10         Fail
003         001          10         Fail
004         001          10         Fail
005         001          10         Fail
006         002          10         Successful
007         002          10         Fail
008         002          10         Fail

The problem here is that how can I count the number of Failed Payment after a Successful Payment.
So the result here should be something like this:

John Doe - 3 Failed Payment
Juan Dela Cruz - 2 Failed Payment

I hope you understand what i’m trying to accomplish here.

Your help will be greatly appreciated and rewarded!

Thanks! 🙂

  • 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-18T00:00:50+00:00Added an answer on June 18, 2026 at 12:00 am

    For brevity and clarity, I’m going to alias a few things in my code.

    SELECT
        Users.UserName,
        FailCount
    FROM
        (
        SELECT
            UserId,
            COUNT(*) As FailCount
        FROM
            Payments,
            (
            SELECT
                UserId,
                MAX( PaymentId ) As LastSuccessId
            FROM
                Payments
            WHERE
                Status = Successful
            GROUP BY
                UserId
            ) AS LastSuccess
        WHERE
            PaymentId > LastSuccessId
        GROUP BY
            UserId
        ) As FailCount
        INNER JOIN Users ON Users.UserId = FailCount.UserId
    ORDER BY
        UserName DESC
    

    I’m a bit rusty, I haven’t tested this SQL but if it doesn’t work I suspect it’s because of my “FROM Payments, <subquery>” syntax. I’m not in a position where I can verify my solution, so you might have to recruit someone to help you if it doesn’t work.

    If you’re wondering, here’s my logic:

    1. My code works in the assumption that PaymentIds are incremented sequentially chronologically.
    2. Do a query that gets the ID of the last successful payment, per user (the innermost SELECT).
    3. Re-query the Payments table to get the COUNT of rows, per user, that come after the “last successful payment ID” values that were retrieved by the innermost SELECT.
    4. JOIN the results from step 3 with the users table to get the usernames.

    Also, a few tips for future database design: there is no need to use Hungarian Notation in DB design – I cringe every time I see “tbl_” prefixes. I also advise against naming primary-key fields simply “id” as it makes JOINs more difficult as you need to disambiguate them. Consider prefixing them with the name of the table (e.g. “payments.id” becomes “Payments.PaymentId“).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have an JPA entity like this: @Entity @Table(name = category) public class Category
I have a table which has one TEXT column that looks like this: 0-4
I have a table that stores data that has been entered regarding the amount
I have a table storing millions of rows. It looks something like this: Table_Docs
I have a table relationship that looks like the following: barn ------ PK barn_id
I am looking for a database that supports indexes like this: select name from
I have table that I insert data with following query (from c# code): INSERT

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.