Number of rows updated in a oracle table - database

I have a table called t1 which is already updated by a file. I have table t2 which is created as backup for table t1 before modifications. Now I want to know how many records got updated in table t1. Is there anyway that I can do join with back up table and know how many records got altered? Or how to use sql%rowcount function on a already updated table? Or how should i proceed with ALL_TAB_MODIFICATIONS?

You can join the tables on their primary key (cos you didn't update that, hopefully!) and then compare every column.. you'll have to check for nulls too, and it'll make quite a lot of typing. You could use all_tab_cols and a bit of sql to create your query though (write an sql that creates sql as its output )
Actually, thinking about it, you might be able to get away with less typing by doing a natural join the tables together to get a set of rows that didn't change and removing that set from the original full set:
select * from original
Minus
select original.* from original natural inner join backup
Ive never done it, but the theory is that natural join joins on all equal column names so every column of each table will feature in the join condition. It's an inner join so only columns that have not changed will be represented. Any columns that have become null or become valued from null will also disappear. This is hence the set of rows that have not changed. If all you're after is a count, do a count of the original table less the count of this join result. If you want to know which rows changed, do the result set minus.
Ideally you shouldn't do this; instead at the point the update is run, capture the number of rows it affected. However, this technique could be used long after the update was performed (but before some other update was run)

Related

Multiple SQL Joins on Two Tables

I need to create 1 table based on matching the records of two tables, lets call them table p and table c, in SQL Server. The manual way to do this in Excel is to match the Order Column in table p with the Order Column in table c. Once those are matched, you take the corresponding Batch Column in table c and match it with the Batch Column in table p. Once those are matched you then take that corresponding Order Column in table p and match it with table c again and thats the final item we want to pull. Any ideas?
It's hard to say for sure given the lack of table definitions, but it sounds as though you need to do two joins, once over to table c, then back again to table p, which you would alias in order to pull the right column value. Perhaps this will help you get started.
SELECT p1.Order
FROM p
INNER JOIN C
ON p.Order = c.Order
INNER JOIN p as p1
ON p1.brance = c.branch

Can I inner join a previous SQL query then perform counts

I've created a query from a fairly large database.
At the moment each single procedure undertaken by an employee appears as 3 identical timed rows Each row informs the site where procedure occurred, equipment part used and whether day or night.
I want to combine the rows with matching name and time together to create a single row containing all the other fields.
I then want to be able to create a log for the employee to show how many of each procedure has been undertaken and the site and technique used.
I figure an inner join may be the best way to do this but would be grateful for further help as to how to set this up on a sub-query.
Current query:
SELECT procedure, employee, chart_time, form,
FROM cust.records
WHERE employeeID IN () AND procedurelabel LIKE 'rad1'
Really appreciate the help
Your question could use a little clarification... not least being the DDL for your table and a few notes on what each column is. Nonetheless, assuming these rows are all in the cust.records table and that each set of 3 rows have a unique combination of name and time, you could do something like this...
SELECT -- first select fields common to all rows... may as well take these from the first table
records1.procedure, records1.employee, records1.chart_time,
-- ... then select records from your joins
records2.some_column,
records3.some_column
FROM cust.records records1
INNER JOIN WHERE cust.records records2 on records1.chart_time = records2.chart_time
and records1.procedure = record2.procedure
and records1.employee = records2.employee
-- Possible condition required here to not join this row to itself
-- or to explicitly join to a specific type of row
INNER JOIN WHERE cust.records records3 on records1.chart_time = records3.chart_time
and records1.procedure = record3.procedure
and records1.employee = records3.employee
-- Possible condition required here to not join this row to itself
-- or to explicitly join to a specific type of row
WHERE employeeID IN ()
AND procedurelabel LIKE 'rad1'
-- Possible condition required here specify the row to select for records1.
Might also be worth considering a table re-design since what you've described doesn't sound normalised.

Need to avoid repetition results in sql table

I have written a SQL command to get the different entries from two tables. But table 3 is not a steady one it fills one by one (It has a dynamic nature because it is filled by a RFID reader). So the difference table (table2) has multiple entries of same. Please help to avoid this adding same entries.
INSERT INTO table2 (EPC)
SELECT PL.EPC
FROM priorityLevel PL
WHERE NOT EXISTS (
SELECT 1
FROM table3 T3
WHERE PL.EPC = T3.EPC
);
I expect not to repeat same entry.

netezza left outer join query performance

I have a question related to Netezza query performance .I have 2 tables Table A and Table B and Table B is the sub set of Table A with data alteration .I need to update those new values to table A from table B
We can have 2 approaches here
1) Left outer join and select relevant columns and insert in target table
2) Insert table a data into target table and update those values from tableB using join
I tried both and logically both are same.But Explain plan is giving different cost
for normal select
a)Sub-query Scan table "TM2" (cost=0.1..1480374.0 rows=8 width=4864 conf=100)
update
b)Hash Join (cost=356.5..424.5 rows=2158 width=27308 conf=21)
for left outer join
Sub-query Scan table "TM2" (cost=51.0..101474.8 rows=10000000 width=4864 conf=100)
From this I feel left outer join is better .Can anyone put some thought on this and guide
Thanks
The reason that the cost of insert into table_c select ... from table_a; update table_c set ... from table_b; is higher is because you're inserting, deleting, then inserting. Updates in Netezza mark the records to be updated as deleted, then inserts new rows with the updated values. Once the data is written to an extent, it's never (to my knowledge) altered.
With insert into table_c select ... from table_a join table_b using (...); you're only inserting once, thereby only updating all the zone maps once. The cost will be noticeably lower.
Netezza does an excellent job of keeping you away from the disk on reads, but it will write to the disk as often as you tell it to. In the case of updates, seemingly more so. Try to only write as often as is necessary to gain benefits of new distributions and co-located joins. Any more than that, and you're just using excess commit actions.

T-SQL Update Statement WHERE clause with table alias

Right I'm getting myself confused so just want to clarify, let's say I want to do the following update inside a trigger. The trigger is on a table called tblCustomers:
UPDATE tblContacts
SET tblContacts.ContactName = I.ContactName
FROM tblContacts cont
INNER JOIN inserted I ON cont.customerID = I.customerID
Will that only update the records from the inner join between inserted and tblContacts, i.e. in most cases just one record, or do I need to include a WHERE clause?
Also, am I OK to use the alias (cont) in the where clause or do I need to use tblContacts?
Sorry I know this is silly but I'm getting myself more and more confused! Thanks!
Will that only update the records from the inner join between inserted and tblContacts, i.e. in most cases just one record
Yes. That can be one or multiple rows - depending on what your statement that causes the trigger to fire actually did - but it will only ever update those rows that are part of the Inserted table
Also, am I OK to use the alias (cont) in the where clause or do I need to use tblContacts?
You can definitely use the table alias! In fact, when you define a table alias, you have to use it.
Your update query will update all tblContacts.ContactName values wherever there is a match per ON clause defined ON cont.customerID = I.customerID. So if 10 rows matches then 10 values (10 rows) will be affected.
Again, yes you can use the table alias in WHERE condition.

Resources