Counting rows by a condition in another table - oracle

Need to count the number of rows in one table which connect to a second table by (name.sample) where in the second table the (name.sample) was created before (or after) a certain date.

select count(*) from table1 t1
inner join table2 t2 on t1.my_foreign_key_column = t2.my_primary_key_column
where t2.creation_date >= 'my_date_literal'

Related

Merge query to distribute equal number of records between 100 users

I have two tables in Oracle, in first table I have 100 users and in second table I have 100000 records. I want to distribute equal amount of records between them.....
Instead of writing updating and using rownum <= 1000 to distribute data....I want to write merge statement that can divide equal number of records between 100 users.
Table 1
column A Column B column c
1 Pre 90008765
2 Pre 90008766 and so on like this
Table 2
column a Column B column C Column d
1 null null null
2 null null null
And so on will have 100000 records
and between these two tables column a will be common in which we can apply join condition..... please guide me with merge query
If I understand correctly these words "write merge statement that can divide equal number of records between 100 users", you want this:
merge into table2 tgt
using (
select tb.rwd, ta.a
from (select rownum rn, a, b, c, count(1) over () cnt from table1) ta
join (select rowid rwd, rownum rn, a, b, c, d from table2) tb
on mod(ta.rn, cnt) = mod(tb.rn, cnt)) src
on (tgt.rowid = src.rwd)
when matched then update set a = src.a
dbfiddle
This statement assigns rows from T1 to rows in T2 in sequence 1-2-3-...-1-2-3-..., using function mod(). Of course you can update other columns if you need, not only A.

compare two tables and delete rows from one table if there are similar coumn values in two tables hive

Table description are in the link
Table 1 and Table 2 has rows with A and D .I need these two removed from Table 2 .
Please check the link below for descriptive detail . Thank you .
You may do an INSERT OVERWRITE using a LEFT JOIN select query.
INSERT overwrite TABLE table2
SELECT t2.*
from table2 t2
LEFT JOIN table1 t1
on (t1.x = t2.p) --use appropriate common column name
WHERE t1.x is NULL; --where there's no common element in t2

Oracle: Joining two table with a common column plus a additional column(latest effective date) from second table to select other column

Joining two table with a common column plus an additional column (latest effective date) from the second table to select another column. I joined the tables with all conditions, but the resulting table has duplicate records as there are multiple records in Table2 for the same identifier, out of which I just need the record with the latest effective date. I am using Oracle.
Table1 (Column A, B, C, D);Table2 (C, Efft_date, X),Table3...
The result should be as below after Joining the tables: A, B, C, X and columns from other tables
Value of X depends on latest/max value of efft_date from Table2.
Other Info: There are other tables that are joined and other conditions in where clause.
Please help to join the tables without duplicates
So you need to further restrict your result set based on the max of table2.efft_date. So you need a clause like:
AND table2.efft_date = ( SELECT MAX( table2b.efft_date)
FROM table2 AS table2b
WHERE table2b.c = table2.c )
This assumes that table2 cannot have duplicate efft_date values.
You need to join to a subquery so that you get just 1 "latest date" for each value of column c. For this I recommend using row_number()
select t1.A, t1.B, t1.C, t1.D, t2.x, t2.efft_date
from table1 t1
inner join (
select c, x, efft_date, row_number() over(partition by c order by efft_date DESC) as rn
from table2
) t2 on t1.c = t2.c and t2.rn = 1
...
Note that by ordering the dates in descending values, the most recent date will be assigned the row number of 1. Hence the join condition and rn = 1 will only allow the most recent dates to be included in the result.
Changing the order to ascending date order would do the revers, just allow the earliest dates.

Oracle 20 million update based on join

I have a need to do the following
UPDATE TABLE2 t2
SET t2.product_id = (select t1.product_id from
table1 t1 where t1.matching_id = t2.matching_id)
Except that TABLE2 has 27 million records. The product_id is a newly added column and hence populating data to it.
I could use a cursor , break down my record set in TABLE2 to a reasonably smaller number, But with 27 million records, I am not sure whats the best way.
Pl suggest, even if it means exporting my data to excel.
Update - THe matching columns are indexed too.
The only thing I could do different is replace the update for a CREATE TABLE AS
CREATE TABLE table2_new AS
SELECT t2.* (less product_id), t1.product_id
FROM table1 t1
JOIN table2 t2
ON t1.matching_id = t2.matching_id
But later you will have to add the CONSTRAINTS manually, delete table2 and replace for table2_new
update (select t1.product_id as old_product_id, t2.product_id as new_product_id
from table1 t1
join table2 t2 on (t1.matching_id = t2.matching_id)) t
set t.new_product_id = t.old_product_id

Oracle: Returning the earliest date

I have a stored procedure which I use to return the earliest available date in a column of dates. I need to only return the earliest, and currently am using date arithmetic to reduce the number of returned rows. However, doing it this way, my procedure gets stuck in a loop of the first two top returned values, meaning I have several rows which are never read. Could somebody please let me know where I need to use the MIN function in the following WHERE clause, please? Thanks:
SELECT **COLS**
INTO **VARS**
FROM **TABLE**
INNER JOIN **TABLE TO JOIN**
ON **JOIN TARGET**
WHERE ROWNUM = 1 AND LASTREADTIME < SYSDATE - (30/86400)
ORDER BY LASTREADTIME DESC;
if you only need the earliest date
SELECT MIN(LastReadTime)
INTO **VARS**
FROM table
if you need other datas
SELECT t2.col1, t1.col1, t1.col2, t1.LastTreadTime
INTO **VARS**
FROM table t1
JOIN table2 t2 on t1.col1 = t2.col1
WHERE t1.LastReadTime = (SELECT MIN(t2.LastReadTime) FROM table t2);

Resources