Bigquery type cast on JOIN Clause - google-bigquery

I'm trying to join two tables on two columns
-- query to join two tables
SELECT
*
FROM
[raw.raw_sales] AS game_records
JOIN
[facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
(game_records.Away_team = avg_aggregate.team_name)
AND (game_records.game_date = avg_aggregate.time_update)
it gives me this error Error: 10.30 - 10.56: Timestamp literal or explicit conversion to timestamp is required. because game_records.game_date is type STRING and avg_aggregate.time_update is type DATE.
but if I do the conversion within the JOIN..ON.. clause
-- query to join two tables
SELECT
*
FROM
[raw.raw_sales] AS game_records
JOIN
[facebook_aggregate.avg_aggregate] AS avg_aggregate
ON
(game_records.Away_team = avg_aggregate.team_name)
AND (DATE(game_records.game_date) = DATE(avg_aggregate.time_update))
It gives me this error:
Error: ON clause must be AND of = comparisons of one field name from each table, with all field names prefixed with table name. .
Is there any way to do this without creating an intermediate table? Thanks!

Try using standard SQL (uncheck "Use Legacy SQL" under "Show Options"). You shouldn't need to do anything aside from remove the brackets around the table names:
SELECT
*
FROM
raw.raw_sales AS game_records
JOIN
facebook_aggregate.avg_aggregate AS avg_aggregate
ON
game_records.Away_team = avg_aggregate.team_name
AND game_records.game_date = avg_aggregate.time_update;

BigQuery Standard SQL (see Enabling Standard SQL) does not have this limitation for ON clause. Try running your query in Standard SQL.
As Elliott mentioned - make sure you are not using square brackets around tables references. In Standard SQL - when you need to escape special chars - yo should use back-ticks
Also check Migrating from legacy SQL if you will follow above direction

Related

Creating a view in PostgresSQL for a query that worked in SQL Server (COUNT between two DATETIME columns/TIMECODE

I have a problem with Redshift where it throws out a syntax error when trying to create a view similar to the following:
SELECT
*,
(SELECT COUNT(*)
FROM [Leads - leads]
WHERE Receive_Time BETWEEN dbo.[Leads - extended spot summary fla32813].[Plan Air]
AND.dbo.[Leads - extended spot summary fla32813].[Plan End]) AS Leads
INTO
[32813DA]
FROM
.dbo.[Leads - extended spot summary fla32813]
`
This code has numerous errors from a Redshift perspective:
It is using square braces as delimiters. Redshift (and the SQL Standard) is double quotes.
The three-part naming doesn't look right for Redshift.
into is not allowed in views in any of the mentioned databases.
You may have other errors as well, but you need to get the names right before you start with anything related to logic.
The SELECT ... INTO ... syntax is specific to SQL Server and create a table from a SELECT. The ISO SQL syntax is CREATE TABLE AS (SELECT ...) but does not woks with SQL Server.
Use a more simple an sharable syntax like :
CREATE TABLE ??? (..);
INSERT INTO ???
SELECT ...

Compare two CLOB columns in Oracle SQL Developer

I am trying to compare two columns of CLOB datatype in Oracle SQL Developer. Below is the query used,
select *
from t1
join t2 on t1.reference_no = t2.reference_no
where dbms_lob.compare (t1.text, t2.t_col_clob) = 0;
Getting error message as
ORA-00904: : invalid identifier.
This means I don't have access to dbms_lob.compare
How comparison can be done with using dbms_lob.compare?
Please note raised this as separate because all the information available is using dbms_lob.compare, Found nothing how this can be done without using dbms_lob.compare and also I won't be getting access to dbms_lob.compare. Please suggest a SQL way to compare this.

BigQuery: Querying repeated fields

I'm trying to use the following query to get the rows for which the event names are equal to: EventGamePlayed, EventGetUserBasicInfos or EventGetUserCompleteInfos
select *
from [com_test_testapp_ANDROID.app_events_20170426]
where event_dim.name in ("EventGamePlayed", "EventGetUserBasicInfos", "EventGetUserCompleteInfos");
I'm getting the following error: Cannot query the cross product of repeated fields event_dim.name and user_dim.user_properties.value.index.
Is it possible to make it work by not having a flattened result ?
Also, I'm not sure why the error is talking about the "user_dim.user_properties.value.index" field.
The error is due to the SELECT *, which includes all columns. Rather than using legacy SQL, try this using standard SQL, which doesn't have this problem with repeated field cross products:
#standardSQL
SELECT *
FROM com_test_testapp_ANDROID.app_events_20170426
CROSS JOIN UNNEST(event_dim) AS event_dim
WHERE event_dim.name IN ("EventGamePlayed", "EventGetUserBasicInfos", "EventGetUserCompleteInfos");
You can read more about working with repeated fields/arrays in the Working with Arrays topic. If you are used to using legacy SQL, you can read about differences between legacy and standard SQL in BigQuery in the migration guide.

SAS read a table from a SQL Server connection with name containing special characters

I have a connection in SAS to a sql server table where the table name is 'Additions_to_Aggregate$'. The quotes are part of the name. So in my SAS editor when I try to run the below in part of my code, I'm returned errors because SAS is reading it as string rather than as the table name.
proc sql;
Create Table Name_Compare as
SELECT DISTINCT a.Insured_Name, agg.Policy_Holder_Name, a.Segment
FROM MySQLLib.ADV_Portfolio_Split as a
LEFT JOIN MySQLLib.'Additions_to_Aggregate$'n.data as agg
on a.Insured_Name = agg.Policy_Holder_Name;
quit;
Is there any way to force SAS to read the table name as a literal string, or do you have any other solution ideas? I already tried renaming the table in SAS explorer but I get this error and don't know how to interpret it.
You're looking for a name literal. Either:
LEFT JOIN MySQL.'Additions_to_Aggregate$'n
or
LEFT JOIN MySQL."'Additions_to_Aggregate$'"n
depending on how SAS handles the quotes in the DBMS connection; it may or may not require the second, outside pair of quotes. If for some reason you need single quotes around it (SAS doesn't have any special meaning for single/double outside of macro resolution), you can double them up:
LEFT JOIN MySQL.'''Additions_to_Aggregate$'''n

Using an Alias column in the where clause in ms-sql 2000

I know you cannot use a alias column in the where clause for T-SQL; however, has Microsoft provided some kind of workaround for this?
Related Questions:
Unknown Column In Where Clause
Can you use an alias in the WHERE clause in mysql?
“Invalid column name” error on SQL statement from OpenQuery results
One workaround would be to use a derived table.
For example:
select *
from
(
select a + b as aliased_column
from table
) dt
where dt.aliased_column = something.
I hope this helps.
Depending on what you are aliasing, you could turn it into a user defined function and reference that in both places. Otherwise your copying the aliased code in several places, which tends to become very ugly and means updating 3+ spots if you are also ordering on that column.