Google BigQuery: TABLE_QUERY AND TABLE_DATE_RANGE - google-bigquery
I have Big Query tables like below, and like to issue a query to the tables marked <=.
prefix_AAAAAAA_20170320
prefix_AAAAAAA_20170321
prefix_AAAAAAA_20170322 <=
prefix_AAAAAAA_20170323 <=
prefix_AAAAAAA_20170324 <=
prefix_AAAAAAA_20170325
prefix_BBBBBBB_20170320
prefix_BBBBBBB_20170321
prefix_BBBBBBB_20170322 <=
prefix_BBBBBBB_20170323 <=
prefix_BBBBBBB_20170324 <=
prefix_BBBBBBB_20170325
prefix_CCCCCCC_20170320
prefix_CCCCCCC_20170321
prefix_CCCCCCC_20170322
prefix_CCCCCCC_20170323
prefix_CCCCCCC_20170324
prefix_CCCCCCC_20170325
I made a query as this
SELECT * FROM
(TABLE_QUERY(mydataset,
'table_id CONTAINS "prefix" AND
(table_id CONTAINS "AAAAAA" OR table_id CONTAINS "BBBBBB")' )
AND
TABLE_DATE_RANGE(mydataset.prefix, TIMESTAMP('2017-03-22'), TIMESTAMP('2017-03-24')))
I got this error.
Error: Encountered " "AND" "AND "" at line 5, column 4. Was expecting: ")" ...
Does anybody has ideas?
You cannot mix TABLE_QUERY and TABLE_DATE_RANGE for exactly same FROM!
Try something like below
#legacySQL
SELECT *
FROM (TABLE_QUERY(mydataset, 'REGEXP_MATCH(table_id, "prefix_[AB]{7}_2017032[234]")'))
Consider Migrating to BigQuery Standard SQL
In this case you can Query Multiple Tables Using a Wildcard Table
See How to Migrate from TABLE_QUERY() to _TABLE_SUFFIX
I think, in this case your query can look like
#standardSQL
SELECT *
FROM `mydataset.prefix_*`
WHERE REGEXP_CONTAINS(_TABLE_SUFFIX, '[AB]{7}_2017032[234]')
I can not migrate to Standard SQL because ...
If I would like to search for example between 2017-03-29 and 2017-04-02, do you have any smart SQL
Try below version
#legacySQL
SELECT *
FROM (TABLE_QUERY(mydataset,
'REGEXP_MATCH(table_id, r"prefix_[AB]{7}_(\d){8}") AND
RIGHT(table_id, 8) BETWEEN "20170329" AND "20170402"'))
Of course yo can adjust above to use whatever exactly logic yo need to apply!
Related
Bigquery job failed with error: Encountered " "FROM" "FROM ""
I'm calling a SQL query with a BigQuery API with Airflow. This query works perfectly fine in the BigQuery workspace but says I'm writing FROM FROM even though I'm not... The logs say line 4, character 20 is where the error occurs which corresponds to: , EXTRACT(DATE FROM event_time) AS session_date. My overall query structure looks something like: SELECT * FROM ((SELECT fields_here FROM table_name LEFT JOIN UNNEST(sub_table) AS s WHERE 1=1 UNION ALL (SELECT fields_here FROM table_name LEFT JOIN UNNEST(sub_table) AS s WHERE 1=1 ORDER BY 1, 2)) ORDER BY 1, 2 I'm also using the LEAD() window function and COALESCE() but not sure if that matters. Really confused why this error is occurring...
Issue was not adding use_legacy_sql=False argument in Airflow
Extracting hour by using coalese in SQL on a timestamp
I am trying to update a query to extract the hour from a timestamp and I keep getting an error. The error I get is due to the FROM clause I was using. SELECT analytics_platform_data_type , activity_date_pt , activity_timestamp_pt , analytics_platform_timestamp_utc , analytics_platform_timestamp_utc_iso --This is the clause that is causing the problem (Begin) , extract(hour from coalesce(activity_timestamp_pt)) as latd_hour_pt --Clause above is the issue; Line above is line 9 (End) , analytics_platform_ platform , ad_channel_name , publisher_name , ip_address , analytics_platform_unique_activity_id , click_id , latd_custom_fields FROM table_date_range([AllData_AnalyticsMobileData_], timestamp('2018-09- 25'), timestamp('2018-09-27')) where 1=1 and analytics_platform_data_type = 'CLICK' and partner_name = 'ABC123' If I remove the extract hour piece the query works fine. When I add it I get the error: Encountered " "FROM" "from "" at line 9, column 16. Was expecting: ")" ... I have seen the clause I am trying to use in the above query used before, but it was a much more complex query that was using sub queries. Really not sure what the issue is. (Using Google Big Query Legacy SQL)
Your query is mixing Legacy Syntax (table_date_range) with Standard Syntax (Extract) If for some reason you need to stick with Legacy SQL - use HOUR() instead of EXTRACT() But it is much recommended to migrate stuff to Standard SQL - where you should use wildcard functions instead of table_date_range Something like FROM `project.dataset.AllData_AnalyticsMobileData_*` WHERE _TABLE_SUFFIX BETWEEN '2018-09-25' AND '2018-09-27' see more at https://cloud.google.com/bigquery/docs/reference/standard-sql/migrating-from-legacy-sql#table_decorators_and_wildcard_functions in Migrating to Standard SQL doc
BigQuery - Is it possible to use an IF statement within à FROM?
I would like to do the following FROM if(... = ..., table_date_range(mytable, timestamp('2017-01-01'), timestamp('2017-01-17')), table_date_range(mytable, timestamp('2016-01-01'), timestamp('2016-01-17')) ) Is this kind of operation allowed on BigQuery ?
You can do this using a condition on _TABLE_SUFFIX in standard SQL. For example, SELECT * FROM `my-dataset.mytable` WHERE IF(condition, _TABLE_SUFFIX BETWEEN '20170101' AND '20170117', _TABLE_SUFFIX BETWEEN '20160101' AND '20160117'); One thing to keep in mind is that since the matching table suffixes are probably determined dynamically (based on something in your table) you will be charged for a full table scan.
For BigQuery Legacy SQL (which code in your question looks more like) you can use TABLE_QUERY table wildcard function to achieve this. See example below: SELECT ... FROM TABLE_QUERY([mydataset], "CASE WHEN ... = ... THEN REPLACE(table_id, 'mytable_', '') BETWEEN '20170101' AND '20170117' ELSE REPLACE(table_id, 'mytable_', '') BETWEEN '20160101' AND '20160117' ") or , with IF() SELECT ... FROM TABLE_QUERY([mydataset], "IF(... = ..., REPLACE(table_id, 'mytable_', '') BETWEEN '20170101' AND '20170117', REPLACE(table_id, 'mytable_', '') BETWEEN '20160101' AND '20160117') ") Meantime, when possible, consider migrating to BigQuery Standard SQL
HiveQL : use SELECT in clause WHERE
Is there a way to do this in HiveQL : SELECT ...... from default.thm_renta_produits_jour rpj WHERE rpj.co_societe = '${hiveconf:in_co_societe}' AND rpj.dt_jour >= (SELECT MIN(dt_jour) FROM default.calendrier WHERE co_an_semaine = '${hiveconf:in_co_an_sem}') Because when i do this, i get this error : FAILED: ParseException line 51:26 cannot recognize input near 'SELECT' 'MIN' '(' in expression specification Thanks,
Hive does not support sub queries in where clause it supports sub queries in from clause only.
Hive does not support sub queries in the WHERE clause. Perhaps you can work around this by moving your sub query to a JOIN clause like so: SELECT rpj.* FROM default.thm_renta_produits_jour rpj JOIN ( SELECT MIN(dt_jour) AS min_dt_jour FROM default.calendrier WHERE co_an_semaine = '${hiveconf:in_co_an_sem}' ) m WHERE rpj.co_societe = '${hiveconf:in_co_societe}' AND rpj.dt_jour >= m.min_dt_jour; Hope that helps.
I know that this is an old post, but the previous answers are now outdated. Newer versions of Hive (0.13+) support subqueries of where clauses, so your query should run. https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries#LanguageManualSubQueries-SubqueriesintheWHEREClause
Why is this query not working for number 1?
this query returns 5 results as expected select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '0' but this query returns nothing select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1' the USERFIELD1 is a varchar 80 field Here is all the data that is in the DB and as you can see there are two records with USERFIELD1 = '1' but there seems to be a line break after them...maybe that is causing the issue...is there anyway to grab those two records LEADID,CREATEUSER,CREATEDATE,MODIFYUSER,MODIFYDATE,ACCOUNTMANAGERID,ASSIGNDATE,BUSINESSDESCRIPTION,COMPANY,COMPANY_UC,CREDITRATING,DATAQUALITY,DESCRIPTION,DIVISION,DONOTSOLICIT,EMAIL,EMPLOYEES,FAX,FIRSTNAME,HOMEPHONE,IMPORTID,IMPORTSOURCE,INDUSTRY,INTERESTS,ISPRIMARY,LASTCALLDATE,LASTNAME,LASTNAME_UC,LEADSOURCEID,MIDDLENAME,MOBILE,NEXTCALLDATE,NOTES,PREFERRED_CONTACT,PREFIX,PRIORITY,QUALIFICATION_CATEGORYID,REVENUE,SECCODEID,SICCODE,STATUS,SUFFIX,TICKER,TITLE,TOLLFREE,TYPE,USERFIELD1,USERFIELD2,USERFIELD3,USERFIELD4,USERFIELD5,USERFIELD6,USERFIELD7,USERFIELD8,USERFIELD9,USERFIELD10,WEBADDRESS,WORKPHONE,LEAD_ADDRESSID,DONOTEMAIL,DONOTFAX,DONOTMAIL,DONOTPHONE Q134915558 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Harry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1 ,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ134915558 ,NULL,NULL,NULL,NULL Q39769667 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper2,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ39769667 ,NULL,NULL,NULL,NULL Q488888476 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ488888476 ,NULL,NULL,NULL,NULL Q504845720 ,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000G,2011-09-20 17:06:10.053,NULL,Rafner ext.,RAFNER EXT.,NULL,NULL,NULL,NULL,0,raf#raf.com,NULL,NULL,James,4075615519,NULL,NULL,sales,NULL,NULL,NULL,Rafner,RAFNER, ,NULL,NULL,NULL,Raf associates is asking a question,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,QQ504845720 ,NULL,NULL,NULL,NULL Q539171226 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper3,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ539171226 ,NULL,NULL,NULL,NULL Q547088411 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ547088411 ,NULL,NULL,NULL,NULL Q913526837 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Parry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site agan,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1 ,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ913526837 ,NULL,NULL,NULL,NULL Q925684753 ,U6UJ9A00000S,2011-09-21 09:36:10.420,U6UJ9A00000S,2011-09-21 09:36:10.420,NULL,2011-09-21 09:36:10.420,NULL,POSfasion,POSFASION,NULL,NULL,NULL,NULL,0,sdfa#ss.com,NULL,NULL,Maria,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,becker4,BECKER4,L6UJ9A000004,NULL,NULL,NULL,this is another lead from the live site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,7778889999,QQ925684753 ,NULL,NULL,NULL,NULL
If there are characters after the 1 in the userField1 then try using a wildcard, as shown below. select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1%' Also the statement you wrote select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1' Is essentially equivalent to select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 = '1' You may want to read up on using the LIKE clause http://msdn.microsoft.com/en-us/library/ms179859.aspx
Use where USERFIELD1 = '1 ' You don't need LIKE as you aren't using any wild cards.
You're using LIKE wrong. Put a wildcard or a pattern in there or use an = operator. Your data seems malformed.
Try a like with the value surrounded by a wildcard ie USERFIELD1 like '1%' Should sort out whatever problem you're having if it is related to the line break field