This week's book / license giveaways are in the JDBC and Relational Databases and Java in General forums.
We're giving away four copies each of PostGIS in Action and Java Advanced Topics Training and have the authors on-line!
See this thread and this one for details.
The moose likes JDBC and Relational Databases and the fly likes Sum function sql statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Sum function sql statement" Watch "Sum function sql statement" New topic
Author

Sum function sql statement

Loh Peggie
Ranch Hand

Joined: Jul 09, 2009
Posts: 51
Hey guys do you know how to implement the sql statement for the sum unit price in jsp?

This is my code:


But the problem is I do not know how to add in the sum UnitPrice.


Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Aggregate functions just take the field you are interested in as a parameter. So:

notice you will have to add a group by for all fields not in the function for it to work.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Loh Peggie
Ranch Hand

Joined: Jul 09, 2009
Posts: 51
However my table is in t form of inner join.

Currently I am doing a shopping cart and I need to make use of inner join statement to display the details.

The two database that I make use of is called:
shoppingcartitems
Products
I link them together by using the productId

My shoppingcartitems attributes are:
SCId
UserId
ProductId
Quantity


My products attributes are:
ProductId
ProdName
ProdColor
ProdDesc
UnitPrice
Quantity
ProdCatId
UserId
ProdImage


The shopping cart will display ProdImage, UnitPrice, ProductName, quantity and TotalPrice(But i dont know how to write the sql statement that sum the unit price from the database)

Thanks
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The join doesn't make a difference, the logic is still the same. To flesh it out a bit:

Loh Peggie
Ranch Hand

Joined: Jul 09, 2009
Posts: 51
Thanks

Erm may I know is my sql statement correct:



Thanks!
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No, you need to group things not in your aggregate function.
 
Consider Paul's rocket mass heater.
 
subject: Sum function sql statement