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 |
jonathanr
Starting Member
11 Posts |
Posted - 2011-01-26 : 20:25:50
|
Hi,The situation is as follows:We have several vendors that we purchase from, so over a given period we may purchase from a particular vendor multiple times or only once. I need to construct a query that sums up to a total amount for each vendor and then show only the vendors that have totals over a certain amount.For example: There are several vendors (below) and show all vendors that have a total of greater than $2001/1/2009 Joe Blogs and Co. $503/1/2009 Penny Savings supplies $20013/1/2009 Joe Blogs and Co. $5008/1/2009 Penny Savings supplies $3009/1/2009 OTT Inc. $100Result:Joe Blogs and Co. $550Penny Savings supplies $500This is my query so far:select pmt_ref_date,vendor_id, vendor_name, bkg_amt, pmt_status_codefrom dbo.pmtwhere pmt_status_code ='CLR'and pmt_ref_date between x AND yCan anyone point me in the direction as tio how I can achieve what I am after?Thanks,Jonathanr |
|
ZZartin
Starting Member
30 Posts |
Posted - 2011-01-26 : 20:41:56
|
Try using a group by clause to generate a sum on the field you want to get the total from, then you can use a HAVING clause at the end of statement to only include records that meet your total. |
 |
|
jonathanr
Starting Member
11 Posts |
Posted - 2011-01-26 : 20:53:09
|
Hi ZZartin,Thanks, I have come up with this so far:se DBSpymtSELECT vendor_name,SUM(bkg_amt) AS Total FROM dbo.pmtwhere pmt_status_code ='CLR'and pmt_ref_date between '2010/11/01'AND '2011/02/6'GROUP BY vendor_nameorder by Total descThe only thing that I still need to do is the showing of totals over a specific amount. I will chech out the HAVING option..jonathanr |
 |
|
jonathanr
Starting Member
11 Posts |
Posted - 2011-01-26 : 20:55:01
|
Cool - all done :)use DBSpymtSELECT vendor_name,SUM(bkg_amt) AS Total FROM dbo.pmtwhere pmt_status_code ='CLR'and pmt_ref_date between '2010/11/01'AND '2011/02/6'GROUP BY vendor_nameHAVING SUM(bkg_amt)<20000order by Total desc |
 |
|
|
|
|
|
|