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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:24:18+00:00 2026-06-03T09:24:18+00:00

I have a database table with thousands of rows. I would like to run

  • 0

I have a database table with thousands of rows. I would like to run a report that basically lists all clients who have spent more than £2,000 with us.

The table has a lot of columns, but the ones I am interested are as follows:

======================================================
|  ClientKey | PVID | ShortText           | Payments |
======================================================
|  13276     | 76668| Paid by PET HEALTH  | 56.79    |
|  12893     | 62368| Paid by PET HEALTH  | 42.79    |
|  23782     | 87468| Paid by PET HEALTH  | 26.29    |
|  23246     | 79868| Paid by PET HEALTH  |-16.52    |
======================================================

I need to group the rows by ClientKey and sum up the Payments columns. Then I need to filter them by removing anything where the Payments column sum is less than £2,000. I also need to ignore any rows where the PVID is 44444.

So far I know I will need to have something similar to the following:

@clinical = Clinical.where("ShortText LIKE 'Paid by PET HEALTH' AND PVID != 44444")

That takes care of filtering the rows down to exclude anything with a PVID of 44444 and only showing rows where the ShortText contains Paid by PET HEALTH.

I can’t get my head around how to sum up the payments column for each of the ClientKey’s.

The end result I would like is the ability to do this:

Client Key 13276 has spent £99,999 so far.
Client Key 21342 has spent £3,200 so far.
Client Key 32312 has spent £2,110 so far.

Any help and/or pointers would be appreciated!

  • 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-03T09:24:19+00:00Added an answer on June 3, 2026 at 9:24 am

    You can try:

    Clinical.select("ClientKey as client_key, sum(Payments) as total_payments").group("ClientKey").where("ShortText LIKE 'Paid by PET HEALTH' AND PVID != 44444")
    

    For more information about the grouping, refer rails guide – ActiveRecord Query Interface

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

Sidebar

Related Questions

that my problem: I have database table like that: id (AI) market_id 1 6
i have database table like this +-------+--------------+----------+ | id | ip | date |
I have a database table with a field that I need to read from
I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
Suppose I have this database table (some sample code below) that stores the relationship
I have a database table transaction which stores all the transaction made by users
I have a database table with hundreds of thousands of forum posts, and I
I have hundreds of thousands of rows with four date columns that were imported
I have one database table which contains 8 columns. One of the columns is
I have a database table using an enum. This is already working with hibernate

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.