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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:08:25+00:00 2026-05-27T08:08:25+00:00

I’ve done all of this in active record, but it’s not going to work

  • 0

I’ve done all of this in active record, but it’s not going to work with pagination so I’d like to switch to a SQL query with OFFSET so I can query efficiently. – Rather than getting User.all, then working out the calculations on related objects and then compiling it all into a bundled array and finally sending it up to the view I’d like to handle the calcs in a find_by_sql command so it’s easier to manage pagination etc.

Trying to work out a users total amounts invested + their residual uninvented amounts in my little stock market simulator I’m playing with.

Have a share prices table that has multiple entries for each new share price for each company, so want to:

  • a) select the last entry from that table to get the latest share
    price
  • b) ownerships shows what users from users table own what
    company shares
  • c) So if we multiply shares_owned from the ownerships
    table with the latest share price from a) then we get the total
    amount invested
  • d) Once we have the total amount invested across all
    companies, we need to add on what uninvested dollars the user has
    associated with them so total invested + u.dollars should give us the
    total valuation for a given user.
  • e) what screws me up is that a user might not own anything at a particular time, which means he will have no entries in the ownership table. In that case the query needs to only return his uninvested u.dollars amount.

Trying to get that total valuation per customer and order by ‘richest’ user:

select u.id,
sum( 
    ((
  select s.price from shareprices s
  WHERE s.company_id = o.company_id and u.id = o.user_id
  ORDER BY id DESC LIMIT 1) * o.shares_owned
    ))
    + u.dollars as totalAmount
from users as u full outer join ownerships as o on u.id = o.user_id group by u.id order by totalamount ASC

That returns fine for where there are ownerships and the calculations for total invested work out, but for users who only have uninvested dollars how can i get them to show in that summed column, so essentially its 0 (ie. no owned investment amounts because they own shares in no companies) + u.dollars to get how much they have, but I don’t understand how to make SQL do that.

I am hoping to avoid needing a pgsql function() to achieve this logic but if the above didn’t make it obvious, I’m terrible at SQL and only learning it now. I hope my explanation is even understandable!

  • 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-05-27T08:08:25+00:00Added an answer on May 27, 2026 at 8:08 am

    You can add a colaesce around the part of the calculation that needs to treat nulls as zeros (its not clear to me which part needs to )

    sum(COALESCE 
        ((
      select s.price from shareprices s
      WHERE s.company_id = o.company_id and u.id = o.user_id
      ORDER BY id DESC LIMIT 1) * o.shares_owned
        )) , 0))
        + u.dollars totalAmount
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.