0

I have 2 tables with no primary-foreign key relationship. I want to delete a record(row to be more specific) in Table1 when count of a row in Table2 reaches a certain value.

Table1
ID      Name
1       name1
2       name2

Table2
ID     Name     Country
1      name1     US
2      name2     China
3      name2     Germany
4      name1     KSA

If name1 count in Table2 reaches 40 then delete name1 from Table1

Hope i have explained it all. Thanks in advance

7
  • 2
    What have you tried? Where did you get stuck? Commented Jan 16, 2018 at 8:15
  • Welcome to Stack Overflow! To give you a great answer, it might help us if you have a glance at How to Ask if you haven't already. It might be also useful if you could provide a minimal reproducible example. Commented Jan 16, 2018 at 8:16
  • Can you please provide few rows from the tables and the output you need. Commented Jan 16, 2018 at 8:16
  • 2
    You can look into trigger. Perform the required logic in the INSERT trigger Commented Jan 16, 2018 at 8:17
  • @PawanKumar rows are almost same as in this example. Data type of Name column is nvarchar. Commented Jan 16, 2018 at 8:30

4 Answers 4

1

From what I understood I came up with this solution. I'm not sure how efficient this is but I guess does the trick.

DELETE FROM Table1 where Name IN (select Name from Table2 group by [Name] having count(Name)>=40)

Edit: Here you SQL Fiddle that you can try with small set of data. http://sqlfiddle.com/#!6/9bf70/21

Sign up to request clarification or add additional context in comments.

4 Comments

I have tested your query. Delete part is not working but Select is giving correct results.
It might be SQL Fiddle because I tried it on a local database and it's deleted the results that it supposed to.
may be it works with one table. Could you try with 2 tables as in my scenario?
I think you are closest to the solution.
0

If I understood well you need DELETE with JOIN; In the sub-query you get the count / name and then join to table1 and DELETE where count reaches the limit you want.

DELETE T1
FROM tbl1 T1
INNER JOIN (
    SELECT
        name
        ,COUNT(name) as cnt
    FROM tbl2
    GROUP BY name
)T2
    ON T1.name = T2.name
WHERE cnt >= 40

Edit: I added some sample data here for you test the code.

7 Comments

I tried this solution but i think there is some syntax issue here. DELETE T1 is invalid object. Could you look into this whats wrong DELETE WorkShop.WorkshopName FROM Workshop INNER JOIN ( SELECT workshopname ,COUNT(workshopname) as cnt FROM WorkshopOrders GROUP BY WorkshopName )WorkshopOrders ON Workshop.WorkshopName = WorkshopOrders.WorkshopName WHERE cnt >= 10
You don't need to put the column name before DELETE statement; just leave the table name (or use aliases as in my code);
sorry about the mess..i dont know how to format it here as i'm new on this forum.
I tested with table name and it executed but 0 rows affected. one workshop have 10 orders but query didnt delete in Table1 :(
because condition is to delete over 40?
|
0

Use stored procedure.

Create Procecedure DeleteRecords
(
@recCount int,
@nameField varchar(20)
)
Begin
 If((select count(id) from Table2 where [name]=@nameField) >= @recCount)
begin
 Delete from Table1 where [name]=@nameField
end
End

Comments

0

You create the INSERT trigger on Table2

create trigger ti_Table2 ON Table2 for insert
as
begin

    -- delete rows from table1
    delete  t1
    from
    (
        -- here it get list of name that exceeds 40 rows
        select  i.Name
        from    (
                    -- get the distinct Name 
                    -- inserted contains newly inserted rows
                    select  distinct i.Name
                    from    inserted i
                ) i
                -- join it to Table2 to find no of rows
                inner join Table2 t2    on  i.Name  = t2.Name
        group by i.Name
        having count(*) >= 40    -- when exceed 40
    ) t2
    inner join Table1 t1    on  t2.Name     = t1.Name

end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.