|
-
Oct 2nd, 2006, 02:24 PM
#1
Thread Starter
Frenzied Member
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
-
Oct 2nd, 2006, 03:12 PM
#2
Frenzied Member
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
-
Oct 2nd, 2006, 04:14 PM
#3
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.
-
Oct 3rd, 2006, 08:07 AM
#4
Thread Starter
Frenzied Member
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
-
Oct 3rd, 2006, 08:21 AM
#5
Thread Starter
Frenzied Member
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
-
Oct 3rd, 2006, 08:31 AM
#6
Frenzied Member
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
-
Oct 3rd, 2006, 09:05 AM
#7
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|