I want to query an hStore column on PG database and return ONLY the values corresponding to a specific key and value.
hstore column contains the information in the below manner
hours: {"tue_1_open"=>"19:30", "tue_1_close"=>"21:45"}
I am able to query based on key and value as follows
Model.where("hours #> hstore(:key,:value )", key: "tue_1_open", value: "19:30")
But I want to query whose value is grater than specific time. I want query something like
Model.where("hours #> hstore(:key,:value )", key: "tue_1_open", value: ">= 19:30")
Any idea how we can achieve this?
You can try to get greater value in integer not time
Model.where("(hours ? 'tue_1_open')::int > 19")
for datetime hours value of 'tue_1_open' should be a Datetime value
Model.where("(hours ? 'tue_1_open')::timestamp > Time.now")
Related
Ruby on Rails distinct query returns array of unique values in specified field.
User.distinct(:name)
returns array of unique values in name field.
e.g. ['John', 'David', 'Steve']
Looking out for an equivalent aggregate query (User.collection.aggregate([])) in MongoDB which would return an array of unique values in specified field.
I have a jsonb column named 'available_quantity'. which will have sample values
{ "100": 50, "1000":10 }
Now, I want to query all keys with values less than 50.
I tried this query,
Bundle.where('available_quantity #> ?', {'100': 5}.to_json)
But this one gives me all the Bundle with available_quantity containing {100: 5}.
How can I do that? Is that even possible?
You can use the ->> operator:
Bundle.where("(available_quantity->>'100')::int < 50")
In a postgres database I have a string field code in orders table. The field contains values like 'COA-38-A', 'EFILLDIRT' and 'HE60LS-A'. There is a form on UI to filter orders based on code and only integer values are allowed in the field.
The query should filter the orders by integer values in the code field i.e if 30 is entered, the query should return orders with code 'COA-38-A' and 'HE60LS-A' because the query contains >=
I tried adding ::integer in the query:
Order.where('code::integer >= ?', params[:code])
but got the following error:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer
Is there any way to filter by only integer values?
try this?
Order.where("regexp_replace(0||code, '[^0-9]+', '', 'g')::integer >= ?", params[:code].to_i)
How can I order query results by an hstore attribute?
#items = Item.includes(:product).order('products.properties #> hstore("platform")')
Causes
PG::Error: ERROR: column "platform" does not exist
LINE 1: ...oduct_id" ORDER BY products.properties #> hstore("platform"...
platform is a hstore key, stored in the properties column, which is an hstore type.
Double quotes are used to quote identifiers (such as table and column names) in PostgreSQL (and other databases that follow the standard). So when you say:
hstore("platform")
PostgreSQL sees "platform" as a quoted column name and since there is no platform column, you get an error.
Strings in standard SQL are quoted with single quotes, you want to say:
.order("products.properties #> hstore('platform')")
This will probably still fail though, hstore('platform') doesn't make much sense and neither does using #> here; a #> b means
does the hstore a contain the hstore b
If you're trying to sort on the value of the 'platform' key in the properties hstore then you'd want to use -> to lookup the 'platform' key like this:
.order("products.properties -> 'platform'")
Hi how can i sort my entries by the timestamp of the columns?
def self.get_users
##db.execute("SELECT * FROM users")
end
Do you mean timestamp of the values, or is your column key some kind of timestamp?
CQL doesn't support sort - it returns data in the order it is stored on disk. You tell Cassandra how you want you columns sorted by specifying the comparator_type on your Column Family. In this case, you might consider using a composite column keys of [timestamp, column name].