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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:39:28+00:00 2026-05-28T01:39:28+00:00

I have a table called ORDEREXECUTIONS that stores all orders that have been executed.

  • 0

I have a table called ORDEREXECUTIONS that stores all orders that have been executed. It’s a multi currency application hence the table has two columns CURRENCY1_ID and CURRENCY2_ID.

To get a list of all orders for a specific currency pair (e.g. EUR/USD) I need to lines to get the totals:

v = Orderexecution.where("is_master=1 and currency1_id=? and currency2_id=? and created_at>=?",c1,c2,Time.now()-24.hours).sum("quantity").to_d
v+= Orderexecution.where("is_master=1 and currency1_id=? and currency2_id=? and created_at>=?",c2,c1,Time.now()-24.hours).sum("unitprice*quantity").to_d

Note that my SUM() formula is different depending on the the sequence of the currencies.
e.g. If I want the total ordered quantities of the currency pair USD it then executes (assuming currency ID for USD is 1 and EUR is 2.

v = Orderexecution.where("is_master=1 and currency1_id=? and currency2_id=? and created_at>=?",1,2,Time.now()-24.hours).sum("quantity").to_d
v+= Orderexecution.where("is_master=1 and currency1_id=? and currency2_id=? and created_at>=?",2,1,Time.now()-24.hours).sum("unitprice*quantity").to_d

How do I write this in RoR so that it triggers only one single SQL statement to MySQL?

  • 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-28T01:39:28+00:00Added an answer on May 28, 2026 at 1:39 am

    I guess this would do:

    v = Orderexecution.where("is_master=1 
                               and ( (currency1_id, currency2_id) = (?,?)
                                  or (currency1_id, currency2_id) = (?,?)
                                   )
                               and created_at>=?"
                            ,c1, c2, c2, c1, Time.now()-24.hours
                            )
                      .sum("CASE WHEN currency1_id=? 
                                   THEN quantity 
                                 ELSE unitprice*quantity 
                            END"
                          ,c1 
                          )
                      .to_d
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table called Users that has a column called deleted, a boolean
I have table called 'shipped_data' https://i.stack.imgur.com/JXBej.png and need go get all 'caseNumbers' that match
I have a table called Point. The table has two columns that we'll deal
I have a table called USERS that has a foreign key to the table
I have a table called Comments that stores messages between two users, and the
I have a table called ApprovalTasks... Approvals has a status column I also have
I have a table called BlogPost which has a 1-to-many relationship with the Comment
I have a table called Orderrow. Orderrow has a a compound primary key (CPK)
I have a table called ProjectRegion. It has two columns, an id and a
I have a Table called Address. I have a Trigger for insert on that

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.