I have a query that results in an ActiveRecord::Relation:
> query.class
=> ActiveRecord::Relation
Usually these two expressions yield the same results, but I have a problem where they yield different results:
query.pluck(:id).uniq.sort
query.map(&:id).uniq.sort
For example:
[14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 321, 322, 323, 324, 325]
vs
[310, 311, 312, 313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325]
The query in question is a paging query. I have 116 sample users and the query is:
"SELECT users.* FROM users WHERE users.company_id = 2 LIMIT 25 OFFSET 100"
It's not clear to me why that should ever be the case, or what I should be looking for. Any suggestions as to why they might be different?
The documentation suggests they should be equivalent.
Your query:
SELECT users.* FROM users WHERE users.company_id = 2 LIMIT 25 OFFSET 100
doesn't have an ORDER BY clause so you have no reason to expect any particular 25 rows to come back. Keep in mind that the rows in a table have no natural order so you always need to explicitly specify an order if you need a specific order.
Add an ORDER BY and you should get the same results from both the pluck and map approaches.
Related
Hey I have an issue with trying to do a reverse order lookup or a bottom up vlookup
My formula is
=ARRAYFORMULA(IFERROR(VLOOKUP(A3:A,{'movement co'!C:R;bfitt!C:R;gladstone!C:R;parkhurst!C:R;mayfield!C:R;alexandria!C:R;'crows nest'!C:R;newtown!C:R;'gregory hills'!C:R;annandale!C:R;graceville!C:R;'south penrith'!C:R;'north melbourne'!C:R;'back on track'!C:R;'elsternwick'!C:R;'brighton'!C:R},{16,2,3,4,15,10,5},0)))
This looks over multiple tabs/sheets to gather the data which works fine, but I need it to look at the last added row which I can't seem to make work with a sort function.
Any ideas?
try:
=ARRAYFORMULA(IFERROR(VLOOKUP(A3:A, SORT(
{'movement co'!C:R, ROW('movement co'!C:R);
bfitt!C:R, ROW(bfitt!C:R);
gladstone!C:R, ROW(gladstone!C:R);
parkhurst!C:R, ROW(parkhurst!C:R);
mayfield!C:R, ROW(mayfield!C:R);
alexandria!C:R, ROW(alexandria!C:R);
'crows nest'!C:R, ROW('crows nest'!C:R);
newtown!C:R, ROW(newtown!C:R);
'gregory hills'!C:R, ROW('gregory hills'!C:R);
annandale!C:R, ROW(annandale!C:R);
graceville!C:R, ROW(graceville!C:R);
'south penrith'!C:R, ROW('south penrith'!C:R);
'north melbourne'!C:R, ROW('north melbourne'!C:R);
'back on track'!C:R, ROW('back on track'!C:R);
'elsternwick'!C:R, ROW('elsternwick'!C:R);
'brighton'!C:R, ROW('brighton'!C:R)}, 17, 0),
{16, 2, 3, 4, 15, 10, 5}, 0)))
Well i have problem with my data
This is my healthcare database
(Name, Value1, Value2, Value3, Value4)
Jhon 10, 20, 30, 40
Jhon 9, 12, 21, 33
Noah 8, 22, 18, 10
Anna 9, 19, 29, 32
Clark 11, 4, 17, 20
In healthcare database one person can ill two times, three times or more as you can see the example of my database there is two jhon's who have two records because he ill twice
The purpose i use k-means is to get two cluster (cluster 1 : group 1, cluster 2 : group 2) with their member
And i want get output like this :
Group 1 : jhon, clark
Group 2 : noah, anna, jhon
You see there is two jhon's, one member can be to group 1 and group 2, so how i can fix this problem ??
K-means works by iterations between pairs of steps. You basically alternate between
assuming you know the mapping of instances to clusters, and calculate the cluster centers
assuming you know the cluster centers, assign instances to clusters
Thus if you have constraints, e.g., that all jhon (sic) should belong to the same cluster, you can incorporate this into step 2: you'll need to find the cluster for which the simultaneous assignment of all of them is the most likely.
See Constrained k-means clustering with background for details.
I need to create subscale scores for 4 subscales of the REI: REI_Appear; REI_Hlth; REI_Mood; REI_Enjoy. The items comprising each subscale are as follows:
Appearance (9 items): 1, 5, 9, 13, 16, 17, 19, 21, 24
Health (8 items): 3, 6, 8, 15, 18, 20, 22, 23
Mood (4 items): 2, 7, 12, 14
Enjoyment (3 items): 4, 10, 25
For example, I have placed REI_Appear in the target variable but then im unsure of what to place in the numeric expression section for it to work?
There are several important issues.
Do you want means or sums or some other composite?
Do any items need reversing?
How do you want to handle missing data?
Assuming you want means, there are no items needing reversing, and you want a participant to have at least 3 items to get a score, then you could use:
compute REI_appear = mean.3(item1, item5, ..., item24).
EXECUTE.
where you replace item1 etc. with the relevant variable names.
I have an existing post dedicated to the topic of computing scale scores for psychological tests which discusses some of these issues further.
I have an excel sheet / Yaml file containing few records.
I was wondering if rails provide a way to achieve pagination while reading data from yaml document or excel sheet.
Try Kaminari gem.
You can paginate through any kind of Array-ish object using Kaminari::PaginatableArray
> p Kaminari::PaginatableArray.new([*1..100]).page(3).per(20)
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
Another good pagination gem is will_paginate
It works really well with active record relations, and will also paginate arrays as long as you require the right file first. See https://stackoverflow.com/a/8407304/441979
it's a noob question so hope somebody can help on this
I want to transform the resultset from the query which has a multiple lines for one user to a single row of data for one activity
i get this data
userid, date, activity_id, parameter_id, parameter_value
545, 2011/9/11, 1, 4, 20
545, 2011/9/11, 1, 5, 10,
545, 2011/9/11, 1, 6, 30,
i want it to convert to
userid, date, activity_id, parameter_4_id_value, parameter_5_id_value, parameter_6_id_value
545, 2011/9/11, 1, 20,10,30
any example will be really helpful. Thank you
If you are talking about pulling data from MySQL you can use GROUP_CONCAT to do something like this. See this other answer for an example https://stackoverflow.com/a/276949/511203