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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:25:56+00:00 2026-05-30T22:25:56+00:00

Imagine the scenario, I have a table Day_fee(date(Date),fee(Integer)) I want to have a list

  • 0

Imagine the scenario,

I have a table Day_fee(date(Date),fee(Integer))

I want to have a list with only the months of the table, for example:

Day_fee
date(Date)  fee(Int)
----------|---------
2012/03/27   1
2012/03/26   2
2012/03/25   2
2012/05/01   3
2012/05/02   3
2012/07/01   2
2013/04/01   2
2013/05/01   1

The result => a list with 03, 05 and 07 , without repeat the months.

And the other big question:

I would want a hash o list with month and year, for example {“03″=>”2012″,”04″=>”2013″,”05″=>”2013”, “05”=>”2012″, “07”=>”2012″} , without repeat the months, maybe the table can have more years with the same month

Beacuse, after that, i want to show a html table with the months and paginate that.

Thoughts?

Thanks for your help.

  • 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-30T22:25:57+00:00Added an answer on May 30, 2026 at 10:25 pm

    Try this:

    If you are using Postgres DB

    # Postgres
    DayFee.sum(:fee, :group => "EXTRACT(MONTH FROM date)")
    
    => #<OrderedHash {"3" => 5, "5"=> 6, "7"=> 2}
    

    If you are using MySQL

    DayFee.sum(:fee, :group => "MONTH(date)")
    

    Edit

    Based on your comment, it looks like you want a different data:

    Postgres

    DayFee.count(:fee, :group => "date_trunc('month', date)").
      map{|d, k| Date.parse(d)}.
      inject({}){|h, d| h[d.month]=d.year;h}
    

    MySQL

    DayFee.count(:fee, :group => "DATE_FORMAT(date, '%Y-%m')").
      map{|d, k| Date.parse(d+"-00 00:00")}.
      inject({}){|h, d| h[d.month]=d.year;h}
    

    To get a 2D array

    DayFee.count(:fee, :group => "date_trunc('month', date)", 
        :order => "date_trunc('month', date) ASC").
      map{|d, k| Date.parse(d)}.
      map{|h, d| [d.month, d.year]}
    
    # [["03", "2012"], ["03", "2013"], ["03", "2014"]]
    

    To get hash with collection of years

    DayFee.count(:fee, :group => "date_trunc('month', date)").
      map{|d, k| Date.parse(d)}.
      inject({}){|h, d| (h[d.month]||=[]) << d.year;h}
    
    # {"03" => ["2012", "2013", "2014"]}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have following table: NAME DATE OTHER_CONTANT 'A' '2012-06-05' 'baz' 'A' '2012-06-04' 'bar'
Imagine the following scenario: I have a list of Labels stored in a table
So imagine this scenario, I have a list and it has a bit of
Imagine case scenario, you have a list of recipes that have ingredients as a
Imagine the scenario you have a listing page that is a concatenation of multiple
Imagine following scenario: We have a lot of parallel development going on in several
Imagine this scenario: You have a desktop and a laptop. The desktop has a
I'm looking for example of how I would solve the scenario below: Imagine my
Scenario Imagine a REST service that returns a list of things (e.g. notifications) Usage
Imagine a scenario where you have a complex framework of WinRT code that you

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.