Let's assume I have data like this and I'd like to fond the max value on a column an repeat for all the others within the same group:
What coul i put on Result as formula to obtain the desired result? Is there a way to obtain this result without ordering the data first?
use:
=INDEX(VLOOKUP(A2:A&B2:B, SORT({A2:A&B2:B, C2:C}, 2, ), 2, ))
Related
I'm trying to do something similar to this question: Google sheets using Filter and Sort together
I have an input range with two columns, and as output I want to create a dynamically sorted and filtered list from the input range, using a single formula.
See this document for the desired result: https://docs.google.com/spreadsheets/d/109xcbORFZxTjH0Vjd6PVqYlOxMIdK7aXqf5-jnMMPik/edit?usp=sharing
I tried the formula: =SORT(FILTER(B11:C100, B11:B100 = or(I11,I12,I13,I14)), 2, 0) but it doesn't work. What I am doing wrong here? Any help much appreciated.
try:
=ARRAYFORMULA(QUERY(B11:C,
"where lower(B) matches '"&TEXTJOIN("|", 1, LOWER(I11:I))&"'
order by C desc", 0))
You can modified or(,,,,) like follow:
=SORT(
FILTER(B11:C100,
((B11:B100 = I11)*1+(B11:B100 = I12)*1+(B11:B100 = I13)*1+(B11:B100 = I14)*1)>0
)
, 2, 0
)
I am currently trying to use google sheets to sort a table based on the unique values found in only two of the columns.
What I want to happen is that columns A (Brand) and B (Dimensions) are both checked for unique values, removing any duplicate data.
The problem is using these two columns to filter and show the rest of table. I can't managed to achieve it.
Original Data
What is should look like after being culled
.
You can use Query function:
=query(A1:D8,"select A, B, min(C), min(D) group by A, B",1)
Example spreadsheet
try:
=ARRAYFORMULA(SORT(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(SORTN(
IF(A2:C<>"", {"♦"&A2:A&"♦"&B2:B, "♦"&C2:D}, ), 99^99, 2, 1, 1))
,,99^99)), "♦"), 2, 1))
I have set of data where I want to apply filters by default to a numberDisplay. The data is something like this.
data = [{category:'A',value:10},
{category:'B',value:10},
{category:'C',value:10},
{category:'S',value:10},
{category:'C',value:10},
{category:'A',value:10}]
I am trying to create a number display which will show sum of values other than category 'S', I tried using fake groups but they are failing. What would be the best method to achieve this ?
You don’t need a fake group for this, since you’re not trying to change the shape/structure of the aggregation. Ordinary crossfilter reductions cover this purpose.
You can simply do
cf.groupAll().reduceSum(d => d.category === ‘S’ ? 0 : d.value);
This will sum the value of every row included in the current filters, but will substitute zero if the row’s category is S.
I have a clickhouse table that has one Array(UInt16) column. I want to be able to filter results from this table to only get rows where the values in the array column are above a threshold value. I've been trying to achieve this using some of the array functions (arrayFilter and arrayExists) but I'm not familiar enough with the SQL/Clickhouse query syntax to get this working.
I've created the table using:
CREATE TABLE IF NOT EXISTS ArrayTest (
date Date,
sessionSecond UInt16,
distance Array(UInt16)
) Engine = MergeTree(date, (date, sessionSecond), 8192);
Where the distance values will be distances from a certain point at a certain amount of seconds (sessionSecond) after the date. I've added some sample values so the table looks like the following:
Now I want to get all rows which contain distances greater than 7. I found the array operators documentation here and tried the arrayExists function but it's not working how I'd expect. From the documentation, it says that this function "Returns 1 if there is at least one element in 'arr' for which 'func' returns something other than 0. Otherwise, it returns 0". But when I run the query below I get three zeros returned where I should get a 0 and two ones:
SELECT arrayExists(
val -> val > 7,
arrayEnumerate(distance))
FROM ArrayTest;
Eventually I want to perform this select and then join it with the table contents to only return rows that have an exists = 1 but I need this first step to work before that. Am I using the arrayExists wrong? What I found more confusing is that when I change the comparison value to 2 I get all 1s back. Can this kind of filtering be achieved using the array functions?
Thanks
You can use arrayExists in the WHERE clause.
SELECT *
FROM ArrayTest
WHERE arrayExists(x -> x > 7, distance) = 1;
Another way is to use ARRAY JOIN, if you need to know which values is greater than 7:
SELECT d, distance, sessionSecond
FROM ArrayTest
ARRAY JOIN distance as d
WHERE d > 7
I think the reason why you get 3 zeros is that arrayEnumerate enumerates over the array indexes not array values, and since none of your rows have more than 7 elements arrayEnumerates results in 0 for all the rows.
To make this work,
SELECT arrayExists(
val -> distance[val] > 7,
arrayEnumerate(distance))
FROM ArrayTest;
I am using the Ruby Mongoid gem and trying to create a query to retrieve the last 100 documents from a collection. Rather than using Mongoid, I would like to create the query using the underlying driver (Moped). The Moped documentation only mentions how to retrieve the first 100 records:
session[:my_collection].find.limit(100)
How can I retrieve the last 100?
I have found a solution, but you will need to sort collection in descending order. If you have a field id or date you would do:
Method .sort({fieldName: 1 or -1})
The 1 will sort ascending (oldest to newest), -1 will sort descending (newest to oldest). This will reverse entries of your collection.
session[:my_collection].find().sort({id:-1}) or
session[:my_collection].find().sort({date:-1})
If your collection contain field id (_id) that identifier have a date embedded, so you can use
session[:my_collection].find().sort({_id:-1})
In accordance with your example using .limit() the complete query will be:
session[:my_collection].find().sort({id:-1}).limit(100);
Technically that query isn't finding the first 100, that's essentially finding 100 random documents because you haven't specified an order. If you want the first then you'd have to say explicitly sort them:
session[:my_collection].find.sort(:some_field => 1).limit(100)
and to reverse the order to find the last 100 with respect to :some_field:
session[:my_collection].find.sort(:some_field => -1).limit(100)
# -----------------------------------------------^^
Of course you have decide what :some_field is going to be so the "first" and "last" make sense for you.
If you want them sorted by :some_field but want to peel off the last 100 then you could reverse them in Ruby:
session[:my_collection].find
.sort(:some_field => -1)
.limit(100)
.reverse
or you could use use count to find out how many there are then skip to offset into the results:
total = session[:my_collection].find.count
session[:my_collection].find
.sort(:some_field => 1)
.skip(total - 100)
You'd have to check that total >= 100 and adjust the skip argument if it wasn't of course. I suspect that the first solution would be faster but you should benchmark it with your data to see what reality says.