Results 1 to 7 of 7

Thread: Simple Difference query

  1. #1

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Simple Difference query

    Hey guys,
    Suppose I have two tables with the same schema, and using one field of each, can I easily get the difference from both tables with a single query?

    Something like
    Code:
    'Table A
    ID  qty
    e4 1
    e5 20
    e6 2
    e7 5
    
    'Table B
    ID  qty
    e4 1
    e5 22
    e3 12
    
    'Producing
    ID  qtyfromA   qtyfromb
    e3    0                12
    e5    20               22
    e6    2                 0
    e7    0                 5
    Where it joins the tables, but only tells me what's different, and what's entirely missing from both sides of the join? I'm not too hot with SQL. I can make it pull the differences where both tables have the same ID number with different qty numbers, but not where either table is missing both..

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Simple Difference query

    Details would be dependant on what db/vb versions you're using. I think you can do what you want in sql, but would have to use Iif or Nz (Access) or similar SQL Server functions.
    For instance, a normal Join query would return either nothing or Null for TableA.e3, depending. You need some way to convert Nothing or Null to 0.
    I could test it out in Access, but there's lots better SQL gurus here than me.
    Tengo mas preguntas que contestas

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Simple Difference query

    In SQL Server you could use a "Derived" table created from a Union query. I don't think Access supports Derived tables.

    Code:
    Select Id, Sum(QtyA) As QtyFromA, Sum(QtyB) as QtyFromB
    From
       (Select Id, Qty As QtyA, 0 As QtyB From TableA
        Union All
        Select Id, 0, Qty From TableB
       ) As DataSet
    Group By Id
    Having Sum(QtyA) <> Sum(QtyB)
    [Edit]
    I keep forgetting about Full Outer Joins.

    Code:
    Select IsNull(TableA.Id,TableB.Id) As ID, IsNull(TableA.Qty,0) As QtyFromA, IsNull(TableB.Qty,0) AS QtyFromB
    From TableA Full Join TableB On TableA.Id = TableB.Id
    Where IsNull(TableA.Qty,0) <> IsNull(TableB.Qty,0)
    Last edited by brucevde; Oct 2nd, 2006 at 04:28 PM.

  4. #4

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Simple Difference query

    I am using Access, unfortunately, which means it won't allow me to use a Full Join. And though it doesn't support derived tables, I can just query a query and get the same result, I suppose. I'll give it a shot, thanks.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  5. #5

    Thread Starter
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: Simple Difference query

    The union all and the sum worked out well. Seems a bit wasteful to have to use two queries and so much "fake" zero data, but this app will never feel it, so thanks, that'll do

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  6. #6
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Simple Difference query

    In Access you also have the Nz() function, which will convert null values to either an empty string (default) or a value, such as 0, that you supply.
    Tengo mas preguntas que contestas

  7. #7
    Junior Member
    Join Date
    Apr 2003
    Posts
    23

    Re: Simple Difference query

    If you have a main ID table such as:

    Main Table
    ID Text
    e1 aaa
    e2 bbb
    e3 ccc
    e4 ddd
    e6 eee
    e7 fff

    You can query the main table for the Id's and left join the other two tables A and B for quantities.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width