Teradata update statement - teradata

I am trying to update column A of TABLE1 by joining with TABLE2 and finding the matching values of TABLE1.B and TABLE2.C
UPDATE TABLE1
from TABLE1 1,TABLE2 2
SET 1.A = 'Y'
WHERE 1.B = 2.C;
The error message I am getting is:
'Column/Parameter'database.TABLE1.T1' does not exist.
This is the same error message I have been having. I have checked and the database and table names are all written correctly and do exist

Related

Impala Using CASE to determine if an ID from one Tableis in another Table

Background:
Hey everyone! I'm hoping you can help me with something that I've been trying to figure out. I have a dataset/table called customer_universe that shows all of our in scope customers. Every row/cust_id in that table is unique.
Let's say this table has 60,000 total rows. Every cust_id entry in this table is unique so total rows = unique row count.
There is also a dataset that I created (customer_sport_product_purch) that lists out all of customers (from the customer_universe table) and any of the 3 in-scope sports products they purchased along with a purchase date. This tables only contains customers who have purchased one of the three sport products but since there are three sport products and a customer may have purchased multiple, cust_id field does not contain only unique customers.
Let's say this table has 46,000 total rows but only 25,000 unique customer.
Goal Query Output:
I need to write a query that lists out every customer in the customer_universe table and one more column with a binary (1/0) value that will indicate if they have purchased a sport product or not.
So this query output should have a total of 60000 records and only two columns.
Environment and Attempted Solutions Details
I'm currently building these queries using Impala in Hue. I'm trying to use a case statement to get me my desired result but I'm getting the error message provided below.
Customer_universe Table:
Cust_ID
Customer_Since
1
02-20-2019
2
01-13-2020
3
06-17-2012
4
06-19-2021
5
06-06-2017
Customer_sport_product_purch Table:
Cust ID
Product
Purch_Dt
1
Basketball
01-01-2022
1
BoxGlove
02-01-2020
5
BoxGlove
12-15-2019
Desired Query Output:
Cust_ID
Sport_Purch
1
1
2
0
3
0
4
0
5
1
Queries I've attempted and the Error Messages I've Received:
Query 1:
SELECT a.cust_id,
case when (a.cust_id in (select distinct b.cust_id from DB.customer_sport_purch b)
then 1 else 0 end as Sport_Purch
FROM DB.customer_universe
GROUP BY cust_id;
Error Message 1:
Error while compiling statement: FAILED: SemanticException [Error 10249]: line 2:72 Unsupported SubQuery Expression 'cust_id': Currently SubQuery expressions are only allowed as Where Clause predicates
Query 2:
SELET a.cust_id,
case when (a.cust_id in sportPurch) then 1 else 0 end as Sport_Purch
FROM DB.customer_universe a,
(select distinct cust_id from DB.customer_sport_purch) sportPurch
GROUP BY a.cust_id;
Error Message 2:
Error while compiling statement: FAILED: ParseException line 2:36 cannot recognize input near 'sportPurch' ')' 'then' in expression specification
Other Considerations:
I cannot bring bring the customer_sport_table.cust_id values into a text file and have the query read from file since those values will change frequently and need to be able to just re-execute queries.
Thanks in advance!

Using Joins and Views in SQL

I essentially have 4 tables, but not all the tables have common fields
Table 1 has A
Table 2 has A and B
Table 3 has B and C
Table 4 has C
so when I tried to join them all, it doesn't work because
SQLITE_ERROR: cannot join using column C - column not present in
all tables
Which I understand, not all the table share the same columns.
I tried creating a view (TABLE_ABC) using "table1, table2, and table3", then tried doing a join to that view
Join TABLE_ABC using (C)
but I get the same SQLITE Error.
So my questions are:
Is there a way to join all 4 tables even though they all do not share a column? Do I just need to create a 5th table using "table1, table2, and table3" and connect 4 to that?
Can you do a join to a view?

RMYSQL Writetable error

I have the following R dataframe
Sl NO Name Marks
1 A 15
2 B 20
3 C 25
I have a mysql table as follows. (Score.table)
No CandidateName Score
1 AA 1
2 BB 2
3 CC 3
I have written my dataframe to Score.table using this code
username='username'
password='userpass'
dbname='cdb'
hostname='***.***.***.***'
cdbconnection = dbConnect(MySQL(), user=username, password=userpass,
dbname=dbname, host=hostname)
Next we write the dataframe to the table as follows
score.table<-'score.table'
dbWriteTable(cdbconn, score.table, dataframe, append =F, overwrite=T).
The code runs and I get TRUE as the output.
However, when I check the SQL table, the new values haven't overwritten the existing values.
I request someone to help me. The code works. I have reinstalled the RMySQL package again and rerun and the results are the same.
That updates are not happening indicates that the RMySQL package cannot successfully map any of the rows from your data frame to already existing records in the table. So this would imply that your call to dbWriteTable has a problem. Two potential problems I see are that you did not assign values for field.types or row.names. Consider making the following call:
score.table <- 'score.table'
dbWriteTable(cdbconn, score.table, dataframe,
field.types=list(`Sl NO`="int", Name="varchar(55)", Marks="int"),
row.names=FALSE)
If you omit field.types, then the package will try to infer what the types are. I am not expert with this package, so I don't know how robust this inference is, but most likely you would want to specify explicit types for complex update queries.
The bigger problem might actually be not specifying a value for row.names. It can default to TRUE, in which case the package will actually send an extra column during the update. This can cause problems, for example if your target table has three columns, and the data frame also has three columns, then you are trying to update with four columns.

update one table with 2 where conditions in the same and one condition in another table

I have 2 tables fees and students. i want to update one field of fees with 3 WHERE conditions, i.e, 2 conditions in table 'fees' and 1 condition in table 'students'.
I tried many queries like
UPDATE fees, students SET fees.dues= 300 WHERE fees.month= November
AND fees.session= 2017-18 AND students.class= Nursery
It gives me error like java.sql.SQLException: near",": syntax error
I am using sqlite as database. Please suggest me a query or let me correct this query.
Thanks
You cannot join tables in a UPDATE command in SQLite. Therefore, use a sub-query in the where condition
UPDATE fees
SET dues = 300
WHERE
month = November AND
session = 2017-18 AND
student_id IN (SELECT id FROM students WHERE class=Nursery)
Also, I am not sure about the types of your columns. String literals must be enclosed in single quotes ('). The expression 2017-18 would yield the number 2017 minus 18 = 1999. Should it be a string literal as well?
UPDATE fees
SET dues = 300
WHERE
month = 'November' AND
session = '2017-18' AND
student_id IN (SELECT id FROM students WHERE class='Nursery')

Rename column of one table based on value of a row in another table

For example I have two tables - Table1 and Table2
Table1 with columns Q1,Q2 and Q3.
Q1 Q2 Q3 Map_id
11 23 34 11101
22 22 22 11102
Table2 with column refvalue, actualvalue
refvalue actualvalue Map_id
Q1 Remaining 11101
Q2 Utilized 11101
Q3 Actual 11101
Q1 Remaining1 11102
Q2 Utilized1 11102
Q3 Actual1 11102
Now i want to replace column_name of Table1 to corresponding value in Table2.
Example :
select Q1 as "Remaining", Q2 as "Utilized", Q3 as "Actual"
from Table1;
Now this is hard coded, how to make it logical ? I have no idea how to approach this and I am new in oracle.
Version : Oracle11g
Client : Toad
Although this is very bad relations design and I advise you to change it, it can be done with PL/SQL , something like this:(maybe adjust it a little bit)
select 'SELECT Q1 as '||MAX(CASE WHEN t2.ref_value = 'Q1' THEN t2.actual_value END)||',
Q2 as '||MAX(CASE WHEN t2.ref_value = 'Q2' THEN t2.actual_value END)||',
Q3 as '||MAX(CASE WHEN t2.ref_value = 'Q3' THEN t2.actual_value END)
||'FROM Table1'
FROM Table1 t1
INNER JOIN Table2 ON(1=1)
This will generate the sql you need
Create a log table, DDL_LOG_TAB.
Loop through the records in Table2 using implicit cursor.
For each record in Table2, build a string statement, "ALTER TABLE TABLE1 RENAME COLUMN rec.refvalue TO rec.actualvalue" where rec refers each record in the implicit cursor.
Inside the loop, insert each of the generated statement into DDL_LOG_TAB.
Now you have all the required DDLs in your new DDL_LOG_TAB. Just select the SQL and execute it as a dynamic SQL.
You may even improvise this new table to hold execution timestamps (start and end TS), error message or success flag. In future you can use this table for,
a. Generating and executing any kind of DDLs.
b. Referring past history of DDLs executed in chronological order.
c. If any DDL errors out and execution stops, you can fix the error and restart the execution right from the point of failure.
Hope this helps now and also in the future. Good luck.

Resources