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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:09:40+00:00 2026-05-21T16:09:40+00:00

Give the following (already simplified) query in SQLite: def self.calculate(year, month, user_id, partner_id) where(‘:user_id

  • 0

Give the following (already simplified) query in SQLite:

def self.calculate(year, month, user_id, partner_id)
  where(':user_id = entries.user_id OR :partner_id = entries.user_id', {    
      :user_id => user_id,
      :partner_id => partner_id
  }).
  where('entries.date <= :last_day', { 
      :last_day => Date.new(year, month, 1).at_end_of_month
  }).
  select('sum(case when joint = "f" and user_id = :user_id then amount_calc else 0 end) as sum_single' , {    
      :user_id => user_id 
  }).
  group("strftime('%Y-%m', date)")
end

The full query has more sums with different case when statements and some of them depend on whether it is user_id oder partner_id. Unfortunately, Rails complains as select does not take the second parameter with the substitutions like where does. Is there any way to achieve what I want without running two queries, one for user_id and one for partner_id?

  • 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-21T16:09:41+00:00Added an answer on May 21, 2026 at 4:09 pm

    One can be so blind….instead of:

    select('sum(case when joint = "f" and user_id = :user_id then amount_calc else 0 end) as sum_single' , {    
      :user_id => user_id 
    }).
    

    just build the string:

    select('sum(case when joint = "f" and user_id = ' + user_id.to_s + ' then amount_calc else 0 end) as sum_single').
    

    As nobody answered, this is for the archives 🙂

    Edit: Sorry, beware of that: as noted below, this is vulnerable.

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

Sidebar

Related Questions

I need an sql query to give me the following: All the values in
While Compiling the WPF Application for many times in a day it give following
Give the following XML I'm trying to update the UpgradeImage and TargetImage SourceFile attributes
Why does the following give no compilation error?: // T.h template<class T> class X
Why does the following code give me an error (g++ 4.1.2)? template<class A> class
Why does the following code give me a segmentation fault? #define MAXROWS 10 #define
Why does the following not give an error? for (int i=0; i<10; ++i) //
The ANSI C grammar from -link- give me the following rules for array declarations:
I need a regex that will give me the following results from each example
I have a problem with BlackBerry JDE 6.0 The following code give me the

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.