Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / April 2006

Tip: Looking for answers? Try searching our database.

Using  SUM and GROUP BY

Thread view: 
Ben - 27 Apr 2006 15:06 GMT
Hi,

I have a table that stores hours, minutes, seconds and quantity.  I can
find the quantity per hour by selecting hours and quantity and group by
hours with a sum for the quantity.
          However if I want minutes I need to multiply the hours by 60
and add them to the minutes and then find the sum of the quantity.  I
have tried the following sql statement.

SELECT Sum((hours*60)+minutes) AS mins, Sum(quantity) AS quant FROM
table1GROUP BY mins;

but keep getting this error

ERROR: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]
Too few parameters. Expected 1.

I've been playing around with it for a while, but get that message each
time.

Could someone point out where I am going wrong?

Thanks.

Ben.
Ben - 27 Apr 2006 15:49 GMT
BTW there is a space between table1 and GROUP
Bjorn Abelli - 27 Apr 2006 16:19 GMT
"Ben" <bs2k1@excite.com> wrote...

> I have a table that stores hours, minutes, seconds and quantity.
> I can find the quantity per hour by selecting hours and quantity
> and group by hours with a sum for the quantity.

I'm not clear about what each row would represent?

Does it possibly represent some elapsed time, which gives a specific
quantity?

Then I don't understand what you think the result of such queries would
give.

e.g. if you have records like this:

1 0 10
1 1 10
1 1 20
2 0 10
2 5 20

The result of the grouping you used above would give

1 40
2 30

Is that what you want?

> However if I want minutes I need to multiply the hours by
> 60 and add them to the minutes and then find the sum of the
[quoted text clipped - 9 lines]
> ERROR: java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]
> Too few parameters. Expected 1.

Well, you can't GROUP by the aggregated function, as the GROUP BY is the
criteria on *how* to aggregate...

And I'm not sure why you're using SUM on the hours and minutes anyway if you
want to group by them. If you group by them, there will only be one row to
SUM anyway...

Here you have to choose what you want the result to be.

Either you want something that sums it all:

SELECT Sum((hours*60)+minutes) AS mins, Sum(quantity) AS quant
FROM table1;

Gives:

427 70

Or you want something that distinguish each "timeframe".

SELECT (hours*60+minutes) AS mins, Sum(quantity) AS quant
FROM table1
GROUP BY (hours*60+minutes);

...given the same records as above, you would get:

60 10
61 30
120 10
125 20

Is that what you want?

// Bjorn A
Ben - 27 Apr 2006 16:41 GMT
Perfect thanks.  You got it with the second.  I had just realised about
the two sums actually giving the total.  Sorry to confuse the
situation.

Thanks!!

Ben.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.