SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Having
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

JAdauto
Posting Yak Master

USA
128 Posts

Posted - 09/06/2012 :  16:46:02  Show Profile  Reply with Quote

I would like to compare the Tender Amount value in one table vs the NetSalese + Tax in another table and return those Tender records if there are differences.
What I currently am using is returning many more records then I anticipate. Does the Having Sum(t.Amount) also utilize the Where conditions such as t.[type] = 1) same as the Sum(amount) in the select portion?

Thanks,
JAdauto

Select t.checknumber, t.FKStoreID, t.Dateofbusiness, Sum(t.amount)
from HstGndTender t
where t.storeID = 123
and t.dateofbusiness = '2012-09-05'
and t.[type] = 1
group by t.checknumber, t.dateofbusiness, t.fkstoreId
Having Sum(t.amount) <>
(Select Sum(s.NetSales + s.Tax)
from dpvhstchecksummary s
where s.storeID = t.storeID
and s.dateofbusiness = t.Dateofbusiness
and s.checkid = t.checknumber
group by s.checkid, s.dateofbusiness, s.fkstoreid)

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 09/06/2012 :  19:32:22  Show Profile  Reply with Quote
Is fkstoreId and storeid the same thing? I am assuming that it is, because if that were not the case, the query could fail if the subquery returns more than one value. You might also try this:
SELECT t.checknumber,
       t.FKStoreID,
       t.Dateofbusiness,
       SUM(t.amount)
FROM   HstGndTender t
	   LEFT JOIN dpvhstchecksummary s ON
		s.storeId = t.StoreId
		AND s.dateofbusiness = t.Dateofbusiness
		AND s.checkid = t.checknumber
WHERE  t.storeID = 123
       AND t.dateofbusiness = '2012-09-05'
       AND t.[type] = 1
GROUP BY
       t.checknumber,
       t.dateofbusiness,
       t.fkstoreId
HAVING
       SUM(t.amount) <> SUM(s.NetSales + s.Tax)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000