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 |
|
fparker
Starting Member
27 Posts |
Posted - 2012-01-26 : 10:19:02
|
| I have two tables and I want to count the records by order id and come up with just a list of counts that don't match. Table OrdersOrderid, Branch, Order123, 238, 'some data'123, 238, 'some data'124, 238, 'some data'Table SourceOrderid, Branch, Source123, 238, 'some data'123, 238, 'some data'124, 238, 'some data'124, 238, 'some data'I am able to write the query that gives me all the results but I just want the differences. So my result should beorderid, count of orders, count of source124, 1, 2I am gettingorderid, count of orders, count of source123, 2, 2124, 1, 2TIA--f |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-26 : 10:33:18
|
Can you add a where clause to your existing query as WHERE [Count of Orders] <> [Count of Source] |
 |
|
|
fparker
Starting Member
27 Posts |
Posted - 2012-01-26 : 12:18:41
|
| I am having a problem with syntax on that. I am use Sql Server 2000 I beleive.--f |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-26 : 12:40:41
|
| The syntax I posted should work on SQL 2000. If you post your query, someone on the forum should be able to suggest what might be wrong. |
 |
|
|
fparker
Starting Member
27 Posts |
Posted - 2012-01-26 : 12:56:18
|
| here you goThis will return all results. I just want the ones that don't match. declare @branch as varchar(5)/***************************************/ set @branch = 123 selectorderid,(select count(*) from order with (nolock) where orderid = x.orderid and branchnum = @branch ) as ordered,( select count(*) from sourced with (nolock) where lineid in ( select lineid from line where orderid in (select orderid from order with (nolock) where orderid = x.orderid and branchnum = @branch )) ) as sourcedfrom order x with (nolock) where branchnum = @branch group by orderid results look like this:orderid ordered sourced595605 1 1593517 1 1591429 1 1595252 1 1594208 2 2596296 1 1589679 3 3--f |
 |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-01-26 : 13:12:44
|
You can add the where clause by making your current query into a subquery like this:declare @branch as varchar(5)/***************************************/set @branch = 123 SELECT * FROM (selectorderid,(select count(*) from order with (nolock) where orderid = x.orderid and branchnum = @branch ) as ordered,( select count(*) from sourced with (nolock) where lineid in (select lineid from line where orderid in (select orderid from order with (nolock) where orderid = x.orderid and branchnum = @branch )) ) as sourcedfrom order x with (nolock) where branchnum = @branchgroup by orderid) SWHERE ordered <> sourced I did not quite understand the subqueries where you are doing the "select lineid" - it was not in the original table structure that you posted. Nonetheless, would something simpler such as the following work for you?SELECT COALESCE(o.OrderId,s.OrderId) AS OrderId, o.OrderCount, s.SourceCountFROM( SELECT OrderId, COUNT(*) AS OrderCount FROM [Orders] GROUP BY OrderId ) oFULL JOIN( SELECT OrderId, COUNT(*) AS SourceCount FROM [Source] GROUP BY OrderId) s ON s.OrderId = o.OrderIdWHERE o.OrderCount <> s.SourceCount |
 |
|
|
|
|
|
|
|