Alias Name issue in SNowflake - snowflake-cloud-data-platform

I am using a single query:
(SELECT *,'{"Source": "010","Keys": [{"Value": "CHARGE"}]}' AS ID1
FROM DETAILS) metrics
JOIN DIM.TYPE trant
ON metrics.ID1 = trant.BK_TRANSACTIONTYPE
But I am getting this error:
SQL compilation error: syntax error line 3 at position 30 unexpected
'metrics'.
Please advise how it can be solved.

Can you please try with below query. Hope it works :)
SELECT * FROM (SELECT *,'{"Source": "010","Keys": [{"Value": "CHARGE"}]}' AS ID1 FROM DETAILS) metrics JOIN DIM.TYPE trant ON metrics.ID1 = trant.BK_TRANSACTIONTYPE

Related

Query not running properly

I ran a query using google big query but I get an error as a result. I don't know what could be wrong.
SELECT
usertype,
CONCAT(start_station_name, " to ", end_station_name) AS route,
COUNT(*) as num_trips,
ROUND(AVG(CAST(tripduration AS INT64)/60),2) AS duration
FROM
`bigquery-public-data.new_york_citibike.citibike_stations`
GROUP BY
start_station_name, end_station_name, usertype
ORDER BY
num_trips DESC
LIMIT 10
You must be getting this error: Unrecognized name: usertype . As the error suggests bigquery-public-data.new_york_citibike.citibike_stations dataset doesn't have any column usertype.
As #samuel mentioned, usertype column is present in new_york_citibike.citibike_trips. Do cross check your dataset before querying.

AWS Babelfish co-releated sub query provides in correct results

We are migrating a sql server Database to AWS Aurora Postgres with Babelfish enabled. I connected the Babelfish DB from SSMS through 1433 port. While I ran Babelfish compass report and fixed the un supported functionality. There are few issues which occurred as I was actually modifying the queries/SP's. One such issue is, there is a co-related sub query in an SP and when I created that SP in Babelfish, it was all good but when I execute the SP, it errors out with said error below. I tried replacing that with joins and tried using exists, CTE etc, I am unable to replicate the original results. Any help would be greatly appreciated. Below is the sample query. Any help would be appreciated. Thanks.
SELECT t.POLICY_DOCUMENT_ID,
convert(xml, (select rd.Id as TagId, rd.def_id as RelatedDataDefId, rdd.name_tx as [Name], rd.VALUE_TX as TagCode, rdd.DATA_TYPE_CD as TagDataType, rdd.DOMAIN_CD as TagDomainName
from
TEST_DATA rd
join TEST_DATA_DEF rdd on rdd.relate_class_nm = 'PolicyDocument' and rd.def_id = rdd.id and rdd.ACTIVE_IN = 'Y'
--join #tempPDIDs th on rd.relate_id = th.POLICY_DOCUMENT_ID
where rd.relate_id = t.POLICY_DOCUMENT_ID
for xml PATH('PolicyDocumentTag'), ROOT('PolicyDocumentTags'))) AS POLICY_DOCUMENT_TAGS_XML
from
#tempPDIDs t
join TEST_DOCUMENT pd on pd.id = t.POLICY_DOCUMENT_ID and pd.PURGE_DT is null
left join #tempPDIDFTSTX tmp on pd.id = tmp.POLICY_DOCUMENT_ID
Error:
Msg 33557097, Level 16, State 1, Line 72
missing FROM-clause entry for table "t"

Unsupported subquery type cannot be evaluated - How to fix this?

Update upd_tbl a1
set last_update_date=sysdate(),
rno=(select rno from sid b1
where a1.rowid_object=b1.rowid_object
)
where rowid_object in (select rowid_object from sid);
When I run the above query, I'm getting this error - SQL compilation error: Unsupported subquery type cannot be evaluated.
Kindly guide how to fix this error in Snowflake.
Thanks!
This should work, however, it should be noted that if you have a many-1 relationship between the rowid_object join condition, this may need to be revisited using exists logic for a boolean test:
https://docs.snowflake.com/en/sql-reference/operators-subquery.html
The following code reflects the pattern used for updating a table based on another table's data:
https://docs.snowflake.com/en/sql-reference/sql/update.html
Update
upd_tbl a1
set
a1.last_update_date = sysdate(),
a1.rno = b1.rno
from sid b1
where a1.rowid_object = b1.rowid_object;

How to find table and column in DB2 with tbspaceid tableid specified in error message

I get following error message when trying to insert an object in the database:
com.ibm.db2.jcc.am.SqlIntegrityConstraintViolationException:
DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC=TBSPACEID=2,
TABLEID=19, COLNO=0, DRIVER=4.15.134
How can I retrieve the table/column name for which the error is thrown?
Apparently at the package level, DB2 only works with the IDs and not the names.
You can find them back using the following query:
SELECT C.TABSCHEMA, C.TABNAME, C.COLNAME
FROM SYSCAT.TABLES AS T,
SYSCAT.COLUMNS AS C
WHERE T.TBSPACEID = 2
AND T.TABLEID = 19
AND C.COLNO = 0
AND C.TABSCHEMA = T.TABSCHEMA
AND C.TABNAME = T.TABNAME

SQL Duplicate column names error in Create View, even with aliases

The error is oddly specific, but I've just about honed in where it's occurring. The standard fix doesn't seem to be working though.
Here's where the error occurs. It's a big statement, so I'm just posting where I've found the error
CREATE OR REPLACE VIEW SalesInvoiceDoc
AS
( SELECT si.salinv_Num, si.salinv_Terms, si.salinv_SaleDate,
es.empName AS SalesAgent, man.empName AS ApprovingManager,
si.salinv_ApproveDate, ...
... FROM service_invoice si
JOIN employee es
ON (es.empID = si.salinv_EmpID)
JOIN employee man
ON (man.empID = si.salinv_ManID)
Essentially it's a Sales invoice document with a sales agent and approving manager. Both are stored in the employee table with a subtype discriminator set up for 'manager' or 'sales', and views for the subtype children tables to avoid duplication errors.
I've supplied aliases to try and avoid duplicate columns, but when I run this script, I get the duplicate column names error:
ORA-00957: duplicate column name
Does anyone know how to resolve this? Is it even possible? Any help would be greatly appreciated. Thank you in advance!
Edit: here's the full statement
CREATE OR REPLACE VIEW SalesInvoiceDoc
AS
( SELECT si.salinv_Num, si.salinv_Terms, si.salinv_SaleDate,
es.empName AS SalesAgent,
man.empName AS ApprovingManager, si.salinv_ApproveDate,
sc.custName, sc.custHouse, sc.custCity,
sc.custState, sc.custZIP, sc.custPhone, sc.custEmail,
sv.vehicle_VIN, sv.vehicle_year, sv.vehicle_make,
sv.vehicle_model, sv.vehicle_ext_color, sv.vehicle_trim,
sv.vehicle_list_base_price, sv.vehicle_mileage, sv.vehicle_condition,
sv.vehicle_description,
ti.vehicle_make, ti.vehicle_year, ti.vehicle_model, ti.vehicle_VIN,
ti.tradein_allowance,
sv.vehicle_list_base_price "SellingPrice", sv.shipping "Shipping",
ti.tradein_allowance "TradeAllowance",
(sv.vehicle_list_base_price + sv.shipping - ti.tradein_allowance) "Subtotal",
(sv.vehicle_list_base_price + sv.shipping - ti.tradein_allowance)*.0825 "Taxes",
(sv.vehicle_list_base_price + sv.shipping - ti.tradein_allowance)*1.0825 "TotalSellingPrice"
FROM sales_invoice si
JOIN employee es
ON (es.empID = si.salinv_EmpID)
JOIN employee man
ON (man.empID = si.salinv_ManID)
JOIN customer sc
ON (sc.custID = si.salinv_CustID)
JOIN vehicle sv
ON (sv.vehicle_VIN = si.salinv_SalVIN)
LEFT OUTER JOIN vehicle ti
ON (ti.vehicle_VIN = si.salinv_tiVIN)
);
You are duplicating column names from 2 different tables:
sv.vehicle_VIN,
sv.vehicle_year,
sv.vehicle_make,
sv.vehicle_model
and
ti.vehicle_make,
ti.vehicle_year,
ti.vehicle_model,
ti.vehicle_VIN
The resulting column name does not include the table alias.
Issue is cause sv.vehicle_VIN is appearing twice in your query and so does other columns. See below. You need to alias them all accordingly
**sv.vehicle_VIN**, **sv.vehicle_year**, **sv.vehicle_make**, **sv.vehicle_model**,
**ti.vehicle_make**, **ti.vehicle_year**, **ti.vehicle_model**, **ti.vehicle_VIN**,

Resources