473,396 Members | 2,021 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,396 software developers and data experts.

I need help with SELECT statement

hello,
At the beginning I would like to describe my very simple table. It is
composed of three columns: 'name', 'forename', 'status'. This database
stores results of games. The values of column 'status' can be only: 1 for
win, 2 for lose,0 for draw. My aim is to get a table which consists also 3
columns, but these should be: 'name', 'forename', 'balance'. Of course
balance should display values: number of wins - number of loses for each
player. So the new table should be the agreggate of the same players which
their balance of games. Is there anyone who could create that statement? I
think that it is not very difficult for someone who knows the SQL. If it is
needed the DBMS which I use is MSSQL Server(MSDE). I can not speak and write
english very well so sorry for mistakes. Thanks and greetings for YOU.
Jul 23 '05 #1
1 1018
The design is wrong. You need to all the attributes (results) into a
single row for each entity (person)

CREATE TABLE Games
(first_name CHAR(20) NOT NULL,
last_name CHAR(20) NOT NULL,
win_cnt INTEGER NOT NULL,
lose_cnt INTEGER NOT NULL,
draw_cnt INTEGER NOT NULL,
PRIMARY KEY (first_name, last_name));

Load the new table with this statement:

INSERT INTO Games
SELECT first_name, last_name,
SUM(CASE WHEN status = 1
THEN 1 ELSE 0 END) AS win,
SUM(CASE WHEN status = 2
THEN 1 ELSE 0 END) AS lose,
SUM(CASE WHEN status = 0
THEN 1 ELSE 0 END) AS draw
FROM Foobar
GROUP BY first_name, last_name;

Now your query is easy.

Jul 23 '05 #2

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

Similar topics

4
by: Radioactive Man | last post by:
anyone know of a function like "raw_input", which collects a string from the user entry, but one where I can set a time limit, as follows: time_limit = 10 # seconds user_answer =...
2
by: JellyMan | last post by:
I have two tables in my Access database that I want to be able to synchronize by pressing a form button. I think it can work, but I am not sure how. I am trying to use a single query to do this. ...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
2
by: clinttoris | last post by:
Hello, If someone could help me it would be appreciated as I am not having much luck. I'm struggling with my asp code and have some questions relating to asp and oracle database. First...
6
by: Twobridge | last post by:
I hope someone can help me out with my problem. I have found a sql statement that basically pulls all bills filed within a certain time period and the payments made on those bills with in the...
0
by: NM | last post by:
Hello, I've got a problem inserting binary objects into the postgres database. I have binary objects (e.g. images or smth else) of any size which I want to insert into the database. Funny is it...
13
by: PinkBishop | last post by:
I am using VS 2005 with a formview control trying to insert a record to my access db. The data is submitted to the main table no problem, but I need to carry the catID to the bridge table...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
2
by: kya2 | last post by:
I am not able to create following store procedure. CREATE PROCEDURE DBSAMBA.InsertDeleteBatch(OUT norows INT ) RESULT SETS 1 LANGUAGE SQL BEGIN part1 DECLARE TOTAL_LEFT INT DEFAULT 0; ...
1
by: contactrajib | last post by:
I have a situation where one of the app server thread is executing an update statement on A record and at the same time another thread is trying to read it by executing a select query. Now we are...
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: 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...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.