IF's and INNER JOINS

  • The following query is supposed to increment a count everytime the condition is true..im just not sure about the syntax and the order of the statements etc........Basically, i want to read through the table and everytime the condition is met I want to increment the count..........Can an inner join beused in conjunction with an IF statement, if not are there any other ways of doing this? Thanks guys

    declare @count int

    select @count = 0

    select t2.cust_id, t1.cust_id from unique_cust t1 INNER JOIN unique_cust t2

    if (t1.dup_code = t2.dup_code)

    begin

    select @count = @count+1

    end

    If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!


    If riding in a plane is flying, then riding in a boat must be swimming. To experience the element, get out of the vehicle. Skydive!

  • You need an ON clause for your JOIN, plus, you don't need to increment a counter...

    
    
    DECLARE @counter INT
    SELECT COUNT(*) = @counter
    FROM unique_cust t1
    INNER JOIN unique_cust t2 ON t1.key_field = t2.key_field
    WHERE t1.dup_code = t2.dup_code
  • Just a small thingy in the query of jpipes. You need to switch the Count(*) = @counter to @counter = count(*)

  • Thanks, NPeeters!!

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply