Delete records from 2 tables with 1 query - sql

I need to delete records from 2 different tables that are linked via job#
One table contains dates for completion and I need to delete all records that were completed between 1999 and 2001. In the second table I need to delete all phases of the job where Job number from table 1 match job number in table 2
I've researched a bit and came up with something like this but when running it I receive "Too Many Fields Selected"
DELETE a.,b.
FROM PUB_jc_job a
LEFT JOIN PUB_jc_phase b
ON b.jph_job = a.job_num
WHERE PUB_jc_job.job_compdate BETWEEN #3/31/1999# AND #12/31/2001#

Please remove dot(.) from table alias "a., b." and retry.

DELETE a, b FROM PUB_jc_job a INNER JOIN PUB_jc_phase b ON b.jph_job = a.job_num WHERE PUB_jc_job.job_compdate BETWEEN #3/31/1999# AND #12/31/2001#

Related

Get the "most" optimal row in a JOIN

Problem
I have a situation in which I have two tables in which I would like the entries from table 2 (lets call it table_2) to be matched up with the entries in table 1 (table_1) such that there are no duplicates rows of table_2 used in the match up.
Discussion
Specifically, in this case there are datetime stamps in each table (field is utcdatetime). For each row in table_1, I want to find the row in table_2 in which has the closed utcdatetime to the table 1 utcdatetime such that the table2.utcdatetime is older than the table_1 utcdatetime and within 30 minutes of the table 1 utcdatetime. Here is the catch, I do not want any repeats. If a row in table 2 gets gobbled up in a match on an earlier row in table 1, then I do not want it considered for a match later.
This has currently been implemented in a Python routine, but it is slow to iterate over all of the rows in table 1 as it is large. I thought I was there with a single SQL statement, but I found that my current SQL results in duplicate table 2 rows in the output data.
I would recommend using a nested select to get whatever results you're looking for.
For instance:
select *
from person p
where p.name_first = 'SCCJS'
and not exists (select 'x' from person p2 where p2.person_id != p.person_id
and p.name_first = 'SCCJS' and p.name_last = 'SC')

Do I have to make sure my record only match with one row when using JOIN with DELETE statement?

I have a query like this:
DELETE A
FROM A
LEFT JOIN B ON
A.ID=B.ID
WHERE B.Status='OK'
The records from these join might resulted in more than one rows, for example:
Table A
ID
1
2
Table B
ID Status
1 OK
1 OK
2 OK
Do I have to make sure my record only match with one row? Because in these example, ID 1 will have 2 rows.
Apologize for bad english.
In SQL Server you don't have to ensure a 1 row result per row to delete. The engine will delete all rows from the deleting table that matches your joining or where conditions, even if they are being selected for more than 1 row.
The important part is which table are your deleting, make sure not to delete the wrong one!

Spotfire Join to unmatched records

I have a table that I am trying to match against itself and create a record when the primary key does not match. Here is an example of the table
Location Value
A 5
B 10
C 15
I want to obtain the following table
Location Value Location(2) Value(2)
A 5 B 10
A 5 C 15
B 10 A 5
B 10 C 15
C 15 A 5
C 15 B 10
I have duplicated the first table and tried the various joins but I cannot get the result. Could anyone provide a suggestion on how this can be performed?
Looks like you want the vector product i.e. outer join. But to have a join you need a column with rows matching. A trick here would be to create a new column with just a single value like "1" for all of the rows (use "Insert Calculated Column" - be sure to freeze the column so you can join to it later). Then do a full outer join of that table with a copy of itself (use the "Insert Columns" feature for the join) using the column with that dummy column as the key field. You will then get the combinations you showed above, but it will also have rows where the keys matched.
To remove the matches, you can easily create a new column with an expression testing if the primary keys match like:
if([Location]=[Location(2)],"Match","NoMatch")
Then filter to the matching rows and delete if you don't want them in the data set.
You can certainly ask Spotfire questions here, but you can also try the Spotfire section of the TIBCO Community here:
https://community.tibco.com/products/spotfire

SQL - include results you are looking for in a column and set all other values to null

I have two tables, one with orders and another with order comments. I want to join these two tables. They are joined on a column "EID" which exists in both tables. I want all orders. I also want to see all comments with only certain criteria AND all other comments should be set to null. How do I go about this?
Orders Table
Order_Number
1
2
3
4
Comments Table
Comments
Cancelled On
Ordered On
Cancelled On
Cancelled On
In this example I would like to see for my results:
Order_Number | Comments
1 | Cancelled On
2 | Null
3 | Cancelled On
4 | Cancelled On
Thanks!
This seems like a rather trivial left join.
select o.order_number, c.comments
from orders o
left join comments c
on o.eid = c.eid
and (here goes your criteria for comments)
Tested on Oracle, there might be subtle syntax differences for other DB engines.
It depends on one condition:
Are you trying to SET the other comments to null? (replace the values in the table)
or
Are you trying to DISPLAY the other comments as null? (dont display them)
If you want to change the values in the table use
UPDATE `table` SET `column` = null WHERE condition;
otherwise use:
SELECT column FROM table JOIN othertable WHERE condition;

Delete rows present in other table

I have two tables A and B containing many columns, two of which in use are SKU and Typedesc.
I created a table C by joining A and B on sku and typedesc
create table C as
select A.*
from A inner join
B
on A.sku=B.sku and trim(A.typedesc)=trim(B.typedesc)
C has approx. 130,000 records
Now I want to delete the rows in A which are present in C
delete from A A1
where exists (select 1
from C c1
where A1.sku=c1.sku and trim(A1.typedesc)=trim(c1.typedesc)
)
It says 145,000 rows deleted.
Where did the extra 15,000 rows came from? Is there something wrong with my delete query? So when I join two tables, C should also have 145,000 but instead it has only 130,000!! why is this so? A or B does not contain any primary key.
Even if i delete B from A directly, the number of rows deleted remains 145,000.
delete from A A1
where exists (select 1
from B b1
where A1.sku=b1.sku and trim(A1.typedesc)=trim(b1.typedesc)
)
The EXISTS looks alright, but without seeing which rows are being deleted it's hard to tell what the problem is, and I don't want to review 145,000 rows any more than you do :)
Try this alternative and see if it makes any difference:
DELETE FROM A
WHERE (SKU, TRIM(TypeDesc)) IN (SELECT SKU, TRIM(TypeDesc) FROM B)