473,791 Members | 2,949 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tough Select Query

Dan
I am trying to create a query (in either sql or the design view) to
determine which two (or more I suppose if it's not too complicated)
baseball players were teammates the longest. The database includes the
following fields: YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the number of
years players would have played together on a team. (Technical note:
some players have records for more than one team for a given year
because they may have been traded during the season; for the purpose of
this analysis, I will assume that if two players each have a record for
a partiucular team for a particular year, they were teammates).

Thanks in advance for any help on this query.

--Dan L.

Jul 23 '06 #1
7 2253
DFS
Dan wrote:
I am trying to create a query (in either sql or the design view) to
determine which two (or more I suppose if it's not too complicated)
baseball players were teammates the longest. The database includes
the following fields: YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the number of
years players would have played together on a team. (Technical note:
some players have records for more than one team for a given year
because they may have been traded during the season; for the purpose
of this analysis, I will assume that if two players each have a
record for a partiucular team for a particular year, they were
teammates).

Thanks in advance for any help on this query.

This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team, Count(A.Year) AS
[Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;
Jul 23 '06 #2
Dan

DFS,

Thanks for the ingenious solution.

--Dan L.
>

This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team, Count(A.Year) AS
[Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;
Jul 24 '06 #3
Dan
As I have tested and experimetned withthis query I realized it only
works for players who are teamates on one team. It doesn' t pick up
guys who may have been teammates on more than one team. For example,
Pete Rose and Tony Perez were teammates for 16 years on the Reds but
also 1 year on the Phillies. Unfortunately, I am not good enough with
sql to make the necessary adjustment ot the query. Any additional
thoughts would be apreciated.

--Dan L.
DFS wrote:
Dan wrote:
I am trying to create a query (in either sql or the design view) to
determine which two (or more I suppose if it's not too complicated)
baseball players were teammates the longest. The database includes
the following fields: YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the number of
years players would have played together on a team. (Technical note:
some players have records for more than one team for a given year
because they may have been traded during the season; for the purpose
of this analysis, I will assume that if two players each have a
record for a partiucular team for a particular year, they were
teammates).

Thanks in advance for any help on this query.


This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team, Count(A.Year) AS
[Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;
Jul 25 '06 #4
DFS
Dan wrote:
As I have tested and experimetned withthis query I realized it only
works for players who are teamates on one team. It doesn' t pick up
guys who may have been teammates on more than one team. For example,
Pete Rose and Tony Perez were teammates for 16 years on the Reds but
also 1 year on the Phillies. Unfortunately, I am not good enough with
sql to make the necessary adjustment ot the query. Any additional
thoughts would be apreciated.

Dan,

I think the query does pick up the same teammates across different teams.
Please check your data again. Post your exact table structures, with some
sample data, and the SQL of the query you're actually running.
Using my original query:

Sample Data
-----------------------------
Player, Team, Year
John Smoltz, Team1, 1990
John Smoltz, Team1, 1991
Pete Rose, Team1, 1990
Pete Rose, Team1, 1991
Pete Rose, Team1, 1992
Pete Rose, Team2, 1993
Tony Perez, Team1, 1990
Tony Perez, Team2, 1993
Query Results
-----------------------------
Player1, Player2, Team, Teammate Years
John Smoltz, Pete Rose, Team1, 2
John Smoltz, Tony Perez, Team1, 1
Pete Rose, Tony Perez, Team1, 1
Pete Rose, Tony Perez, Team2, 1


--Dan L.
DFS wrote:
>Dan wrote:
>>I am trying to create a query (in either sql or the design view) to
determine which two (or more I suppose if it's not too complicated)
baseball players were teammates the longest. The database includes
the following fields: YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the number of
years players would have played together on a team. (Technical note:
some players have records for more than one team for a given year
because they may have been traded during the season; for the purpose
of this analysis, I will assume that if two players each have a
record for a partiucular team for a particular year, they were
teammates).

Thanks in advance for any help on this query.


This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team,
Count(A.Year ) AS [Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;

Jul 25 '06 #5
"Dan" <sp******@hotma il.comwrote in
news:11******** **************@ m79g2000cwm.goo glegroups.com:
As I have tested and experimetned withthis query I realized it
only works for players who are teamates on one team. It
doesn' t pick up guys who may have been teammates on more than
one team. For example, Pete Rose and Tony Perez were
teammates for 16 years on the Reds but also 1 year on the
Phillies. Unfortunately, I am not good enough with sql to
make the necessary adjustment ot the query. Any additional
thoughts would be apreciated.

--Dan L.
It works, you'll get two rows, one for Rose+Perez+Reds and one
just underneath it for Rose+Perez+Phil lies.

If you want to sum up the years on different teams, create a new
query, based on the one below, that holds the two players names
and the years. You won't be able to get it into a single query
without a lot of heartache.

Select Player1, Player2, sum([Teammate Years]) as [ttlmate
years] from query1 group by player1, player2;
DFS wrote:
>Dan wrote:
I am trying to create a query (in either sql or the design
view) to determine which two (or more I suppose if it's not
too complicated) baseball players were teammates the
longest. The database includes the following fields:
YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the
number of years players would have played together on a
team. (Technical note: some players have records for more
than one team for a given year because they may have been
traded during the season; for the purpose of this analysis,
I will assume that if two players each have a record for a
partiucular team for a particular year, they were
teammates).

Thanks in advance for any help on this query.


This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team,
Count(A.Year ) AS [Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;



--
Bob Quintal

PA is y I've altered my email address.

--
Posted via a free Usenet account from http://www.teranews.com

Jul 25 '06 #6
DFS
And, try this one for a by-player-by-year view

TRANSFORM First(Team) AS Team1
SELECT Player
FROM Table1
GROUP BY Player
PIVOT Year;
Dan wrote:
As I have tested and experimetned withthis query I realized it only
works for players who are teamates on one team. It doesn' t pick up
guys who may have been teammates on more than one team. For example,
Pete Rose and Tony Perez were teammates for 16 years on the Reds but
also 1 year on the Phillies. Unfortunately, I am not good enough with
sql to make the necessary adjustment ot the query. Any additional
thoughts would be apreciated.

--Dan L.
DFS wrote:
>Dan wrote:
>>I am trying to create a query (in either sql or the design view) to
determine which two (or more I suppose if it's not too complicated)
baseball players were teammates the longest. The database includes
the following fields: YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the number of
years players would have played together on a team. (Technical note:
some players have records for more than one team for a given year
because they may have been traded during the season; for the purpose
of this analysis, I will assume that if two players each have a
record for a partiucular team for a particular year, they were
teammates).

Thanks in advance for any help on this query.


This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team,
Count(A.Year ) AS [Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;

Jul 25 '06 #7
DFS
Bob Quintal wrote:
"Dan" <sp******@hotma il.comwrote in
news:11******** **************@ m79g2000cwm.goo glegroups.com:
>As I have tested and experimetned withthis query I realized it
only works for players who are teamates on one team. It
doesn' t pick up guys who may have been teammates on more than
one team. For example, Pete Rose and Tony Perez were
teammates for 16 years on the Reds but also 1 year on the
Phillies. Unfortunately, I am not good enough with sql to
make the necessary adjustment ot the query. Any additional
thoughts would be apreciated.

--Dan L.
It works, you'll get two rows, one for Rose+Perez+Reds and one
just underneath it for Rose+Perez+Phil lies.

If you want to sum up the years on different teams, create a new
query, based on the one below, that holds the two players names
and the years. You won't be able to get it into a single query
without a lot of heartache.

Select Player1, Player2, sum([Teammate Years]) as [ttlmate
years] from query1 group by player1, player2;

No need to do that, is there? This one will sum the total years they were
teammates on any team:

SELECT A.Player AS Player1, A1.Player AS Player2, Count(A.Year) AS [Total
Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player;


>
>DFS wrote:
>>Dan wrote:
I am trying to create a query (in either sql or the design
view) to determine which two (or more I suppose if it's not
too complicated) baseball players were teammates the
longest. The database includes the following fields:
YearId, PlayerId, and teamId.

I have been unable to write query that can caluclate the
number of years players would have played together on a
team. (Technical note: some players have records for more
than one team for a given year because they may have been
traded during the season; for the purpose of this analysis,
I will assume that if two players each have a record for a
partiucula r team for a particular year, they were
teammates) .

Thanks in advance for any help on this query.
This self-join may be what you're looking for:

SELECT A.Player as Player1, A1.Player AS Player2, A.Team,
Count(A.Yea r) AS [Teammate Years]
FROM Table1 A INNER JOIN Table1 A1
ON (A.Team = A1.Team) AND (A.Year = A1.Year)
WHERE A.Player < A1.Player
GROUP BY A.Player, A1.Player, A.Team;


--
Bob Quintal

PA is y I've altered my email address.

Jul 25 '06 #8

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

Similar topics

4
2275
by: leegold2 | last post by:
Let's I do a mysql query and then I do a, for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...} and it gives me this: PageID Title URL Description 1 lee's gogle1.com This is lee's website. 1 lee's gogle2.com This is lee's website. 2 Jon's yaho1.com This is Jon's website. 2 Jon's yaho2.com This is Jon's website.
3
2599
by: Roman | last post by:
I've been trying this one for 2-3 hours and can't figure it out. I'de appreciate any help or pointers in the right direction. Thanks. Query I need the query to return me all the lottery names and results that have the latest date in the database for that particular game and for the state . So the return data from the data below data would be: Result: --------------------------
7
1751
by: LineVoltageHalogen | last post by:
Greetings All, I was hoping that someone might be able to help me with the following issue: table ddl: create table exchange ( exchangefrom varchar(6), exchangeto varchar(6), exchangecode varchar(6),
1
2108
by: simina | last post by:
Hi... I have an "appointments" page where the user should (or not necessarily) choose a date and time for his appointment, from 6 combo boxes:year, month, day, hour, minute and AM or PM, without seconds 'cause I set them to 0. I need to be able to save them in the database (Access) where the format of the field is for example: 12/22/2004 14:08:00 Now, I do have already code that does that but my supervisor doesn't like it because it's...
198
11574
by: Sy Borg | last post by:
Hello: We are designing two multi-user client server applications that performs large number of transactions on database servers. On an average Application A has a 50% mix of select and update/insert/delete statements and application B has 80-20 mix of select and update/insert/delete statements. Being able to scale the databases as needed so the performance is unaffected, is one of our critical requirements. We've been investigating...
28
3925
by: Arial | last post by:
My SQL string is kind of wierd one. In my application, I need to select things from an unknown name table. But I know the table name before the SQL command is executed. For instance, Dim varname as string = 'one of my variable. It's part of the table name. Dim t1 as String = varname+ "0000"
12
2747
by: Bill Bob | last post by:
I am going mad with this Query. I need to join 3 Tables. Their Formats are Vouchers NOT NULL , NOT NULL , NULL , NOT NULL , (255) CONSTRAINT PRIMARY KEY CLUSTERED
9
1533
by: DFS | last post by:
The following data set is building inspection visits. It consists of multiple visits (2+) made to the same building on the same day. I want to get a list of visits made to the same building on the same day, but by different employees, and for different visit codes (eg records 5-6, or 9-11) Here's the table =====================================
5
2243
by: steven.fafel | last post by:
I am running 2 versions of a correlated subquery. The two version differ slightly in design but differ tremendously in performance....if anyone can answer this, you would be awesome. The "bad" query attempts to build a result set using a correlated subquery. The part causing the error is that the correlated subquery is part of a derived table (joining 3 tables). Trying to run the query takes a long time and the more records in the...
0
9669
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10207
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10155
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9995
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5431
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3718
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.