convert multi row result set to one row - ruby-on-rails

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

Related

How to change the value of a cell only when everyone has left the house?

I have a sheet consisting of entries that look like this, which I use to keep track of when the members of my family are at home:
DateTime
User
In/Out
5/29 13:00
Mike
In
5/29 13:05
Fred
Out
The rows are added via automation from everyone's phone using IFTTT. I have some flexibility in the format, but not a lot.
I would like to create a cell that changes ONLY when everyone is out of the house. Another IFTTT rule will watch that cell, and when it changes it will start the roomba. So the cell should NOT change if anyone returns home, it should only change if everyone has left.
One way I can think to do this is to set the watched cell to the last timestamp when everyone has left the house. That way, it will only update to a new value when everyone has once again left. Anytime there's a new row, if the status of everyone is Out, it will update the last timestamp.
I'm having a little trouble composing the formulas to keep track of when everyone is Out. This involves looking back through the most recent entries and finding the last time everyone who isn't the current user was out. I figure I can use a filter and a reverse sort and a lookup for every row in the table, but this seems a little complicated and inefficient.
Is there a better way to accomplish what I want?
you could track it like:
=SORTN(SORT(B2:C, ROW(B2:B), 0), 9^9, 2, 1, 1)
and then:
=SUMPRODUCT(INDEX(SORTN(SORT(FILTER({B2:C,
ROW(B2:B)}, B2:B<>""), 3, 0), 9^9, 2, 1, 1)="Out",, 2))=
COUNTUNIQUE(B2:B)
where:
TRUE = everybody out
FALSE = someone in
to get a time when house is empty:
=IF(SUMPRODUCT(INDEX(SORTN(SORT(FILTER({B2:C,
ROW(B2:B)}, B2:B<>""), 3, 0), 9^9, 2, 1, 1)="Out",, 2))=COUNTUNIQUE(B2:B),
INDEX(SORTN(SORT(A2:C, ROW(B2:B), 0), 9^9, 2, 2, 1), 1, 1), "someone home")
I think I got it, many thanks to player0.
The columns in yellow are written by the IFTTT automation. Columns D and E are set to the status of Mike and Carrie, as of that time. D3 is set to:
=INDEX(TRANSPOSE(SORTN(SORT(A$2:C3, ROW(B$2:B3), 0), 9^9, 2, 2, 1)),3)
and filled down for the rest of column D, which also populates E.
Once you have a row with a timestamp and everyone's status, it's a relatively simple thing to pick out the latest row where everyone is out. Cell G4 is set to:
=INDEX(SORT(FILTER(A2:E,D2:D="OUT",E2:E="OUT"),1,FALSE),1,1)
In this example, it shows that the last time everyone was out was on 6/5, which is correct. As more rows are added, the value does not change again until the next time everyone is out, which is important for the automation that watches that cell for changes to know when to start the vacuum.
I am definitely open to more elegant solutions that don't need to drag a formula down column D, but for now this one seems to do the job.

Google Sheets Bottom Up Vlookup Over Multiple Tabs

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)))

Google sheets, how to create a table of Vlookup and get the last occurrence for each customer

I have a database of customers, where his zone, seller and some other values can change, and I want to generate a table of the last occurrence of each one
Heres a demo sheet
using Vlookup doesn't get the last occurrence
My database is very big and I need something to not slow my sheet that much, because I was using a formula like this one for each cell
=ARRAYFORMULA(LOOKUP(2,1/(C2:C=A2),$D$2:$D))
but the sheet is very slow because of this
Any help on this please ?
use:
=ARRAY_CONSTRAIN(SORTN(SORT({C3:E, B3:B}, 4, 0), 9^9, 2, 1, 1), 9^9, 3)

Google Sheets: Listing and counting unique values from multiple cells

I'm looking to list and count unique values from multiple cells. The practical application is to list and count the scenes in a movie that a particular character appears in.
I'm using the following array formula to list the scenes from the data table:
=ArrayFormula(TEXTJOIN(", ",TRUE,IF($B$11:$B$64=E13,$A$11:$A$64,"")))
It will returns something like this (these are the scene numbers):
2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4
But I want it to return:
2,3,4
Then to count the unique values I used the following formula:
COUNTUNIQUE(SPLIT(F13,", ",0))
But the problem here is that it returns "1" even when the array formula correctly returns no value (i.e. the character didn't appear in any scene)
Here is the Google Sheet so you can see things in context:
https://docs.google.com/spreadsheets/d/1dwrORFJ508duRP1no7258dqLemujkOjpvA3XmolqtsU/edit?usp=sharing
Any help will be greatly appreciated!
F11:
=ARRAYFORMULA(TEXTJOIN(",",1,UNIQUE(IF(E11=B$11:B,A$11:A,))))
=COUNT(SPLIT(F11,","))
Use UNIQUE() to find unique values before joining them
SPLIT parameter 1 can't be empty, which gives a #VALUE error,Which is counted as 1 with COUNTUNIQUE.Use IFERROR to mask it.(Since we already have unique values, COUNT is simpler)

Ranking Google Sheet Info Uniquely

I am trying to rank the data in one column in my google sheet so that there are no duplicate rankings. I've seen some solutions such as =RANK(A2,$A$2:$A$10)+COUNTIF($A$2:A2,A2)-1, but the problem is that it increments the duplicates based on occurrence in the sheet.
Let's say my data that I'd like ranked is as follows:
1
1
1
2
The rank order would be 2, 3, 4, 1. The problem is, if I change the second entry to 2 (so that my data is now 1, 2, 1, 2) the ranking order becomes 3, 1, 4, 2 instead of 3, 2, 4, 1 like I want. In the original data, the fourth entry was initially the highest and I'd like it to still have the higher rank, but since the formula counts occurrences it gets demoted. Any way to accomplish this?
No, not with native spreadsheet functions. Spreadsheet formulae have no "awareness" of which values were entered most recently.
You would need to resort to Google Apps Script run on an "on edit" trigger.

Resources