473,387 Members | 1,691 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,387 software developers and data experts.

Complex query - need help (where a OR b, SUM() etc)

rey
prizes
+------------+------------+---------+---------+
| l_playerid | s_playerid | l_prize | s_prize |
+------------+------------+---------+---------+
| 1 | 1 | 500 | 350 |
| 2 | 1 | 650 | 245 |
| 1 | 2 | 500 | 65 |
| 2 | 2 | 150 | 85 |
| 3 | 3 | 95 | 125 |
| 5 | 2 | 350 | 205 |
+------------+------------+---------+---------+

players
+----------+----------+
| playerid | name |
+----------+----------+
| 1 | Bob |
| 2 | Jay |
| 3 | Suzie |
| 4 | David |
| 5 | Rick |
| 6 | Jane |
+----------+----------+

I need:

+-------+----------------+----------------+
| name | total_l_prizes | total_s_prizes |
+-------+----------------+----------------+
| Bob | 1000 | 595 |
| Jay | 800 | 355 |
| Suzie | 95 | 125 |
| Rick | 350 | 0 |
+-------+----------------+----------------+

attempt:

SELECT players.name, SUM(prizes.l_prize) AS total_l_prizes,
SUM(prizes.s_prize) AS total_s_prizes FROM prizes, players WHERE
players.playerid = prizes.l_playerid OR players.playerid =
prizes.s_playerid GROUP BY players.playerid

HOWEVER, player gets awarded both l and s prizes for each record even
if he/she only appears as l OR s player for that record.

+-------+----------------+----------------+
| name | total_l_prizes | total_s_prizes |
+-------+----------------+----------------+
| Bob | 1650 | 660 |
| Jay | 1650 | 600 |
| Suzie | 95 | 125 |
| Rick | 350 | 205 |
+-------+----------------+----------------+
Thanks,
Rey
Jul 19 '05 #1
6 1463
rey wrote:
prizes
+------------+------------+---------+---------+
| l_playerid | s_playerid | l_prize | s_prize |
+------------+------------+---------+---------+
| 1 | 1 | 500 | 350 |
| 2 | 1 | 650 | 245 |
| 1 | 2 | 500 | 65 |
| 2 | 2 | 150 | 85 |
| 3 | 3 | 95 | 125 |
| 5 | 2 | 350 | 205 |
+------------+------------+---------+---------+


I tried, but also failed to create such a query. However I'm pretty sure
this would be easier by
a) Making two queries
b) changing table structure for prizes.

Currently prizes seem to contain two information sets for each row. It
would be much more clear if you would have either two tables, one for l
and one for s prize. Or one table where is only 1 id and two prizes,
s_prize and l_prize.

So perhaps something like this:

prizes
+----------+-------+-------+
| playerid |l_prize|s_prize|
+----------+-------+-------+
| 1 | 500 | 0 |
| 2 | 650 | 0 |
| 1 | 500 | 0 |
| 2 | 150 | 0 |
| 3 | 95 | 0 |
| 5 | 350 | 0 |
| 1 | 0 | 350 |
| 1 | 0 | 245 |
| 2 | 0 | 65 |
| 2 | 0 | 85 |
| 3 | 0 | 125 |
| 2 | 0 | 205 |
+----------+-------+-------+
SELECT players.name,
SUM(prizes.l_prize) AS total_l_prizes,
SUM(prizes.s_prize) AS total_s_prizes
FROM prizes, players WHERE
players.playerid = prizes.playerid
GROUP BY players.playerid;
Jul 19 '05 #2
rey wrote:
prizes
+------------+------------+---------+---------+
| l_playerid | s_playerid | l_prize | s_prize |
+------------+------------+---------+---------+
| 1 | 1 | 500 | 350 |
| 2 | 1 | 650 | 245 |
| 1 | 2 | 500 | 65 |
| 2 | 2 | 150 | 85 |
| 3 | 3 | 95 | 125 |
| 5 | 2 | 350 | 205 |
+------------+------------+---------+---------+


I tried, but also failed to create such a query. However I'm pretty sure
this would be easier by
a) Making two queries
b) changing table structure for prizes.

Currently prizes seem to contain two information sets for each row. It
would be much more clear if you would have either two tables, one for l
and one for s prize. Or one table where is only 1 id and two prizes,
s_prize and l_prize.

So perhaps something like this:

prizes
+----------+-------+-------+
| playerid |l_prize|s_prize|
+----------+-------+-------+
| 1 | 500 | 0 |
| 2 | 650 | 0 |
| 1 | 500 | 0 |
| 2 | 150 | 0 |
| 3 | 95 | 0 |
| 5 | 350 | 0 |
| 1 | 0 | 350 |
| 1 | 0 | 245 |
| 2 | 0 | 65 |
| 2 | 0 | 85 |
| 3 | 0 | 125 |
| 2 | 0 | 205 |
+----------+-------+-------+
SELECT players.name,
SUM(prizes.l_prize) AS total_l_prizes,
SUM(prizes.s_prize) AS total_s_prizes
FROM prizes, players WHERE
players.playerid = prizes.playerid
GROUP BY players.playerid;
Jul 19 '05 #3
Rey
Thhank you for your reply!

However, at this time I do not have control over the tables. The tables are
given to me and I must work with them the way they are.
FYI the prizes table looks a little more like this:

prizes
+-------+----------+-----------+-------+-------+
| gameid | l_playerid | s_playerid | l_prize | s_prize |
+-------+----------+-----------+-------+-------+
| 1 | 1 | 1 | 500 | 350 |
| 2 | 2 | 1 | 650 | 245 |
| 3 | 1 | 2 | 500 | 65 |
| 4 | 2 | 2 | 150 | 85 |
| 5 | 3 | 3 | 95 | 125 |
| 6 | 5 | 2 | 350 | 205 |
+-------+---------+------------+-------+-------+

I apologize.

Thanks,
Rey
"Aggro" <sp**********@yahoo.com> wrote in message
news:mv***************@read3.inet.fi...
rey wrote:
prizes
+------------+------------+---------+---------+
| l_playerid | s_playerid | l_prize | s_prize |
+------------+------------+---------+---------+
| 1 | 1 | 500 | 350 |
| 2 | 1 | 650 | 245 |
| 1 | 2 | 500 | 65 |
| 2 | 2 | 150 | 85 |
| 3 | 3 | 95 | 125 |
| 5 | 2 | 350 | 205 |
+------------+------------+---------+---------+


I tried, but also failed to create such a query. However I'm pretty sure
this would be easier by
a) Making two queries
b) changing table structure for prizes.

Currently prizes seem to contain two information sets for each row. It
would be much more clear if you would have either two tables, one for l
and one for s prize. Or one table where is only 1 id and two prizes,
s_prize and l_prize.

So perhaps something like this:

prizes
+----------+-------+-------+
| playerid |l_prize|s_prize|
+----------+-------+-------+
| 1 | 500 | 0 |
| 2 | 650 | 0 |
| 1 | 500 | 0 |
| 2 | 150 | 0 |
| 3 | 95 | 0 |
| 5 | 350 | 0 |
| 1 | 0 | 350 |
| 1 | 0 | 245 |
| 2 | 0 | 65 |
| 2 | 0 | 85 |
| 3 | 0 | 125 |
| 2 | 0 | 205 |
+----------+-------+-------+
SELECT players.name,
SUM(prizes.l_prize) AS total_l_prizes,
SUM(prizes.s_prize) AS total_s_prizes
FROM prizes, players WHERE
players.playerid = prizes.playerid
GROUP BY players.playerid;

Jul 19 '05 #4
Rey
Thhank you for your reply!

However, at this time I do not have control over the tables. The tables are
given to me and I must work with them the way they are.
FYI the prizes table looks a little more like this:

prizes
+-------+----------+-----------+-------+-------+
| gameid | l_playerid | s_playerid | l_prize | s_prize |
+-------+----------+-----------+-------+-------+
| 1 | 1 | 1 | 500 | 350 |
| 2 | 2 | 1 | 650 | 245 |
| 3 | 1 | 2 | 500 | 65 |
| 4 | 2 | 2 | 150 | 85 |
| 5 | 3 | 3 | 95 | 125 |
| 6 | 5 | 2 | 350 | 205 |
+-------+---------+------------+-------+-------+

I apologize.

Thanks,
Rey
"Aggro" <sp**********@yahoo.com> wrote in message
news:mv***************@read3.inet.fi...
rey wrote:
prizes
+------------+------------+---------+---------+
| l_playerid | s_playerid | l_prize | s_prize |
+------------+------------+---------+---------+
| 1 | 1 | 500 | 350 |
| 2 | 1 | 650 | 245 |
| 1 | 2 | 500 | 65 |
| 2 | 2 | 150 | 85 |
| 3 | 3 | 95 | 125 |
| 5 | 2 | 350 | 205 |
+------------+------------+---------+---------+


I tried, but also failed to create such a query. However I'm pretty sure
this would be easier by
a) Making two queries
b) changing table structure for prizes.

Currently prizes seem to contain two information sets for each row. It
would be much more clear if you would have either two tables, one for l
and one for s prize. Or one table where is only 1 id and two prizes,
s_prize and l_prize.

So perhaps something like this:

prizes
+----------+-------+-------+
| playerid |l_prize|s_prize|
+----------+-------+-------+
| 1 | 500 | 0 |
| 2 | 650 | 0 |
| 1 | 500 | 0 |
| 2 | 150 | 0 |
| 3 | 95 | 0 |
| 5 | 350 | 0 |
| 1 | 0 | 350 |
| 1 | 0 | 245 |
| 2 | 0 | 65 |
| 2 | 0 | 85 |
| 3 | 0 | 125 |
| 2 | 0 | 205 |
+----------+-------+-------+
SELECT players.name,
SUM(prizes.l_prize) AS total_l_prizes,
SUM(prizes.s_prize) AS total_s_prizes
FROM prizes, players WHERE
players.playerid = prizes.playerid
GROUP BY players.playerid;

Jul 19 '05 #5
Rey wrote:
However, at this time I do not have control over the tables. The tables are
given to me and I must work with them the way they are.


Ok, would it then be okay two use two queries? (Unless someone else
knows how to do it with one query.)
Jul 19 '05 #6
Rey wrote:
However, at this time I do not have control over the tables. The tables are
given to me and I must work with them the way they are.


Ok, would it then be okay two use two queries? (Unless someone else
knows how to do it with one query.)
Jul 19 '05 #7

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

Similar topics

0
by: awarsd | last post by:
------=_NextPart_000_0007_01C34C8B.2CF5D7A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I'm looking at some search engine where we can have...
0
by: Yoed Anis | last post by:
Hello, What would be the proper way of generating a query like the following; SELECT id FROM sales WHERE SUM( totalsales) <= '20000' GROUP BY id; Assuming the table looks like this; Sales:...
6
by: rey | last post by:
prizes +------------+------------+---------+---------+ | l_playerid | s_playerid | l_prize | s_prize | +------------+------------+---------+---------+ | 1 | 1 | 500 | ...
9
by: Philip Pinkerton | last post by:
When trying to connect to database via the pgAdmin3 GUI it asks for a password. I use the same passworrd as I did when I connect to the DB via command line but I get Ident error? how do I set,...
5
by: binky | last post by:
Question for all you SQL gurus out there. I have a (seemingly) complex query to write, and since I'm just learning SQL server, am not sure how to go about it. Tables: t_trans t_account All...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
2
BeemerBiker
by: BeemerBiker | last post by:
I put together a (what I consider) complex query using the Access wizard. It works fine in access but fails when I code it up. I can actually make it work in code by "simplifying" it, but then it...
3
by: william67 | last post by:
I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated this is the...
9
Jerry Maiapu
by: Jerry Maiapu | last post by:
I have query that return records of two related tables. (One to many) In this query I have about 3 to 4 calculated fields that are based on the fields from the 2 tables. Now I want to have a...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.