Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 24th, 2006, 06:41 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default Multiple Row Calculation

Hi All

I have just got my head around simple calculation in SQL.... What I need to acheive now though is to be able to select several rows that using a WHERE clause and then perform a calculation on the selected rows e.g.

SELECT FIELD1 FROM PRODUCTS WHERE PRODUCTTYPE = 1 - Thats the easy bit!

Now I need to add all of the selected rows together.

Is there syntax to do this?

Ta

Rit
__________________
Rit
www.designandonline.co.uk
INSPIRE | CREATE | DELIVER
 
Old February 24th, 2006, 07:06 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

I suggest you look at Aggregate Functions in SQL Books Online.
Something like:
Code:
SELECT SUM(Field1) Field1Total FROM PRODUCTS WHERE PRODUCTTYPE = 1

--

Joe (Microsoft MVP - XML)
 
Old February 24th, 2006, 08:21 AM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Thanks Joe, I Found something exactly on those lines.

Rit
 
Old February 24th, 2006, 12:55 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Please post your solution so we can all learn.

Thanks,
Richard

 
Old February 24th, 2006, 04:17 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

Sure no problem..

SELECT SUM(Field1) FROM PRODUCTS WHERE PRODUCTTYPE = 1 GROUP BY PRODUCTTYPE)

What I needed to do was isolate a particular selection .. in this example the PRODUCTTYPE = 1 so I dropped in a WHERE clause followed by the GROUP BY.

Seemed to work great... if anyone can see a betetr way then we are all up for learning.

Warm Regards

Rit
 
Old March 1st, 2006, 12:41 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Sweet. Thanks for the solution post.

 
Old March 1st, 2006, 12:59 PM
Friend of Wrox
 
Join Date: Oct 2005
Posts: 173
Thanks: 0
Thanked 2 Times in 1 Post
Default

no problem.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Combine Multiple Rows into One Row eusanpe SQL Language 3 November 13th, 2007 01:25 PM
Insert multiple row in server. Somesh .NET Framework 2.0 1 June 1st, 2007 08:03 AM
Consolidating multiple occurences in a single row ssray23 SQL Language 11 May 18th, 2007 11:00 AM
Display multiple records / row in DataGrid angshujit ASP.NET 2.0 Basics 0 January 18th, 2007 07:03 AM
How can i delete multiple row through PHP coding taha_elearner PHP Databases 1 October 28th, 2005 05:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.