using "like" with cell to reference the last four digits in google sheets - google-sheets-query

hardCode formula
dynamic formula
I am using google sheets to make a report related to customers projects,
in my company When we have new project we give the project a 4 digits number then we create account receivable for the customer contain of 14 digits like that :
the first 6 digits is fixed
the second 4 digits represent the year and month
the last 4 digits represent the project number
so i have column with the Project number and i want to make a query that return the count of costumer payment in front of the project number , when i try to use this formula :
=QUERY({'2016DB'!$A$2:$H$18168;'2018DB'!$A$2:$H$13379;'2017DB'!$A$2:$H$17759;'2019DB'!$A$2:$H},"select
count(Col3) where Col1 = 12040118091492 And Col3 > 0 label count(Col3)
'' ",0)
it's work fine
But when i try to make it dynamic by using like statement :
QUERY({'2016DB'!$A$2:$H$18168;'2018DB'!$A$2:$H$13379;'2017DB'!$A$2:$H$17759;'2019DB'!$A$2:$H},"select
count(Col3) where Col1 like 120401____"""&E7&""" And Col3 > 0 label
count(Col3) '' ",0)
Where E7 Cell Contain the Project number it doesn't work !!

Made a simple example with your type of query. Check this, please: https://docs.google.com/spreadsheets/d/1ISG0AjMNhwnXQvlxDJCMHcyx8mDKiCv6zqOfAGxrrDg/edit?usp=sharing (screenshot: https://i.imgur.com/jHppqzr.png)
Main formula is: =QUERY(A1:A5, "select A where A like 'a__"&D1&"'") where D1 is your needed project number. Looked up values are in format: letter a followed by 2 any characters and then project number.
Mind double and single quotes.

Related

google sheet script filter and query based on checkbox and date

I have 2 sheets.
1 sheet with 3 columns: 1 column with dates, 1 column with data and one with checkboxes
The other sheet I want to apply the query on
If a checkbox is checked that copy a row with specific column based on the date of 2 cells on the target sheet. (one cell contains month (januari, second cell contains year 2023)
The query part works:
=QUERY(Data!A1:D, "select B,A,C where D=TRUE")
But then howto integrate the date filter. I need some guidance, sometimes I cant get my head around it.
=QUERY(filter(Data!A1:D100, "select B,A,C,D where D=TRUE",TEXT(Data!A1:A100,"yyyMMMM") = $D$1 & $B$1))
Latest code I came up with, but does not work.
Suggestions are welcome
Use filter() with two criteria, like this:
=filter(
{ Data!B1:B, Data!A1:A, Data!C1:C },
Data!D1:D,
text(Data!A1:A, "MMMyyyy") = left(B1, 3) & D1
)
...where column Data!A1:A contains numeric date values, cell B1 contains a month name as a text string and D1 contains a year as a number.

SumIIF Access Query

I am struggling to get the desired results i need using an Access query, and was wondering if what i was looking to do was actually achievable in one query, or whether i would need two queries and then export to Excel and interrogate the results there.
I've a table with a number of columns, i am specifically looking at three columns
Row Type - this will either be populated with A or U
Account Number - there will be potentially multiple instances of account number within the table. Although only once against row type "A", and multiple on row type "U"
Value - a currency field. At Account number level, the sum of "U" row, should equal the "A" value
I am looking to produce a query that will list three things.
[Account Number]
Sum of [Value] when [RowType] = "U"
[Value] when [RowType] = "A"
Would i need to create a new column in my table to generate a value for the requirement "Sum of Value when 'U')
I've tried
SUM(IIF([ROWTYPE]='U',[Value],0)) - but that doesn't seem to work.
I've also tried to use the builder within the Query to replicate the same, but again that also doesn't seem to work.
If all else fails i'm content to have to run two queries in Access and then join Excel, but i tihnk for my own learning and knowledge it would be good to know if what i am trying to do is possible.
I was hoping this is possible to compile in Access, but my knowledge of the application is seriously lacking, and despite looking on the MS Access support pages, and also some of the response on the StackOverflow forums, i still can't get my head around what i need to do.
Example of the data
Row Type
Account ID
Value
A
123456789
50.00
U
123456789
30.00
U
123456789
20.00
A
987654321
100.00
U
987654321
80.00
U
987654321
20.00
The data has been loaded into Access, table called "TEST"
This is the SQL i've got, but doesn't give me the desired results.
SELECT [TEST].[ROW TYPE], SUM([TEST].[VALUE]) AS [TEST].[ACCOUNT ID]
FROM [TEST]
GROUP BY [TEST].[ROW TYPE], [TEST].[ACCOUNTID]
When the query generates, would hope to see two rows, one for each account number.
Three row -
Account Number
Sum Value (where row is U)
Value (Where row is A)
I currently get 4 rows in the query. Two results for each account number, one result is the Value when Row Type = A, the other when Row Type = U.
I guess this is what you are after:
SELECT
[Account ID],
Sum(IIf([Row Type]="A",[Value],0)) AS A,
Sum(IIf([Row Type]="U",[Value],0)) AS U
FROM
TEST
GROUP BY
[Account ID];
Output:
Account ID
A
U
123456789
50,00
50,00
987654321
100,00
100,00

How to find entries matching a specific string

I have more than 50000 records in my table with two columns (id and basic) looks like this
ID
BASIC
1
XXX111XXX111
2
XXXX22221111
3
111XXXXX2212
4
2X1X212X1X1X
5
X21X12X1X12X
What I need is to display only records that consist of 5 "X" that are not adjacent. for example, from the above records i need to get data like this
ID
BASIC
4
2X1X212X1X1X
5
X21X12X1X12X
What query will suite to retrieve such record from my database.
I interpret this as meaning that you want 5 Xs that are not adjacent. I think this does what you want:
where concat(' ', col, ' ') regexp '([^X]+[X]){5}[^X]'
The concat() just takes care of the situation where the first or last character is an "X".

Using column names in IMPORTRANGE QUERY

I have used a script that allows me to substitute column names for Col# format in QUERY of a sheet in the same Google workbook but when I call the QUERY from a different Google Sheets spreadsheet with IMPORTRANGE, I get this error:
Range not found (line 11, file "SQLfunction")
My calling cell looks like this:
=QUERY(
IMPORTRANGE("long-hairy-key","MASTER!A:BN"),
IMPORTRANGE("long-hairy-key",SQL("MASTER!A:BN",STATS!D33,true)),
1
)
STATS!D33 is this:
select TRACT,PARID,LINE,OWN1,T_STATUS,ExhibitDate,FEET2,NewEase,TWSacres,ExistEase,PrevDistTWS,ATWSacres,ARdistance,StagingArea,Damages,UpdateReason,ACQ_STATUS where Col51 <>'' ORDER BY LINE ASC, TRACT ASC Label OWN1 'Landowner'
and the SQL function is from here.
Can someone recommend how I might adjust the script to accommodate IMPORTRANGE?
The referred SQL() custom function is not able to access an external spreadsheet. You have to adapt the SQL() custom function to make it able to access an external spreadsheet or to redesign your formula.
If you go for redesigning the formula, one approach is to move the import of the external data out of the "main formula". To do this,
First, get the external range. Add a new sheet (assume that it's name is Sheet1) and add the following formula to A1 =IMPORTRANGE("long-hairy-key","MASTER!A:BN")
Second, replace the "main formula" by other like the next one:
=QUERY(
Shee1!A:BN,
SQL("Sheet1!A:BN",STATS!D33,true)),
1
)
You may generate sql statement with formulas.
Step1. Use importrange to get headers only.
Paste this formula in Sheet1 A1:
=transpose(IMPORTRANGE("long-hairy-key","MASTER!A1:BN1"))
Need to transpose the data to make vertical list of field names.
Step2. Add column numbers.
Paste this formula in B1:
=ARRAYFORMULA(ROW(OFFSET(A1,,,counta(A:A))))
In this step you will get table, which looks like this:
A B
1 FieldName1 1
2 FieldName2 2
3 FieldName3 3
4 FieldName4 4
5 FieldName5 5
Step3. Combine the SQL statement.
Use column C to indicate, which columns are to be selected in your query:
A B C
1 FieldName1 1 1
2 FieldName2 2
3 FieldName3 3 1
4 FieldName4 4
5 FieldName5 5 1
And use this formula in free cell (F1):
="select Col"&JOIN(", Col",FILTER(B:B,C:C=1))
In my case, this formula gives string select Col1, Col3, Col5, and this can be used in query formula:
=query(IMPORTRANGE("long-hairy-key","MASTER!A:BN"), F1)
Use additional columns to make your SQL smarter, add where clause, order by, label and so on.
Update
So this step-by-step solution doesn't solve the problem with rearranged columns.
The trick is to use column names list and save it as values:
paste the formula from step1: =transpose(IMPORTRANGE("long-hairy-key","MASTER!A1:BN1"))
copy it → go to paste spacial → values
Then you need to go through steps 1 and 2 in order to make the list of current firld numbers.
And then you'll have actually two tables. The first one will hold actual field list and field numbers, generated by formulas from step1 and step2. Here's Table1:
A B
1 FieldName1 1
2 FieldName2 2
3 FieldName3 3
4 FieldName4 4
5 FieldName5 5
And the second table will have SQL settings. Here's Table2::
A B C
1 FieldName1 1 1
2 FieldName3 3
3 FieldName2 2 1
4 FieldName4 4
5 FieldName5 5 1
Field names in it must be entered as values. Column B will contain vlookup function:
= vlookup(A1, table1!A:B, 2, 0)
or even ArrayFormula:
= arrayformula(vlookup(offset(A1,,,counta(A1:A)), table1!A:B, 2, 0))
That's all you need to start. Then just make your query smarter.

Is there a possibility to change the order of a string with numeric value

I have some strings in my database. Some of them have numeric values (but in string format of course). I am displaying those values ordered ascending.
So we know, for string values, 10 is greater than 2 for example, which is normal. I am asking if there is any solution to display 10 after 2, without changing the code or the database structure, only the data.
If for example I have to display values from 1 to 10, I will have:
1
10
2
3
4
5
6
7
8
9
What I would like to have is
1
2
3
4
5
6
7
8
9
10
Is there a possibility to ad an "invisible character or string which will be interpreted as greater than 9". If i put a10 instead of 10, the a10 will be at the end but is there any invisible or less visible character for that.
So, I repeat, I am not looking for a programming or database structure solution, but for a simple workaround.
You could try to cast the value as an number to then order by it:
select col
from yourtable
order by cast(col AS UNSIGNED)
See SQL Fiddle with demo
You could try appending the correct number of zeroes to the front of the data:
01
02
03
..
10
11
..
99
Since you have a mixture of numbers and letters in this column - even if not in a single row - what you're really trying to do is a Natural Sort. This is not something MySQL can do natively. There are some work arounds, however. The best I've come across are:
Sort by length then value.
SELECT
mixedColumn
FROM
tableName
ORDER BY
LENGTH(mixedColumn), mixedColumn;
For more examples see: http://www.copterlabs.com/blog/natural-sorting-in-mysql/
Use a secondary column to use as a sort key that would contain some sort of normalized data (i.e. only numbers or only letters).
CREATE TABLE tableName (mixedColumn varchar, sortColumn int);
INSERT INTO tableName VALUES ('1',1), ('2',2), ('10',3),
('a',4),('a1',5),('a2',6),('b1',7);
SELECT
mixedColumn
FROM
tableName
ORDER BY
sortColumn;
This could get difficult to maintain unless you can figure out a good way to handle the ordering.
Of course if you were able to go outside of the database you'd be able to use natural sort functions from various programming languages.