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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:55:19+00:00 2026-06-11T02:55:19+00:00

I stored string variables in a database, either ‘Abstain’, ‘Block’, ‘No’, or ‘Yes’. Currently

  • 0

I stored string variables in a database, either ‘Abstain’, ‘Block’, ‘No’, or ‘Yes’. Currently I am grabbing these using an ORDER BY position however, instead of alphabetic order, I would like to order them like: ‘Block’, ‘No’, ‘Abstain’, ‘Yes’. Is there an easy way to do this in Rails?

My exact code for this is:

def self.unique_votes(motion)
  Vote.find_by_sql("SELECT * FROM votes a WHERE created_at = (SELECT MAX(created_at) as created_at FROM votes b WHERE a.user_id = b.user_id AND motion_id = #{motion.id}) ORDER BY a.position")
end

NB: I am using Postgres SQL and am looking for a solution that i can use in the select statement Thanks!

  • 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-11T02:55:20+00:00Added an answer on June 11, 2026 at 2:55 am

    This is one of the ways to do that. By the way, the best way to do such a thing is to have another table named "Position" that identifies every value by a number and a foreign key in the "votes" table to reference the primary key of "Position" table. Also you can have another column named "Order" in the "Position" table to store those numbers and sort with. You should do these things in the insert time, not on every select.

    Cheers

    SELECT  *
    ,   Case    a.position
            When    'Block'     Then    0
            When    'No'        Then    1
            When    'Abstain'   Then    2
            When    'Yes'       Then    3
            Else    -1
        End                     As  Position_Value
        FROM    votes   a
        WHERE   created_at =    (
            SELECT  MAX(created_at) as  created_at
                FROM    votes   b
                WHERE   a.user_id   =   b.user_id
                AND motion_id   =   #{motion.id}
        )
        ORDER   BY  Position_Value
    

    Any way if it does not work as it should! you can try this one:

    Select  *
        From    (
            SELECT  *
        ,   Case    a.position
                When    'Block'     Then    0
                When    'No'        Then    1
                When    'Abstain'   Then    2
                When    'Yes'       Then    3
                Else    -1
            End                     As  Position_Value
            FROM    votes   a
            WHERE   created_at =    (
                SELECT  MAX(created_at) as  created_at
                    FROM    votes   b
                    WHERE   a.user_id   =   b.user_id
                    AND motion_id   =   #{motion.id}
            )
        )   As  Result
        ORDER   BY  Position_Value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have string stored in python variables, and i am outputting a html that
I'm trying to compare a string stored in a structure. I'm using scanf to
I have an email body stored as a string in a database, something like
i am using a stored procedure to run some queries in my database. The
I'm using the following code to display the values stored in Android sqlite database.
I am copying a stored procedure from one database to another. I am currently
I am trying to create a database with the name of a string stored
I am using a T-SQL block to get the dump of ascii characters stored
Xyz's Goods Hospital @ St. Petersburg Division is stored in a string variable named
I have this string stored in a variable: IN=bla@some.com;john@home.com Now I would like to

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.