I have two metadata tables, t1 and t2. I created a view to unite them:
spark.sql('CREATE OR REPLACE VIEW t3 AS select * from t1 left join t2 on t1.name = t2.name')
Now, I've added some columns to t1. Naturally, I had to execute
spark.sql ('refresh table t1')
and now I can see the updated t1. However, t3 still lacks the new columns. Of course, I can re-create it using the above 'CREATE OR REPLACE' command, but I need to do it periodically, and I'd like t3 to reflect the changes immediately and automatically. Is there any way to do it?
Related
Using SSMS when joining 3+ tables and using SELECT *, I'm wondering if there is an easy way (dynamic) to include the table name & column name in the result set without having to type out all the desired columns.
For example:
Table1
Table2
Table3
SELECT *
FROM Table1 t1
LEFT JOIN Table2 t2 ON t2.keyA = t1.keyA
LEFT JOIN Table3 t3 ON t2.keyB = t3.keyB
Trying to produce output like
Table1-Column1, Table2-Column1, Table3-Column1
OR
t1.Column1, t2.Column1, t3.Column1
If you have a lot of column, there's is an easy way to do it, but not dynamically. If you have only a few columns, doesn't worth a try and do it manually.
Take a look at theses two methods : https://blog.sqlauthority.com/2012/06/06/sql-server-tricks-to-replace-select-with-column-names-sql-in-sixty-seconds-017-video/
First method (drag'n'drop columns) isn't useful in your case cause we need to have all column aligned on different lines. The second (generating the create table script) is the one we need.
Generate the create table for table1 and copy-paste the columns needed from table1 into your query, and repeat for table2 and table3. Make sure they are all aligned in the same editor column.
Then, "the magic trick", simply press and hold the Alt key, click between the coma and the first letter of the first column name of t1 and drag to select the lines between the first column of t1 to the last column of t1. Type "t1.". That's it. repeat for t2 and t3.
This is not sql-server related, nor SSMS, but works with any decent editor supporting multiline editing.
I have in an Oracle database the following situation:
Table T1 with attributes ID and T2_ID.
Table T2 with attributes ID and T3_ID.
Table T3 with attributes ID and T4_ID.
Table T4 with attribute ID.
Each TX_ID is referring to the ID of the related table (example: T3_ID = ID in table T3).
Given an ID in T1, I would like to write a SQL-script to know the related T3_ID and T4_ID, store them in single variables (containing only the retrieved values) and use those values for other operations of DELETE/SELECT in the same script (like DELETE all the rows from another T* table which have T*_ID = T3_ID).
Is there a way to do that in Oracle?
Inside a PLSQL block, use something like this to fetch data in variables v_t3_id and v_t4_id and use that in your delte/update statements.
select t3.id,t4.id into v_t3_id,v_t4_id
from t1
left join t2 on t1.t2_id=t2.id
left join t3 on t2.t3_id=t3.id
left join t4 on t3.t4_id=t4.id
where t1.id=<your value>
If data set is huge, then google on how to use BULK COLLECT to do the same.
Hi i am trying to perform left join on two tables t1 and t2, please note that t2 is a subset of t1 but contains a column called 'category' that i want to be in the resultant table. Both the tables contain id as the primary key and i want to take the values for column 'category' in t2 and id values from t1. My code is as follows
create table new_table as
select t1.id,t2.category
from xyz as t1 left join abc as t2
on t1.id=t2.id
for some reason when i limit the result to 100 using limit 100 with the above query, i get the results but when i am trying to do it for all the data the query never shows any result and is in the execution mode only.
Any help will be greatly appreciated. Thank you.
I can do this in an ugly stored procedure with temp tables and whatnot, but I know an experienced developer could do this SO much more elegantly than what I've come up with. In fact, I'd kind of rather not have to call the sproc at all, but just have one query that gives me what I need.
I'm working with two tables:
T1 BillingDirectivesNeeded
T2 BillingDirectives.
T1 Has two fields relevant to this task -
PKey
WBS1.
There will be many PKeys associated with each WBS1.
T2 has only one field of interest
PKey.
The task I'm trying to address is geting a list of WBS1s from T1 that have ALL of their needed directives in T2 before I enable their import.
We want to import a WBS1 ONLY when all of the PKeys for that WBS1 are found in T2. If not, I'll just leave them grayed out.
I've tried a dozen different ways to get this to happen over the last few hours, and I seem to have a mental block. The pseudo-code would look something like this:
select T1.WBS1 from BillingDirectiveNeeded T1
where [all the T1.PKeys for T1.WBS1 can be found in BillingDirectives T2]
You can try using a Where Exists clause:
Select T1.WBS1
From BillingDirectiveNeeded T1
Where Exists
(
Select 1
From BillingDirectives T2
Where T2.PKey = T1.PKey
)
select DISTINCT T1.WBS1 from BillingDirectiveNeeded T1 where T1.PKey in (SELECT T2.PKey FROM BillingDirectives T2)
I have two tables.
Lets say:
Table T1 with columns id,ref,a1,b1,c1,d1,e1
Table T2 with columns id,ref,a2,b2,c2,d2,e2
I need to update few columns in T2(a2,c2,e2) with respect to values in T1(a1,c1,e1) where T1.ref = T2.ref, given that ref=<certain value>.
For a particular value of ref column there are many records in each table.
I want to update T2 with respect to T1 for one particular value of ref column. Other records will be untouched.
I am currently doing this by dropping all the rows of T2 and inserting the current rows from T1 where ref=<some value>.
For ex: if the ref value=5
then I do these steps.
1. delete from T2 where ref=5;
2. insert into T2 (a2,c2,e2) select a1,c1,e1 from T1 where T1.ref = 5;
Certainly this is not a good method to synchronize the data between the two table.
Please suggest me an efficient solution to achieve this in Oracle.
I think i missed an important point. Both the tables have one column which clearly identifies each record. So I dont want the records to be in T2 which are not present in T1.
Just to reiterate - for a value of column "ref" both the tables returns multiple records, and each record has an unique identifying column. Those records identified in T1 only needs to be present in T2.
Thanks in Advance!
If you don't have exact matching between each row of T1 and T2 then your method is good enough.
But if you have some kind of matching try to implement your logic with merge:
MERGE INTO t2 b
USING (
SELECT *
FROM t1
WHERE t1.ref = <value>) t1
ON (t2.<key> = t1.<key>)
WHEN MATCHED THEN
UPDATE SET <t2.values> = <t1.values>
WHEN NOT MATCHED THEN
INSERT (<t2columns>)
VALUES (<t1values>);
I guess you want something like:
update t2
set (a2,c2,e2) = (select a1,c1,e1 from t1 where t1.ref = 5)
where t2.ref = 5;
But you would have to make sure the nested query produces only one row.