473,498 Members | 2,058 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL query suggestions/help

I'm trying to count the number of records in 'game_dates' where the
columns home_team_id or away_team_id have the same value. E.g., i
want to know the number of records for each team_id where team_id is
home_team_id or away_team_id.

I'm doing this in two separate select statements now. Example:

SELECT count(home_team_id),home_team_id FROM games
WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS
NOT NULL) OR (home_score <> 0 OR away_score <> 0))
GROUP BY home_team_id
and
SELECT count(away_team_id),away_team_id FROM games
WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS
NOT NULL) OR (home_score <> 0 OR away_score <> 0))
GROUP BY away_team_id
and then combining the results. Is there anyway to combine these to
queries into one query? ...and have a single result set returned with
two columns (count,team_id)?

Thanks,
Glenn
Jul 20 '05 #1
3 3463
Hi Glenn,

Try this:

select team_id = isnull(home_team_id, away_team_id),
count = isnull(s1,0) + isnull(s2,0)
from (SELECT home_team_id, count(home_team_id) as s1
FROM games
WHERE league_id = 218
and ((home_score IS NOT NULL OR away_score IS NOT NULL)
OR (home_score <> 0 OR away_score <> 0))
GROUP BY home_team_id) as T1
full outer join
(SELECT away_team_id, count(away_team_id) as s2
FROM games
WHERE league_id = 218
and ((home_score IS NOT NULL OR away_score IS NOT NULL)
OR (home_score <> 0 OR away_score <> 0))
GROUP BY away_team_id) as T2
on home_team_id = away_team_id

Shervin

gl***@glenncarr.com (Glenn Carr) wrote in message news:<21**************************@posting.google. com>...
I'm trying to count the number of records in 'game_dates' where the
columns home_team_id or away_team_id have the same value. E.g., i
want to know the number of records for each team_id where team_id is
home_team_id or away_team_id.

I'm doing this in two separate select statements now. Example:

SELECT count(home_team_id),home_team_id FROM games
WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS
NOT NULL) OR (home_score <> 0 OR away_score <> 0))
GROUP BY home_team_id
and
SELECT count(away_team_id),away_team_id FROM games
WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS
NOT NULL) OR (home_score <> 0 OR away_score <> 0))
GROUP BY away_team_id
and then combining the results. Is there anyway to combine these to
queries into one query? ...and have a single result set returned with
two columns (count,team_id)?

Thanks,
Glenn

Jul 20 '05 #2
Oops!

Sorry I forgot the parentheses. Change the last line to this:

on (home_team_id = away_team_id)

Shervin
"Shervin Shapourian" <Sh**********@hotmail.com> wrote in message
news:4e**************************@posting.google.c om...
Hi Glenn,

Try this:

select team_id = isnull(home_team_id, away_team_id),
count = isnull(s1,0) + isnull(s2,0)
from (SELECT home_team_id, count(home_team_id) as s1
FROM games
WHERE league_id = 218
and ((home_score IS NOT NULL OR away_score IS NOT NULL)
OR (home_score <> 0 OR away_score <> 0))
GROUP BY home_team_id) as T1
full outer join
(SELECT away_team_id, count(away_team_id) as s2
FROM games
WHERE league_id = 218
and ((home_score IS NOT NULL OR away_score IS NOT NULL)
OR (home_score <> 0 OR away_score <> 0))
GROUP BY away_team_id) as T2
on home_team_id = away_team_id

Shervin

gl***@glenncarr.com (Glenn Carr) wrote in message

news:<21**************************@posting.google. com>...
I'm trying to count the number of records in 'game_dates' where the
columns home_team_id or away_team_id have the same value. E.g., i
want to know the number of records for each team_id where team_id is
home_team_id or away_team_id.

I'm doing this in two separate select statements now. Example:

SELECT count(home_team_id),home_team_id FROM games
WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS
NOT NULL) OR (home_score <> 0 OR away_score <> 0))
GROUP BY home_team_id
and
SELECT count(away_team_id),away_team_id FROM games
WHERE league_id = 218 and ((home_score IS NOT NULL OR away_score IS
NOT NULL) OR (home_score <> 0 OR away_score <> 0))
GROUP BY away_team_id
and then combining the results. Is there anyway to combine these to
queries into one query? ...and have a single result set returned with
two columns (count,team_id)?

Thanks,
Glenn

Jul 20 '05 #3
Works great! Thanks.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #4

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

Similar topics

4
2370
by: Bacci | last post by:
I have two tables. The first is "Locations" which has 52,000 zip codes w/cooresponding latitudes and longitudes. The second "Suppliers" has 2,000 company names and addresses. The user enters a...
6
3060
by: Xenophobe | last post by:
I know this isn't a MySQL forum, but my question is related to a PHP project. I have two tables. table1 table2 "table1" contains 2 columns, ID and FirstName:
3
3656
by: Patchwork | last post by:
Hi Everyone, Please take a look at the following (simple and fun) program: //////////////////////////////////////////////////////////////////////////// ///////////// // Monster Munch, example...
2
4294
by: Pramod Ramachandran | last post by:
Hi group, I have a query as follows. SELECT * FROM ACS$USG_EVENT, ACS$USG_EVENTDETAIL WHERE ACS$USG_EVENTDETAIL.PARENTCANONICALID = ACS$USG_EVENT.CANONICALID; both the tables are analyzed...
7
1677
by: Jim | last post by:
I need help on a query. There is a common titles database for several radio stations Some titles are enabled and some are not enabled for each station. Example of some tables: Titles ...
3
2721
by: robboll | last post by:
Whenever I query my database using Enterprise Manager, the response time is about a second. When I access the same data via a Cold Fusion webpage, it takes about 15 seconds (or more) to resolve. ...
7
2355
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
4
2010
by: sheree | last post by:
I have 3 tables (amoung a few others) in a small access database. The tables are as follows: == AEReport -------- AEID (PK) RptCatelog GCRCID PatientID EvntDate
2
1366
by: Donnie | last post by:
Hello All I am trying to find some information on how to design/develop a SQL Query tool embedded in an ASP.NET web application What I would like to have is a tool that helps a user write SQL...
8
6463
by: SaltyBoat | last post by:
Needing to import and parse data from a large PDF file into an Access 2002 table: I start by converted the PDF file to a html file. Then I read this html text file, line by line, into a table...
0
7121
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
6993
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
7197
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...
1
6881
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
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.