Query Function Order by problem Google Sheets - google-sheets

I already have the first part of the query ready, But I would like to order it with the newest date first. Can you please help!
=QUERY(Sheet3!A:I, "
SELECT D, -1 * SUM(I)
GROUP BY D
LABEL -1 * SUM(I) 'Balance Diario'
FORMAT D 'yyyy-MMM-dd',
-1 * SUM(I) '$#,##0.00'
")

Is this were you looking for?
=QUERY(Sheet3!A:I, "
SELECT D, -1 * SUM(I)
GROUP BY D
ORDER BY D DESC
LABEL -1 * SUM(I) 'Balance Diario'
FORMAT D 'yyyy-MMM-dd',
-1 * SUM(I) '$#,##0.00'
")
Put more ORDER BY D ASCorDESC
DESC is to reverse the order

Related

Google Sheets Query to combine data from 3 sheets with different criteria

I need to combine 3 sets of data vertically, using a Query. The first 2 data sources ('SOURCE 1'!A:T and EG:EZ) have the same conditions and I was able to combine them with no problem, but the 3rd data source ('SOURCE 3'!A:T) has different conditions and when I add this source I result in an error. The column headers will be identical in the output.
This is the formula containing ONLY the first 2 data sources, that works:
=QUERY({'SOURCE 1'!A:T;EG:EZ},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1)
When adding in the 3rd data source, below are the 3 formulas I've tried, but all result in the error:
In ARRAY_LITERAL, an Array Literal was missing values for one or more rows.
Attempt 1:
={QUERY({'SOURCE 1'!A:T;EG:EZ},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1);
QUERY('SOURCE 3'!A:T,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col1 is not null")}
Attempt 2:
={QUERY('SOURCE 1'!A:T,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1);
QUERY(EG:EZ,"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'");
QUERY('SOURCE 3'!A:T,"Select Col1,Col2,Col3,Col4,Col5,Col16,' ',Col8,Col9,Col10,Col11,Col12,Col13 where Col1 is not null")}
Attempt 3:
={QUERY('SOURCE 1'!A:T,"Select A,B,C,D,E,F,G,H,I,J,K,L,M where J >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and J <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and T = 'YES' and A starts with 'APP-' order by B,J,A,I label K 'NAME'",1);
QUERY(EG:EZ,"Select A,B,C,D,E,F,G,H,I,J,K,L,M where J >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and J <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and T = 'YES' and A starts with 'APP-' order by B,J,A,I");
QUERY('SOURCE 3'!A:T,"Select A,B,C,D,E,P,' ',H,I,J,K,L,M where A is not null")}
Any advice would be much appreciated. Thank you!
If you write QUERY(EG:EZ,"Select A,B,C,D,E,F,G,H,I,J,K,L,M you can't select A... within EG:EZ!
I suggest to change 'SOURCE 1'!A:T by {'SOURCE 1'!A:T} , idem for EG:EZ and then you will be able to use only Col1, Col2 etc. in your formula.
Try
={QUERY({'SOURCE 1'!A:T},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'",1);
QUERY({EG:EZ},"Select Col1,Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 where Col10 >= date '"&TEXT(B1,"yyyy-mm-dd")&"' and Col10 <= date '"&TEXT(D1,"yyyy-mm-dd")&"' and Col20 = 'YES' and Col1 starts with 'APP-' order by Col2,Col10,Col1,Col9 label Col11 'NAME'");
QUERY({'SOURCE 3'!A:T},"Select Col1,Col2,Col3,Col4,Col5,Col16,' ',Col8,Col9,Col10,Col11,Col12,Col13 where Col1 is not null")}
if there is a lack of data in one of the three queries, you will also get an error, in that case, add iferror(query______,{"","","",...} on each query

Why is this Query statement not displaying "text" formatted phone numbers" on Google Sheets?

This formula is returning eveything right,but the phone numbers, which are preceded by an apostrophe. If I happen to delete the apostrophe, it displays the numbers, but since there may be a 0 at the beginning, it has to come as text.
Here's the formula:
=iferror(QUERY('Form Responses'!$A$4:$P; "select N, B, C, D, E, F, G, H, I, J, K, L, A where N >= 1 and todate(A)=date '"&TEXT(B3;"yyyy-mm-dd")&"' order by N Asc");"Pick a date")
Here's how the data looks:
...and this is the result right now:
Here's the link to an example:
https://docs.google.com/spreadsheets/d/1vxCwfcXRrpuvmkhpRsFrKQSq5aMjkdqlUYY-6_UknuE/edit?usp=sharing
Appreciate your time/help - as usual!
try:
=INDEX(SUBSTITUTE(IFERROR(QUERY(
{'Form Responses'!A4:P\ TEXT('Form Responses'!E4:E; "\×0")};
"select Col14,Col2,Col3,Col4,Col17,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col1
where Col14 >= 1
and todate(Col1) = date '"&TEXT(B3; "yyyy-mm-dd")&"'
order by Col14 asc"); "Pick a date"); "×"; ))
update:
try:
=ARRAYFORMULA(TEXT(SUBSTITUTE(IFERROR(QUERY(
{'Form Responses'!A4:P\ TEXT('Form Responses'!E4:E; "\×0")};
"select Col14,Col2,Col3,Col4,Col17,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col1
where Col14 >= 1
and todate(Col1) = date '"&TEXT(B3; "yyyy-mm-dd")&"'
order by Col14 asc"); "Pick a date"); "×"; );
{"#"\"#"\"#"\"#"\"\0#"\"#"\"#"\"#"\"#"\"#"\"#"\"#"\"mm/dd/yyyy"}))

QUERY returns 0

I have a main data sheet (Main) with several columns, the relevant ones being column A, a date (displayed as dd.mm.YYYY) and column D, a numeric value. In a second sheet, I'm using the query function to build the sum over all these numeric values for a specific month using the following expression:
=SUM(QUERY(Main!$A$1:$D$2364; "SELECT D WHERE MONTH(A) = 1"; 1))
MONTH(A) returns a number between 0 and 11 and the above expression works perfectly well for January (0) to October (9). However, as soon as I try to compare MONTH(A)
with a number > 9, the sum shows zero despite there being numerous entries in both November and December.
What I get is this:
=SUM(QUERY(...; "SELECT D WHERE MONTH(A) = 10"; 1)) equals =SUM(QUERY(...; "SELECT D WHERE MONTH(A) = 1"; 1))
and
=SUM(QUERY(...; "SELECT D WHERE MONTH(A) = '10'"; 1)) equals 0
Can anyone help me out with this one?
try:
=SUM(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)))
or:
=SUMPRODUCT(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)))
or:
=ARRAYFORMULA(SUM(IFERROR(QUERY(Main!A2:D; "SELECT D WHERE MONTH(A) = 10"; 0)*1)))

Joins On Subqueries

I'm still kinda new to SQL; please spare me. I have an issue with trying to join a table or another view to my existing query below.
The last question I had asked refers to joining the two subqueries and giving them an alias in order for the join to work (something I still don't understand?) I'm trying to join a table and another subquery to the query below. But I'm not quite sure how I could continue adding subqueries to it.
I don't really understand how the alias names allowed the subqueries to be joined, and if I add another subquery, then I'll add another alias?
Any assistance you could provide would be much appreciated. Apologize in advanced for the formatting :/
Select
Sub4.DT as Date, Sub2.USOH, Sub2.USOHP, Sub2.OVXH, Sub2.OVXHP, Sub4.USOPP, Sub4.OVXPP
From
(Select
*
From
(Select
x.ID, x.Date, x.USOH, x.OVXH, convert(varchar, x.Date, 1) AS DT,
Abs(Cast((((x.USOH / NullIf((y.USOH), 0)) - 1) * 100) AS Decimal(10, 2))) AS USOHP2,
Format(Abs(((x.USOH / NullIf((y.USOH), 0))) - 1), 'P') AS USOHP,
Abs(Cast((((x.OVXH / NullIf((y.OVXH), 0)) - 1) * 100) AS Decimal(10, 2))) AS OVXHP2,
Format(Abs(((x.OVXH / NullIf((y.OVXH),0)))-1),'P') AS OVXHP
From
(Select
a.Date as aDate, Max(b.Date) As aPrevDate
From
MACDHistogram A
Inner Join
MACDHistogram b on a.Date > b.Date
Group By a.Date) Sub1 -- Group Date > Previous Date SUB1
Inner Join
MACDHistogram x on Sub1.aDate = x.Date
Inner Join
MACDHistogram y on Sub1.aPrevDate = y.Date) T2) Sub2 --Histogram Percent SubQuery SUB2
Inner Join
(Select
*
From
(Select
z.ID, z.ID2, z.Date, z.USO as USOP, z.OVX as OVXP,
convert(varchar, z.Date, 1) as DT,
Cast(((z.USO / NullIf((q.USO),0)- 1) * 100) as Decimal(10,2)) AS USOPP2,
Format(((z.USO / NullIf((q.USO),0))-1),'P') AS USOPP,
Cast(((z.OVX / NullIf((q.OVX),0)- 1) * 100) as Decimal(10,2)) AS OVXPP2,
Format(((z.OVX / NullIf((q.OVX),0))-1),'P') AS OVXPP
From
(Select
c.Date as cDate, Max(d.Date) As cPrevDate
From
Prices C
Inner Join
Prices d on c.Date > d.Date Group By c.Date) Sub3 -- Group Date > Previous Date SUB3
Inner Join
Prices z on Sub3.cDate = z.Date
Inner Join
Prices q on Sub3.cPrevDate = q.Date) T4) Sub4 -- Price Percent Subquery SUB4
On Sub2.Date = Sub4.Date
Order By
sub4.Date Desc

Get all Rows from QUERY() into one cell in a Google Spreadsheet

I have the following formula in a spreadsheet:
=IFERROR( JOIN( CHAR(10), QUERY(Schedule!$A$2:$E, "SELECT C, D, E Where A = '" & B$2 & "' AND B = timeofday '" & text($A4, "HH:MM:SS") & "' AND C = '" & $C$1 & "'", -1)),)
It works great when it returns one row. It does not work if it returns multiple rows. How can I get it to use all rows returned with a CHAR(10) included between rows as well as columns. Basically I want all the returned data separated by line breaks. So when the QUERY returns
Item1 Item2 Item3
Item1A Item2A Item3A
it is changed to
Item1
Item2
Item3
Item1A
Item2A
Item3A
My goal is to put all the returned data in 1 cell with a line break between each item. Any way I can make this happen?
Here is a copy of the file. Cells B3 to F4 are good sample cells where the formula reside.
The solution is imperfect, it adds extra spaces before each line:
Item1 Item2 Item3
Item1A Item2A Item3A
^ spaces
Step1
Get origilal formula + extra column in query:
=QUERY(data, "SELECT C, D, E, '##' Where" & ... & "' label '##' ''", -1)
The result:
Item1 Item2 Item3 ##
Item1A Item2A Item3A ##
Step2
Replace "##" with char(10)
=ARRAYFORMULA(SUBSTITUTE(step1Formula,"##",char(10)))
Item1 Item2 Item3
Item1A Item2A Item3A
^ new lines
Step3
Use concatenate:
=ArrayFormula(" "&TRIM(CONCATENATE(" "&B16:E17)))
Final formula
=ArrayFormula(" "&TRIM(CONCATENATE(" "&SUBSTITUTE(QUERY(Schedule!$A$2:$E, "SELECT C, D, E, '##' Where A = '" & B$2 & "' AND B = timeofday '" & text($A3, "HH:MM:SS") & IF(OR($C$1 = "*",ISBLANK($C$1)), "", "' AND C = '" & $C$1) & IF(OR($F$1 = "*",ISBLANK($F$1)), "", "' AND E = '" & $F$1) & "' label '##' ''", -1),"##",char(10)))))
Shared file:
https://docs.google.com/spreadsheets/d/1otcqvkXb5H3WTSi_exKw9UUcRqN8MKBZ5JYCmTLAWdQ/edit?usp=sharing
I know this is not an elegant solution, but it will help you in your case. Enter this formula at B3 and then copy and paste it onto other cells.
=iferror(join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 0")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 1")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 2")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 3")),"")
&iferror(char(10)&char(10) & join(char(10), query(Schedule!$A$2:$E, "select C,D,E where A='"&B$2&"' and B=timeofday '"&text($A3, "hh:mm:ss")&"' limit 1 offset 4")),"")
Basically you cannot concat result strings with google query(), so instead I'd fetch one row at a time using limit 1 offset 0, limit 1 offset 1, limit 1 offset 2, and so on. Maximum is 5 in this case. Sample sheet is here.

Resources