Using UDTF on Join clause - sql

I'm trying to use UDTF on a join and for some reason I'm receiving an error which says that the UDTF is not recognizing the 1st table on the statement:
SHOW TRANSACTIONS IN ACCOUNT;
CREATE OR REPLACE TEMPORARY TABLE UTIL_DB.PUBLIC.TRANSACTIONS AS
SELECT * from table(result_scan(last_query_id()));
SELECT *
FROM UTIL_DB.PUBLIC.TRANSACTIONS AS T
JOIN table(information_schema.QUERY_HISTORY_BY_SESSION(T."id")) Q
WHERE Q.EXECUTION_STATUS = 'RUNNING'
AND T."id" != CURRENT_SESSION();
ERROR:
SQL compilation error: error line 3 at position 55
invalid identifier 'T."id"'
The motivation for this query is getting the sql text of the active transaction
Can someone please advise how can I resolve it?
Thanks

It seems that the issue is when trying to pass the identifier instead of "constant" into information_schema.query_history_by_session().
As an alternative information_schema.query_history() could be used:
SELECT *
FROM tab AS T
join table(information_schema.query_history()) Q
ON T."id" = Q.session_id -- here the join instead of correlation
WHERE Q.EXECUTION_STATUS = 'RUNNING'
AND T."id" != CURRENT_SESSION();

Related

INSERT INTO with JOINS in Oracle

Hi I try to adapt this blog post to my needs using Oracle to model a Data Vault architecture.
With the following code I try to build a link between two hubs referencing on the original source table "orders".
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders as src
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order AS dest
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
However I receive following error.
Error at Command Line : 192 Column : 17
Error report -
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
*Cause:
*Action:
Any help or hint much appreciated!
There are two places where you have used the as keyword. For giving alias to the table you just need to write the name of the alias after table name. as should not be used there.
Try the following code: (changes are mentioned inline)
INSERT INTO l_customer_order (customer_id_hk, order_id_hk, load_date, record_source)
SELECT DISTINCT h_customers.customer_id_hk, h_orders.orders_id_hk, SYSDATE, 'Customer+Order'
FROM orders src -- removed AS from here
LEFT OUTER JOIN h_orders
ON (h_orders.order_id = src.order_id)
LEFT OUTER JOIN h_customers
ON (h_customers.customer_id = src.customer_id)
LEFT OUTER JOIN l_customer_order dest -- removed AS from here
ON (dest.customer_id_hk = h_customers.customer_id_hk)
AND (dest.order_id_hk = h_orders.order_id_hk)
WHERE dest.order_id_hk IS NULL;
Cheers!!
Remove the as in your query i.e. instead of
FROM orders as src
use
FROM orders src
Oracle doesn't support the as alias, unless it is/will be added in most current versions

Handling Duplicate Column Name Issue in MariaDB Count Query

I am getting an error on the query I'm running to get a count in MariaDB. This is the error:
Error Code: 1060. Duplicate column name 'id_number'
And this is my SQL code:
SELECT COUNT(*) as count FROM (
SELECT * FROM ((cr.customers
INNER JOIN (progress_notes_details
INNER JOIN progress_notes ON progress_notes_details.progress_note_id = progress_notes.id_number)
ON customers.id_number = progress_notes.c_id)
INNER JOIN open_balances ON progress_notes_details.id_number = open_balances.progress_notes_detail_id)
INNER JOIN
customer_payer_xref ON customers.id_number = customer_payer_xref.c_id
WHERE
(((progress_notes_details.qb_isbillable) IS NULL
OR (progress_notes_details.qb_isbillable) <> 1)
AND ((progress_notes_details.date_of_visit) BETWEEN coverage_start AND coverage_end)
AND ((progress_notes_details.dynamics_status) = 3)
AND ((customer_payer_xref.payer_id) = 23)
AND ((customer_payer_xref.primary_secondary_account_type) = 1))
) AS qdat
Can this be resolved via aliases? If so, it's unclear to me where to add them. In the main query? In the subquery?
Also, to clarify, I just inherited this code - and yes, it's bracket-happy.
Alias customers table as c and use it as c.id_number at one of the places it will remove that duplicate error as this is the only table you are using multiple times with idnumber hence duplicate
Remove the outer query:
SELECT COUNT(*)
FROM ((cr.customers . . .
Clearly, you have tables with the same column name. This causes a problem with SELECT *.
All the parentheses are probably not needed for the JOINs as well.

How to do a join in hive with a subquery?

Im using hive 1.2.1 and I'm running into some problems when trying to join using a subquery.
My main table is applications and I'm trying to join it to table credits, based on account and dates. The date condition is giving me troubles when I try to get just one row (the credit has to be after the application, and it can only be one to avoid dupes in the join). I'm using the following code:
SELECT COUNT(1)
FROM applications apps
LEFT JOIN credits c
ON c.python_id =
(
SELECT python_id
FROM credits cr
WHERE cr.ind in ('NP','0P')
AND cr.acct_nbr = apps.acct_nbr
AND cr.date >= apps.date
ORDER BY cr.date DESC
LIMIT 1
)
I'm getting the following error:
[Code: 40000, SQL State: 42000] Error while compiling statement: FAILED: ParseException line 8:24 cannot recognize input near 'SELECT' 'python_id' 'FROM' in expression specification
Could you please help?
Thank you
Issue with your query is
> hive does not support sub query with equals clause, you can write sub query only for IN, NOT IN, EXISTS and NOT EXISTS clause.
> You cannot have a sub query which returns more than one row.
Please look into - [https://cwiki.apache.org/confluence/display/Hive/Subqueries+in+SELECT][1]
There is issue with you logic as well.
My understanding is You are trying to get count from main table wit left join and there is no filter condition defined on outer query to say what records you want.
So the count will always be equal to number of records in main table (applications), If you can provide sample data with expected input and output, we can help you with the query.
Hope this helps.
You should join based on act_nbr and use the date as a filter in the where clause.
SELECT COUNT(1)
FROM applications apps
JOIN credits c
ON c.acct_nbr = apps.acct_nbr
WHERE c.ind in ('NP','0P')
AND c.date >= apps.date
ORDER BY c.date DESC
LIMIT 1

Using a select statement as criteria for an update query

I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))

Teradata Update Table from Select Statement

Sorry if the title is unclear. Basically I'm trying to select certain records from multiple tables then update a certain column value for the returned records.
T-SQL Implementation
UPDATE
CUSTOMERS
SET
LIKES_US = 'Y'
FROM
RESTAURANT REST INNER JOIN CUSTOMERS CUST ON REST.LINK_ID = CUST.LINK_ID
WHERE
REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL
Oracle
UPDATE
(SELECT CUST.LIKES_US
FROM CUSTOMERS CUST INNER JOIN RESTAURANT REST ON CUST.LINK_ID=REST.LINK_ID
WHERE REST.REST_TYPE = 'Diner' AND CUST.LIKES_US IS NULL) NEW_CUST
SET
NEW_CUST.LIKES_US = 'Y';
I am tried doing the same thing in Teradata as I did in Oracle but I get the following error:
Executed as Single statement. Failed [3707 : 42000] Syntax error, expected something like a name or a Unicode delimited identifier or an 'UDFCALLNAME' keyword between the 'UPDATE' keyword and '('.
Elapsed time = 00:00:00.003
STATEMENT 1: Unknown failed.
I looked online for the solution but had no luck.
Have you tried the following syntax with Teradata:
UPDATE CUSTOMERS C1
FROM (SELECT C2.LINK_ID
FROM CUSTOMERS C2
INNER JOIN RESTAURANTS R2
ON C2.LINK_ID = R2.LINK_ID
WHERE R2.REST_TYPE = 'DINER'
AND C2.LIKES_US IS NULL) D1
SET LIKES_US = 'Y'
WHERE C1.LINK_ID = C2.LINK_ID
I think that in this specific case, the below query will perform a little better since it needs one less join.
UPDATE C
FROM CUSTOMERS C, RESTAURANTS R
SET LIKES_US = 'Y'
WHERE
C.LINK_ID = R.LINK_ID
AND R.REST_TYPE = 'DINER'
AND C.LIKES_US IS NULL