473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Hello, Trying to sum() two query's.

Hi all, I'm trying to setup a database (mysql 4.0) for my snooker club
and have run into a little problem, being new to myslq and php. I want
to setup a table where all the results will go, then query this table to
get players averages etc. So will look something like this.

| score | h_playerid | a_playerid | h_score | a_score |
+-------+------------+------------+---------+---------+
| 1 | 1 | 2 | 20 | 15 |
| 2 | 1 | 2 | 35 | 25 |
| 3 | 2 | 1 | 5 | 25 |
| 4 | 2 | 1 | 55 | 25 |

as you can see player play home and away tried to use the following query,

mysql> (select h_playerid, sum(h_score), sum(a_score) from games group
by h_playerid)
-> union all
-> (select a_playerid, sum(a_score), sum(h_score) from games group
by a_playerid)
-> order by h_playerid;

which gives me a result of:

+------------+--------------+--------------+
| h_playerid | sum(h_score) | sum(a_score) |
+------------+--------------+--------------+
| 1 | 55 | 40 |
| 1 | 50 | 60 |
| 2 | 60 | 50 |
| 2 | 40 | 55 |
+------------+--------------+--------------+

half way there now how do I add the home and away entries together? have
tried a GROUP BY at the end no joy, sysntax error;

ERROR 1064: You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'group by h_playerid' at line 4

Thanks to all who could send me in the right direction.


Jul 20 '05 #1
1 1563
HpO
Howdy,

if you're wanting the average (of all games, home and away) for each player,
here's a way to do it in a single query:

mysql> select
-> g1.h_player as player ,
-> (
-> sum(g1.h_score) +
-> (
-> select
-> sum(a_score)
-> from games
-> where a_player = g1.h_player
-> )
-> ) /
-> (
-> count(g1.game) +
-> (
-> select
-> count(game)
-> from games
-> where a_player = g1.h_player
-> )
-> ) as avg_of_scores
-> from games g1
-> group by g1.h_player;
+--------+---------------+
| player | avg_of_scores |
+--------+---------------+
| 1 | 26.25 |
| 2 | 25.00 |
+--------+---------------+
2 rows in set (0.01 sec)

Hope this helps...

"mbza" <mb**@blueyonder.co.uk> wrote in message
news:nu*******************@fe1.news.blueyonder.co. uk...
Hi all, I'm trying to setup a database (mysql 4.0) for my snooker club and
have run into a little problem, being new to myslq and php. I want to
setup a table where all the results will go, then query this table to get
players averages etc. So will look something like this.

| score | h_playerid | a_playerid | h_score | a_score |
+-------+------------+------------+---------+---------+
| 1 | 1 | 2 | 20 | 15 |
| 2 | 1 | 2 | 35 | 25 |
| 3 | 2 | 1 | 5 | 25 |
| 4 | 2 | 1 | 55 | 25 |

as you can see player play home and away tried to use the following query,

mysql> (select h_playerid, sum(h_score), sum(a_score) from games group by
h_playerid)
-> union all
-> (select a_playerid, sum(a_score), sum(h_score) from games group by
a_playerid)
-> order by h_playerid;

which gives me a result of:

+------------+--------------+--------------+
| h_playerid | sum(h_score) | sum(a_score) |
+------------+--------------+--------------+
| 1 | 55 | 40 |
| 1 | 50 | 60 |
| 2 | 60 | 50 |
| 2 | 40 | 55 |
+------------+--------------+--------------+

half way there now how do I add the home and away entries together? have
tried a GROUP BY at the end no joy, sysntax error;

ERROR 1064: You have an error in your SQL syntax. Check the manual that
corresponds to your MySQL server version for the right syntax to use near
'group by h_playerid' at line 4

Thanks to all who could send me in the right direction.


Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Chris | last post by:
I have a SELECT * query that returns info such as 'name', 'date', 'cost', and 'notes'. At the head of this query return it reads how many records were found. I would like to append that with 'how...
15
by: U N Me | last post by:
I have a continuous form that lists records from a query. One of the columns is an amount field, In the footer of the form I place a text box that displays the total (sum) of the amount. The...
1
by: Access | last post by:
I made a query with some totals. One of these totals I only get when using a subquery. I believe it must be possible to get the resulkt in one query ArtNr Total #order #customer...
4
by: Richard Hollenbeck | last post by:
The following query takes about one second to execute with less than 1,000 records, but the report that's based on it takes from 15-30 seconds to format and display. That's frustrating for both me...
4
by: cefrancke | last post by:
Are there any ways to speed up a Cumulative sum in a query or perhaps another faster way to have a cumulative sum column (call a vba function?). For example, I want to sum up all values under...
3
by: Joost | last post by:
I have a form based on a totals query, with one of the fields as the sum of the quantity of an article of the grouped records. I get an #error# in a control on the form where I want to display the...
14
by: deercreek | last post by:
Could use some help. I don't know if I can even do whatI'm trying to do. I have a text box in a report under the details section. It will return a value for each line on a sepcific record set....
0
by: InBoston | last post by:
This is the query. I am trying to sum up the amt allowed. What is happening rite now is it stays separate. Any help would be appreciated! SELECT SUM(AMT_ALLOWED) allowed, SUM(UNITS_NOT_DENIED)...
3
by: Mo | last post by:
I thought that this was going to be super easy (and it probably is), but I couldn't figure it out. I have results from a MySQL query which include PartNumber, Qty, Cost, and Price. In one part of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.