Sort query by column - sorting

I am trying to make a query on Google Sheets and sort the results according to the highest values on column C. The range I am doing the query on is $A$6:$O.
I have tried to do it like this:
=SORT(QUERY(($A$6:$O), "Select A, B, C, D,E,F,G,H,I group by C"))
However, I am always getting an error saying
#VALUE: Unable to parse query string for Function QUERY parameter 2: CANNOT_GROUP_WITHOUT_AGG
Do you have any idea about how I can make this work?

Use the ORDER BY clause in place of GROUP BY.

Related

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

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)

CriteriaQuery equivalent for conditional sum query in MySQL?

In MySQL, I can use the following conditional sum query to count the number of rows, grouped by specific conditions:
SELECT SUM(synced=true) AS sum_a, SUM(synced=false) AS sum_b from test_table where name = "xyz";
I am trying to translate this into an equivalent CriteriaQuery, however, am not able to do so. How would the CriteriaQuery equivalent look for this?
So far, the closest I have reached is using a multiselect function with selectCase expressions as follows:
criteriaQuery.multiselect(
criteriaBuilder.sum(
criteriaBuilder.<Number>selectCase().when(/* conditions for sum_a */), 1
).otherwise(0)
).alias("sum_a"),
criteriaBuilder.sum(
criteriaBuilder.<Number>selectCase().when(/* conditions for sum_b */), 1
).otherwise(0)
).alias("sum_b"),
The above approach, however, is different from the SQL query mentioned at the beginning, even though it returns the correct values.
The query you posted looks correct. That's as good as it gets. You can't reproduce the exact same SQL with HQL or the JPA Criteria API. In fact, the syntax of the SQL query is MySQL specific and only works because MySQL doesn't have native boolean support, but models this as integer 1 and 0. The SQL standard way to model this is to use the fact that aggregate functions are null rejecting and use a case when expression that produces null if your condition is not fulfilled.
criteriaQuery.multiselect(
criteriaBuilder.sum(
criteriaBuilder.<Integer>selectCase().when(/* conditions for sum_a */), 1
).otherwise(criteriaBuilder.nullLiteral(Integer.class))
).alias("sum_a"),
criteriaBuilder.sum(
criteriaBuilder.<Integer>selectCase().when(/* conditions for sum_b */), 1
).otherwise(criteriaBuilder.nullLiteral(Integer.class))
).alias("sum_b"),

Propel, selecting just one column

I am trying to run a query in propel that runs an aggregate function (SUM).
My Code
$itemQuery = SomeEntity::Create();
$itemQuery->withColumn('SUM(SomeColumn)', someColumn)
->groupBy(SomeForeignKey);
Problem
It should theoretically return the sum of every group of items but the problem is propel tries to fetch all columns, and also appends a bunch of other columns to the group by clause. This results in an unexpected categorisation and therefore the sum is incorrect.
Is there anyway to make propel fetch just the column I am running the aggregation function on so that the group by statement works as well?
You need to add a select statement for the column and the foreign key:
$itemQuery = SomeEntity::Create();
$itemQuery->select(array(SomeColumn, SomeForeignKey));
$itemQuery->withColumn('SUM(SomeColumn)', someColumn);
$itemQuery->groupBy(SomeForeignKey);

Oracle query with two paterns in one expression

Input:
TABLE NAME: SEARCH_RECORD
Column A Column B Column C Column D
ID CODE WORD CODE/WORD
--------------------------------------------
123 666Ani RAT 666Ani/RAT
124 777Cae CAT 777Cae/CAT
I need a query to check as a LIKE case
if i search with column B like '%6A' or column C '%A%' it will give result
suppose i want to get the like based on the column D search
**User will search like '%6A%'/'%AT%' (always / will be given by user)**
Expected output:
666Ani/RAT
so, I need a query for the above to get the ID as output (CASE query is preferable)
Need you valuable suggestion
.
It can't be done with simple like.
It should work if the pattern look like '%6A%/%AT%'. It is a valid pattern.
So, you can write: columnD like '%6A%/%AT%' or columnD like first_pattern||'/'||second_pattern if the come from as different variables.
Another approach, if you know for sure that there is only a /(you can check how many they are), may be to use two likes using substr to get first and then second part of the search string.
where
columnB like substr(match_string, 1, instr(match_string,'/'))
and
columnC like substr(match_string, instr(match_string,'/')+1)

How do I use the Hive "test in(val1, val2)" built in function?

The Programming Hive book lists a test in built in function in Hive, but it is not obvious how to use it and I've been unable to find examples
Here is the information from Programming Hive:
Return type Signature Description
----------- --------- -----------
BOOLEAN test in(val1, val2, …) Return true if testequals one of the values in the list.
I want to know if it can be used to say whether a value is in a Hive array.
For example if I do the query:
hive > select id, mcn from patients limit 2;
id mcn
68900015 ["7382771"]
68900016 ["8847332","60015163","63605102","63251683"]
I'd like to be able to test whether one of those numbers, say "60015163" is in the mcn list for a given patient.
Not sure how to do it.
I've tried a number of variations, all of which fail to parse. Here are two examples that don't work:
select id, test in (mcn, "60015163") from patients where id = '68900016';
select id, mcn from patients where id = '68900016' and test mcn in('60015163');
The function is not test in bu instead in. In the table 6-5 test is a colum name.
So in order to know whether a value is in a Hive array, you need first to use explode on your array.
Instead of explode the array column, you can create an UDF, as it is explain here http://souravgulati.webs.com/apps/forums/topics/show/8863080-hive-accessing-hive-array-custom-udf-

Resources