How do I sort a data range but only return one column? (Sheets) - sorting

I have a data range in Google Sheets where I want to sort the data by column B, but only return column A. If it matters, column A is a string, column B is integers.
Using =SORT(A1:B10,2,FALSE) returns both columns A and B, sorted by column B...but I only want it to return column A.
I've also tried:
=QUERY((SORT(A1:B10,2,FALSE)),"select *") <- does exactly the same as sort, tried just for testing
=QUERY((SORT(A1:B10,2,FALSE)),"select col1") <- #value error
=QUERY((SORT(A1:B10,2,FALSE)),"select A") <- #value error (also tried "select A:A" and "select A1:A10")
=QUERY((SORT(A1:B10,2,FALSE)),"select Stat") <- #value error
I've also tried all of the above, but starting with =QUERY(A1:B10,SORT(...
Am I using QUERY wrong? Is SORT not what I want? I could just use SORT in a hidden part of the sheet, then reference the column I want but that feels cheaty, I want to know if there's a way to do what I want to do.

You can set in the first part the column you want to be returned, then the column you want to be sorted with, and then if it's ascending or not (you can then add other columns, obviously. They don't need to be included nor contiguous, but of the same size). Try this:
=SORT(A1:A10,B1:B10,FALSE)

use:
=INDEX(SORT(A1:A10,2,),,1)

Related

INDEX(FILTER(SORT not solving - Need an alternative?

Purpose: I'm trying to fetch the value from column c in the first sheet, where column a matches to column a in current sheet. If there are more than one matches in column a, fetch the most recent entry, according to the date in column b. If the most recent cell in column c was left blank, go back and fetch from the most recent matching row in which column c contains a value.
See the sample sheet, it's pretty clear.
I tried: =INDEX(FILTER(SORT('VISIT LOG'!C2:C,'VISIT LOG'!B2:B,FALSE),'VISIT LOG'!A2:A=A2,'VISIT LOG'!C2:C<>""),1,1)
But it's not working.
Any help will be appreciated!
you can try:
=INDEX(IF(LEN(A2:A),IFNA(VLOOKUP(A2:A,SORT(LAMBDA(z,FILTER(z,INDEX(z,,3)<>""))('Visit log'!A2:C),2,0),3,)),))
You can try with QUERY and MAP:
=MAP(A2:A,C2:C,LAMBDA(ax,cx,IF(ax="","",QUERY('Visit log'!A2:C,"Select C where C is not null AND A = '"&ax&"' order by B desc limit 1"))))

Formula to sort by column that contains times and text, place text at the end, in Google Sheets

My Google Sheets Select statement selects rows from a master sheet of results and then sorts them by time (least amount of time first) ascending.
Some of these results will be entered as DNS and I want them to appear at the end of the time but they appear at the top.
Here is my statement:
Select A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,V,W where not D contains '/' and O is not Null order by P Asc
Column P contains time in HH:mm:ss formatted as duration. If one of the riders is a DNS, they appear at the top of the sort. Unless I sort descending - which is not desired.
With DNS, you probably mean "did not start". The query() function will only accept one data type in a column, and because most of the values in column P are time values, the "DNS" values will return as null. query() sorts null values first.
Try this to sort your data the way you describe:
=sort( { Data!A2:R, Data!Q2:R, Data!V2:W }, Data!P2:P, true )
Then use filter() or query() to remove rows where column D contains a /.

Simpler alternative to simultaneously Sort and Filter by column in Google Spreadsheets

I have a spreadsheet (here's a copy) with the following (headered) columns:
A: Indices for a list of groceries;
B: Names for the groceries to be indexed by column A;
C: Check column with "x" for inactive items in column B, empty otherwise;
D: Sorting indices that I want to apply to column B;
Currently, I am getting the sorted AND filtered result with this formula:
=SORT(FILTER(B2:B; C2:C = ""); FILTER(D2:D; C2:C = ""); TRUE)
The problem is that I need to apply the filter two times: one for the items and one for the indices, otherwise I get a mismatch between elements for the Sort function.
I feel that this doesn't scale well since it creates duplication.
Is there a way to get the same results with a simpler formula or another arrangement of columns?
=SORT(FILTER({Itens!B2:B\Itens!G2:G}; Itens!D2:D=""))
=SORT(FILTER({Itens!B2:B\Itens!G2:G}; Itens!D2:D="");2;1)
or maybe: =SORT(FILTER(Itens!B2:B; Itens!D2:D="");2;1)

How to filter clickhouse table by array column contents?

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;

Using XPath to find rows where a specific column has value

I'm having trouble using XPath to find a row in a table where a specific column contains a value. The table has 10 columns where 2 of them will show Yes|No but I'm only interested in finding the value in one of the columns (the 4th one). My initial attempt was this:
//table[#id='myTable']/tbody/tr/td[text() = 'Yes']
but it finds it rows from both columns. I thought I could try something like this but it's not a valid expression:
//table[#id='myTable']/tbody/tr/td[4]/text()='Yes'
Any suggestions? Thanks.
You can try this way :
//table[#id='myTable']/tbody/tr[td[4][. = 'Yes']]
The XPath return row (tr) having the forth td child value equals "Yes".

Resources