Link Sheets and Query with timestamps - google-sheets

I'm trying to link a custom query, but since i'm new in this, i'm getting stuck when Sheets tries to read timestamps
I'm getting the following error:
Something's wrong. Please try again later: Error while parsing the query: Syntax error: Table name contains '-' character. It needs to be quoted: 'xxxxxxxxxx' [at 3:10]
SELECT COUNT(DISTINCT sequence) as aaaaaaaa,bbbbbbb,
EXTRACT (date from CreationDateBR) as data,
FROM xxxxxxxxxxxx
WHERE CreationDateBR BETWEEN '2021-01-01' AND '2022-01-01'
AND loja IN ('Marketplace C')
AND parceiros NOT IN ('partner A')
GROUP BY parceiros,data
ORDER BY data,parceiros asc

The error message states that the issue is in the table name. You can try to put backticks around it (`):
FROM `xxxxxxxxxxxx`
A good trick to ensure the proper formatting is to go to your table in the BigQuery UI and click on "query this table": the proper formatting will open in a new query editor.

Related

Using a subquery as a parameter

I am using Google Sheets and have a connected query where I am using parameters. When one of the parameters is configured to be a subquery, the query will run, but no results are returned.
For example, here is my (simplified) query:
SELECT *
FROM table
WHERE campaign IN (#CAMPAIGN);
In this example, I have the #CAMPAIGN parameter in the Google Sheet configured as:
SELECT DISTINCT campaign FROM table2
If I manually substitute the parameter in the BQ console, it runs fine and returns the expected results. Is there a reason this functionality does not work with parameter substitution in the Google Sheet? Is there a way around this?
Depending on how much SQL SELECT type lookups you do, it may help to use a #customfunction that I wrote. You need to place my SQL .js in your Google sheets project and the =gsSQL() custom function will be available.
The one requirement for this versus using =QUERY() is that unique column titles are required for each column.
It is available on github:
gsSQL github project
This example works if each sheet is a table, so it would be entered something like
=gsSQL("SELECT books.id, books.title, books.author_id
FROM books
WHERE books.author_id IN (SELECT id from authors)
ORDER BY books.title")
In this example, I have a sheet named 'books' and another sheet named 'authors'.
If you need to specify a named range or an A1 notation range as a table, this can also be done with a little more work...
=gsSQL("SELECT books.id, books.title, books.author_id
FROM books
WHERE books.author_id IN (SELECT id from authors)
ORDER BY books.title", {{'books', 'books!$A$1:$I', 60};
{'authors', 'authors!$A$1:$J30', 60}}, true)
In this example, the books and authors come from specific ranges, the data will be cached for 60 seconds and column titles are output.

Crystal Report not showing data

tblTransaction doesn't contain any data, so the fields from tblAccount and tblAcctDetails won't display. How can I get records to display even if there are no matching records in tblTransaction?
Right click the link between tblTransaction and tblAccount to open Link Options. From here, you can change the Join Type to a Right Outer Join. (Returns all rows from the right table, even if there are no matches in the left table.)
Alternatively you can replace your tables with a SQL command and perform any links or unions manually.
You go through the UNION ALL Query for fetching data and try it.
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions]
UNION
SELECT expression1, expression2, ... expression_n
FROM tables
[WHERE conditions];

Join with <= in the On clause in Google bigquery

I want to execute a query with join in Google bigquery that has '<=' instead of '=' in it's on clause:
select s.count_value as count_value,s.total as total,sum(p.total) as accumulated from stats s join stats p on p.rn <=s.rn group by count_value,total,s.rn
When I run this query, I receive an error message saying:
Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name.
Any idea how I can implement this query?
You should enable Standard SQL to do such JOINs
See Enabling Standard SQL
in CLI - just add the --use_legacy_sql=false flag to your command line statement.

Where clause on tag keys in influxdb does not work

I see some strange behavior in 0.9.6.1. The issue is when i query a field without a where clause, it works but when i add "WHERE" in the statement for a tag key, it gives me empty results.
for ex,
select successful, merchant_id from session_metrics_new limit 5
name: session_metrics_new
time successful merchant_id
1453975732000000000 1 bms
1453975733000000000 1 snp
1453975735000000000 1 bms
1453975735000000000 1 snp
1453975739000000000 1 bms
but this does not work
select successful, merchant_id from session_metrics_new where merchant_id =~ /bms/ limit 5
Here, successful is a field key while merchant_id is a tag key. I do not know if this a bug or the way i have stored data. Please help
You're using the regex syntax.
I tried a query on my DB with the same syntax that you used, and I got a result set without a problem. The only problem I see there is if successful is also a TAG rather than a FIELD. But in that case you should get the following exception:
Server returned error: statement must have at least one field in select clause
Are you executing this query through the InfluxDb admin interface or through a 3rd party library for say Java, C#, NodeJs or something like that?
Try a simple where clause instead if you think you'll know the full value of merchant_id field all the time, it's slightly different (it doesn't do pattern matching, but it matches the whole value from the field), that should work and it should even be faster:
select successful, merchant_id from session_metrics_new where merchant_id = 'bms' limit 5

Grails select specific column and use it in where clause

I'm doing a simple query using some math functions (the actual query is more complicated, but even this simple example demonstrates the error):
SELECT 123 as distance,
c
FROM MyDomainClass as c
And this will return [distance,domain class] pairs. That works fine. However, I want to filter and sort by distance as well.
SELECT 123 as distance,
c
FROM MyDomainClass as c
WHERE distance < 10
Unfortunately, it cannot find the 'distance' column in the result set:
Unknown column 'distance' in 'where clause'
I turned SQL logging and see the following generated query:
select 123 as col_0_0_, ............. where distance<?
So I can see the error, but I don't understand why it's using a generated column name 'col_0_0_' in the result set or how I can use this generated name in the where clause.
Any ideas?

Resources