Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Otacustes
Starting Member
15 Posts |
Posted - 2008-08-29 : 07:18:31
|
| Hello allI have two tables set out like this:TableADesc QtyABC 1DEF 2GHI 3ABC 10TableBDescRST 1UVW 2XYZ 3ABC 5I need to know what the difference is between the tables. The qty of ABC in tableA is 11 and in tableB is 5; as tableB is the most upto date I need to know the difference is -6There is every possibility that tableA and tableB could have duplicate desc entries for which the qty values need to be added and then compared to find the difference from tableBWhat is the easiest way to achieve this?ThanksAny help or guidance offered is greatly appreciated. |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-29 : 07:35:50
|
[code]DECLARE @TableA TABLE ([Desc] CHAR(3), Qty INT)INSERT @TableASELECT 'ABC', 1 UNION ALLSELECT 'DEF', 2 UNION ALLSELECT 'GHI', 3 UNION ALLSELECT 'ABC', 10DECLARE @TableB TABLE ([Desc] CHAR(3), Qty INT)INSERT @TableBSELECT 'RST', 1 UNION ALLSELECT 'UVW', 2 UNION ALLSELECT 'XYZ', 3 UNION ALLSELECT 'ABC', 5SELECT [Desc], SUM(aQty) AS aQty, SUM(bQty) AS bQty, SUM(bQty - aQty) AS [Diff]FROM ( SELECT [Desc], SUM(Qty) AS aQty, 0 AS bQty FROM @TableA GROUP BY [Desc] UNION ALL SELECT [Desc], 0 AS aQty, SUM(Qty) AS bQty FROM @TableB GROUP BY [Desc] ) AS dGROUP BY [Desc]ORDER BY [Desc][/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Otacustes
Starting Member
15 Posts |
Posted - 2008-08-29 : 08:57:24
|
Hi PesoI should have said that I was developing this for Access so I am trying to convert it over Anyway, I have converted it to this:SELECT [Desc], Sum(aQty) AS aQty, Sum(bQty) AS bQty, Sum(bQty-aQty) AS DiffFROM (SELECT[Desc],SUM(Qty) AS aQty,0 AS bQty FROM A GROUP BY[Desc] UNION ALL SELECT[Desc],0 AS aQty,SUM(Qty) AS bQty FROM B GROUP BY[Desc]) AS dGROUP BY [Desc]ORDER BY [Desc]; ...but when I run it I get an error saying "Circular reference caused by alias 'aQty' in query definitions SELECT list"I suspect the same is true of bQty as well? I suspect I have made a cock-up somewhere but cannot quite put my finger on it Any help or guidance offered is greatly appreciated. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-29 : 08:59:59
|
[code]SELECT [Desc], Sum(mQty) AS aQty, Sum(nQty) AS bQty, Sum(nQty - mQty) AS DiffFROM ( SELECT [Desc], SUM(Qty) AS mQty, 0 AS nQty FROM A GROUP BY [Desc] UNION ALL SELECT [Desc], 0 AS mQty, SUM(Qty) AS nQty FROM B GROUP BY [Desc] ) AS dGROUP BY [Desc]ORDER BY [Desc][/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
Otacustes
Starting Member
15 Posts |
Posted - 2008-08-29 : 09:18:35
|
Ah-ha!! I see now Thanks very much for your help, Peso |
 |
|
|
Otacustes
Starting Member
15 Posts |
Posted - 2008-08-29 : 11:46:35
|
I now need to get two different versions of this so that one shows all of the additions in tableB and the other to show all the deletions with the correct qtysThe code supplied by Peso works great but I also need these two in separate queries.I have been playing around with this to try and adapt it but cannot seem to get the right combination.Is it as simple as a where clause? ThanksAny help or guidance offered is greatly appreciated. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-29 : 12:51:19
|
quote: Originally posted by Otacustes I now need to get two different versions of this so that one shows all of the additions in tableB and the other to show all the deletions with the correct qtysThe code supplied by Peso works great but I also need these two in separate queries.I have been playing around with this to try and adapt it but cannot seem to get the right combination.Is it as simple as a where clause? ThanksAny help or guidance offered is greatly appreciated.
by additions do you meant ones with diff >0 and deletions as diff<0then what you want is simply an extra HAVING clausefor additions:-SELECT [Desc], Sum(mQty) AS aQty, Sum(nQty) AS bQty, Sum(nQty - mQty) AS DiffFROM ( SELECT [Desc], SUM(Qty) AS mQty, 0 AS nQty FROM A GROUP BY [Desc] UNION ALL SELECT [Desc], 0 AS mQty, SUM(Qty) AS nQty FROM B GROUP BY [Desc] ) AS dGROUP BY [Desc]HAVING Sum(nQty - mQty)>0ORDER BY [Desc] and for deletions:-SELECT [Desc], Sum(mQty) AS aQty, Sum(nQty) AS bQty, Sum(nQty - mQty) AS DiffFROM ( SELECT [Desc], SUM(Qty) AS mQty, 0 AS nQty FROM A GROUP BY [Desc] UNION ALL SELECT [Desc], 0 AS mQty, SUM(Qty) AS nQty FROM B GROUP BY [Desc] ) AS dGROUP BY [Desc]HAVING Sum(nQty - mQty)<0ORDER BY [Desc] |
 |
|
|
Otacustes
Starting Member
15 Posts |
Posted - 2008-08-30 : 02:16:43
|
Oh!!...as simple as that no wonder I couldn't get it, I was messing around in completely the wrong area.Thanks for your help all who have posted. |
 |
|
|
|
|
|
|
|