How to Join (equal) two data columns that belongs to the String and Double types respectively in Alibaba MaxCompute? - sql

I am not authorized to share the table details.
For instance, let me consider an Example:
I am trying to join two columns of String and Double data types respectively in Alibaba MaxCompute.
In the earlier version of MaxCompute, the String and Double data types are converted to the bigint data type at the cost of precision. 1.1 = “1” in a Join condition.
Whereas the same code does not work in the new version of the MaxCompute. The code syntax is like follows:
SELECT * FROM t1 JOIN t2 ON t1.double_value = t2.string_value;
Error:
WARNING:[1,48] implicit conversion from STRING to DOUBLE, potential data loss, use CAST function to suppress
What is the correct syntax to do the join operation in Alibaba MaxCompute V2?

I did a bit of digging and it seems like this SQL command is the recommended way of getting around this issue.
select * from t1 join t2 on t.double_value = cast(t2.string_value as double);

As the error message suggests:
SELECT *
FROM t1 JOIN
t2
ON CAST(t1.double_value as string) = t2.string_value;

Related

SQL INNER JOIN syntax Error BigQuery // New to SQL

I'm getting this error "No matching signature for operator = for argument types: INT64, STRING. Supported signature: ANY = ANY at [9:6]
" while trying to JOIN two table in BQ, Login ID column is listed in the two tables but BQ shows that there's an error on line 9enter image description here
SELECT
performance.name,
performance.ahtdn,
tnps.tnps,
FROM
`data-exploration-2023.jan_scorecard_2023.performance-jan-2023` AS performance
LEFT JOIN
`data-exploration-2023.jan_scorecard_2023.tnps-jan-2023` AS tnps
ON performance.login_id = tnps.login_id
I've checked the syntax for INNER JOIN online and on BQ documentation but I couldn't find the reason why I get this error.
The error should be due to trying to join INT64 and STRING column. It should be resolved casting the numeric column to text one like this:
CAST(performance.login_id AS STRING) = tnps.login_id
or cast the second one if it is the numeric.

EXTERNAL_QUERY suddenly started to return BYTE value instead of STRING

I'm using Query which joins external data through EXTERNAL_QUERY() LIKE THIS
(this is just example, not actual one)
SELECT
ext.program_id,
SUM(price) AS total_price
FROM a_dataset.purchases pcs
LEFT OUTER JOIN (
SELECT
program_id,
version
FROM EXTERNAL_QUERY(
'CONNECTION_INFO',
'SELECT program_id, version FROM products'
)
) ext ON pcs.program_id = ext.program_id
This query actually worked at my environment.
However, from today, this part ↓
EXTERNAL_QUERY(
'CONNECTION_INFO',
'SELECT program_id, version FROM products'
)
starts to return byte value which looks like encrypted and
turns out to show this message
No matching signature for operator = for argument types: STRING, BYTES. Supported signatures: ANY = ANY at [37:9]
'CONNECTION_INFO' refers Cloud SQL, read replica instance of MySQL.
Do you have any ideas how to fix this, or why these return values started to changed ?

Teradata - Comparing Varchar to decimal

I am very new to Teradata and SQL in general. I need to create a table by combining data from three tables. I was able to successfully join two of them. I am not able to write the joining condition for the third table properly. Here is the code:
select s.cola, s.colb,
t.colc, t.cold,
u.cole, u.colf, u.colg, u.colh, u.coli, u.colj, u.colk, u.coll
from table1 s
inner join table2 t
on s.colb = t.colc
inner join table3 u
on t.cold = cast(u.colm as decimal)
order by 3
where substr(cast(s.cola as varchar(10)),6,2) = 11 and substr(cast(s.cola as varchar(10)),1,4) = 2017 and substr(cast(s.cola as varchar(10)),9,2) between 06 and 10
The error I am getting is:
[Teradata Database] [2620] The format or data contains a bad character.
I think the problem is with the line: on t.cold = cast(u.colm as decimal). The u.colm is of type VARCHAR(50) while t.cold is of type DECIMAL(10, 0). I believe I have casted it properly. Please help.Thanks in advance.
There's some bad data in u.colm.
Depending on your Teradata release you can check it using
WHERE u.colm > '' AND TRYCAST(u.colm as decimal(10,0)) ISNULL
or
WHERE u.colm > '' AND TO_NUMBER(u.colm) IS NULL
You can also use those in the join-condition, e.g.
on t.cold = trycast(u.colm as decimal(10,0))
Don't forget to add the precision of the decimal, as it defaults to (5,0).
Your WHERE_condition is strange, what's the datatype of s.cola?
Seems it's a string with a date yyyy-mm-dd in it. Try
WHERE trycast(s.cola as date) between date '2017-11-06' and date '2017-11-10'
Finally the ORDER BY should be placed after WHERE.

Upper to Lowercase conversion in SQL Server

I have the below columns in from 2 different tables -
DimTeamProject.ProjectNodeGUID DimIteration.ProjectGUID
------------------------------ ------------------------
FAE8B08E-286E-487D-B1C1-011853028CDB fae8b08e-286e-487d-b1c1-011853028cdb
I was trying a join operation while matching the case. It gave me an error like
Conversion failed when converting from a character string to uniqueidentifier.
The query I was trying was -
select
p.ProjectNodeName, i.IterationName
from
DimTeamProject p, DimIteration i
where
(p.ProjectNodeGUID) = UPPER(i.ProjectGUID)
I tried the "char" and "cast" function too but without success. Please help.
DimIteration.ProjectGUID is an "nvarchar" & DimTeamProject.ProjectNodeGUID is an "uniqueidentifer"
Just cast the appropriate side as a uniqueidentifier:
select p.ProjectNodeName, i.IterationName
from DimTeamProject p
inner join DimIteration i on p.ProjectNodeGUID =
CAST(i.ProjectGUID as uniqueidentifier)
See the demo with a SQL Fiddle.

Firebird - How to use "(? is null)" for selecting blank parameters

I am working with an Excel Report linked to a Firebird 2.0 DB and I have various parameters linked to cell references that correspond to drop down lists.
If a parameter is left blank, I want to select all the possible options. I am trying to accomplish this by putting ... WHERE... (? is null), as described in http://www.firebirdsql.org/refdocs/langrefupd25-sqlnull.html , but I get an "Invalid Data Type" error.
I found some Firebird documentation (http://www.firebirdfaq.org/faq92/) where it talks about this error, but it states that "The solution is to cast the value to appropriate datatype, so that all queries return the same datatype for each column." and I'm not quite sure what that means in my situation.
SELECT C.COSTS_ID,
C.AREA_ID,
S.SUB_NUMBER,
S.SUB_NAME,
TP.PHASE_CODE,
TP.PHASE_DESC,
TI.ITEM_NUMBER,
TI.ITEM_DESC,
TI.ORDER_UNIT,
C.UNIT_COST,
TI.TLPE_ITEMS_ID
FROM TLPE_ITEMS TI
INNER JOIN TLPE_PHASES TP ON TI.TLPE_PHASES_ID = TP.TLPE_PHASES_ID
LEFT OUTER JOIN COSTS C ON C.TLPE_ITEMS_ID = TI.TLPE_ITEMS_ID
LEFT OUTER JOIN AREA A ON C.AREA_ID = A.AREA_ID
LEFT OUTER JOIN SUPPLIER S ON C.SUB_NUMBER = S.SUB_NUMBER
WHERE ((C.AREA_ID = 1 OR C.AREA_ID = ?) OR **(? IS NULL))**
AND ((S.SUB_NUMBER = ?) OR **(? IS NULL))**
AND ((TI.ITEM_NUMBER = ?) OR **(? IS NULL))**
AND ((TP.PHASE_CODE STARTING WITH ?) OR **(? IS NULL))**
ORDER BY TP.PHASE_CODE
Any help is greatly appreciated.
If you are not using Firebird 2.5 (but version 2.0 or higher), or if you are using a driver that doesn't support the SQL_NULL datatype introduced in Firebird 2.5, then you need to use an explicit CAST, eg;
SELECT *
FROM TLPE_ITEMS TI
WHERE TI.ITEM_NUMBER = ? OR CAST(? AS INTEGER) IS NULL
This will identify the second parameter as an INTEGER to the driver (and to Firebird), allowing you to set it to NULL.
Now the faq you reference mentions cast the value to appropriate datatype, what they mean is that you should not cast to a data type that might result to conversion errors if it isn't null.
In my example I cast to INTEGER, but if the values are actually strings and you use say "IX0109302" as a value, you will get a conversion error as it isn't an appropriate INTEGER. To prevent that, you would need to cast to a (VAR)CHAR of sufficient length (otherwise you get a truncation error).
If you are using Firebird 1.5 or earlier this trick will not work, see CORE-778, in that case you might get away with something like TI.ITEM_NUMBER = ? OR 'T' = ?, where you set the second parameter to either 'T' (true) or 'F' (false) to signal whether you want everything or not; this means that you need to move the NULL detection to your calling code.