• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Reply to: Damn SQL Joins

Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "Damn SQL Joins"

Collapse

  • LondonManc
    replied
    Originally posted by original PM View Post
    found out the problem was not the join

    it was the where clause which initially said

    where print flag = 0 and audit type = 2

    needed to add

    or printflag = 0 and audit type = NULL

    but cheers everyone rep on it is way!
    Or just where print flag = 0 and COALESCE(audit type,2) =2

    Leave a comment:


  • GreenMirror
    replied
    Originally posted by original PM View Post
    found out the problem was not the join

    it was the where clause which initially said

    where print flag = 0 and audit type = 2

    needed to add

    or printflag = 0 and audit type = NULL

    but cheers everyone rep on it is way!
    And audit type is null......

    Leave a comment:


  • original PM
    replied
    found out the problem was not the join

    it was the where clause which initially said

    where print flag = 0 and audit type = 2

    needed to add

    or printflag = 0 and audit type = NULL

    but cheers everyone rep on it is way!

    Leave a comment:


  • WTFH
    replied
    Originally posted by mudskipper View Post
    Well that's the most productive I've been so far today
    Likewise!

    Leave a comment:


  • mudskipper
    replied
    Originally posted by WTFH View Post
    too slow - MS is in there already
    Well that's the most productive I've been so far today

    Leave a comment:


  • WTFH
    replied
    too slow - MS is in there already

    Leave a comment:


  • mudskipper
    replied
    Originally posted by original PM View Post
    zero value in first table (invoice not printed)

    there is then going to an audit table - which has multiple rows for each invoice -- however I only want the audit record for the invoice being created

    so currently it is

    select tblinvoice.invoicenumber, tblaudit.auditdate
    from tblinvoice
    Inner Join tblaudit on tblinvoice.invoicenumber = tblaudit.invoicenumber
    where tblinvoice.printflag = 0 and tblaudit.audittypeid = 2

    But generally get 2871 records just looking for invoices with a print flag of 0

    but only 2848 when trying the above - which is correct as the missing 23 invoices do not have a record in the audit table as they were created prior to the audit functionality coming on line

    :sigh: I could pass it to our dev team but it should be simple so killing some time trying to do it myself!
    Inner isn't left. Move your restriction on the audit table to the join, not the where clause

    Code:
    SELECT tblinvoice.invoicenumber, tblaudit.auditdate
    FROM tblinvoice 
    LEFT Join tblaudit ON tblinvoice.invoicenumber = tblaudit.invoicenumber AND tblaudit.audittypeid = 2
    WHERE tblinvoice.printflag = 0
    Last edited by mudskipper; 15 November 2018, 11:32.

    Leave a comment:


  • original PM
    replied
    zero value in first table (invoice not printed)

    there is then going to an audit table - which has multiple rows for each invoice -- however I only want the audit record for the invoice being created

    so currently it is

    select tblinvoice.invoicenumber, tblaudit.auditdate
    from tblinvoice Inner Join tblaudit on tblinvoice.invoicenumber = tblaudit.invoicenumber
    where tblinvoice.printflag = 0 and tblaudit.audittypeid = 2

    But generally get 2871 records just looking for invoices with a print flag of 0

    but only 2848 when trying the above - which is correct as the missing 23 invoices do not have a record in the audit table as they were created prior to the audit functionality coming on line

    :sigh: I could pass it to our dev team but it should be simple so killing some time trying to do it myself!

    Leave a comment:


  • mudskipper
    replied
    Originally posted by original PM View Post
    Damn...

    So this is simple

    I have one table and I want to return all records from one table where a value - zero.

    I then want to join it to another table but whether there is any data in the other table or not I still want the query to return all records from the first table with just null values if there is no data in the second table...

    so thats a left outer join (or left join)

    but apparently not
    It is.

    Originally posted by original PM View Post
    why I am so dumb
    That's probably a thread in its own right

    Originally posted by original PM View Post
    what am I missing?
    Post some sample code, expected results, what you're getting

    Leave a comment:


  • WTFH
    replied
    Originally posted by original PM View Post
    Damn...

    So this is simple

    I have one table and I want to return all records from one table where a value - zero.

    I then want to join it to another table but whether there is any data in the other table or not I still want the query to return all records from the first table with just null values if there is no data in the second table...

    so thats a left outer join (or left join)

    but apparently not

    why I am so dumb and what am I missing?
    Is the value zero or null in the first table?
    Have you a key between them?
    What is the relationship - is it one to one, one to many, many to one?

    Leave a comment:


  • original PM
    started a topic Damn SQL Joins

    Damn SQL Joins

    Damn...

    So this is simple

    I have one table and I want to return all records from one table where a value - zero.

    I then want to join it to another table but whether there is any data in the other table or not I still want the query to return all records from the first table with just null values if there is no data in the second table...

    so thats a left outer join (or left join)

    but apparently not

    why I am so dumb and what am I missing?
Working...
X