I am trying to write a single SQL query that would retrieve the data
that I need. For example, I have a table called Athletes that has 2
fields: name and sport containing the name of an athlete and the
sports that he or she participates in. Some sample data:
John,hockey
Michael,footbal l
John,swimming
Eric,swimming
Michael,basebal l
I need a SQL query that would return each athlete and the sports they
participate in:
John - hockey,swimming
Michael - football,baseba ll
Eric - swimming
I've tried all sorts of joins and aggregate functions but with no
success. I want to avoid to run a query listing the athletes and then
doing a query for each of them to get the sports. Can anyone provide
some tips on doing this with just one SQL query? 5 5233
On Apr 3, 2:28 pm, "AdrianG" <adrian.grigo.. .@altairtech.ca wrote:
I am trying to write a single SQL query that would retrieve the data
that I need. For example, I have a table called Athletes that has 2
fields: name and sport containing the name of an athlete and the
sports that he or she participates in. Some sample data:
John,hockey
Michael,footbal l
John,swimming
Eric,swimming
Michael,basebal l
I need a SQL query that would return each athlete and the sports they
participate in:
John - hockey,swimming
Michael - football,baseba ll
Eric - swimming
I've tried all sorts of joins and aggregate functions but with no
success. I want to avoid to run a query listing the athletes and then
doing a query for each of them to get the sports. Can anyone provide
some tips on doing this with just one SQL query?
You need to break that out into more tables. Put the names in one
table, the sports in another table and a cross table in between. This
will be a many to many relationship and make your query a piece of
cake.
AdrianG (ad************ *@altairtech.ca ) writes:
I am trying to write a single SQL query that would retrieve the data
that I need. For example, I have a table called Athletes that has 2
fields: name and sport containing the name of an athlete and the
sports that he or she participates in. Some sample data:
John,hockey
Michael,footbal l
John,swimming
Eric,swimming
Michael,basebal l
I need a SQL query that would return each athlete and the sports they
participate in:
John - hockey,swimming
Michael - football,baseba ll
Eric - swimming
I've tried all sorts of joins and aggregate functions but with no
success. I want to avoid to run a query listing the athletes and then
doing a query for each of them to get the sports. Can anyone provide
some tips on doing this with just one SQL query?
It's indeed not a trivial problem, as there is no direct function fot
this in SQL Server. SQL Server MVP Anith Sen has a couple of methods on http://www.projectdmx.com/tsql/rowconcatenate.aspx.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx
On Apr 3, 5:00 pm, "Brad" <Brad.Marsh...@ Teksouth.comwro te:
On Apr 3, 2:28 pm, "AdrianG" <adrian.grigo.. .@altairtech.ca wrote:
I am trying to write a single SQL query that would retrieve the data
that I need. For example, I have a table called Athletes that has 2
fields: name and sport containing the name of an athlete and the
sports that he or she participates in. Some sample data:
John,hockey
Michael,footbal l
John,swimming
Eric,swimming
Michael,basebal l
I need a SQL query that would return each athlete and the sports they
participate in:
John - hockey,swimming
Michael - football,baseba ll
Eric - swimming
I've tried all sorts of joins and aggregate functions but with no
success. I want to avoid to run a query listing the athletes and then
doing a query for each of them to get the sports. Can anyone provide
some tips on doing this with just one SQL query?
You need to break that out into more tables. Put the names in one
table, the sports in another table and a cross table in between. This
will be a many to many relationship and make your query a piece of
cake.- Hide quoted text -
- Show quoted text -
Thanks Brad - normalizing the data will surely help but still I can't
see a quick way of retrieving the concatenated "sports" fields. The
real data is a bit more complicated than the sample that I mentioned
but the idea is the same.
On Apr 3, 6:00 pm, Erland Sommarskog <esq...@sommars kog.sewrote:
AdrianG (adrian.grigo.. .@altairtech.ca ) writes:
I am trying to write a single SQL query that would retrieve the data
that I need. For example, I have a table called Athletes that has 2
fields: name and sport containing the name of an athlete and the
sports that he or she participates in. Some sample data:
John,hockey
Michael,footbal l
John,swimming
Eric,swimming
Michael,basebal l
I need a SQL query that would return each athlete and the sports they
participate in:
John - hockey,swimming
Michael - football,baseba ll
Eric - swimming
I've tried all sorts of joins and aggregate functions but with no
success. I want to avoid to run a query listing the athletes and then
doing a query for each of them to get the sports. Can anyone provide
some tips on doing this with just one SQL query?
It's indeed not a trivial problem, as there is no direct function fot
this in SQL Server. SQL Server MVP Anith Sen has a couple of methods onhttp://www.projectdmx. com/tsql/rowconcatenate. aspx.
--
Erland Sommarskog, SQL Server MVP, esq...@sommarsk og.se
Books Online for SQL Server 2005 athttp://www.microsoft.c om/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.c om/sql/prodinfo/previousversion s/books.mspx- Hide quoted text -
- Show quoted text -
Right on, Erland! The page compiled by Anith Sen was exactly what I
was looking for - concatenation of row values. I tried the first
approach (Dynamic SQL) but while it worked for a small number of
records, it failed ( Server stack limit has been reached.) against a
larger number (i.e. 20,000 records which is not really that much).
However, the blackbox XML method worked like a charm. Here is a
slightly modified version (to only show distinct sports and to remove
the trailing spaces) that worked quite fast:
SELECT p1.name,
( SELECT distinct RTRIM(sport) + ', '
FROM Athletes p2
WHERE p2.name = p1.name
ORDER BY RTRIM(sport) + ', '
FOR XML PATH('') ) AS sports
FROM Athletes p1
GROUP BY name ;
Now, the real database is more complex than this example but it is
surely a great start.
Thanks again!
Adrian
AdrianG (ad************ *@altairtech.ca ) writes:
Right on, Erland! The page compiled by Anith Sen was exactly what I
was looking for - concatenation of row values. I tried the first
approach (Dynamic SQL) but while it worked for a small number of
records, it failed ( Server stack limit has been reached.) against a
larger number (i.e. 20,000 records which is not really that much).
However, the blackbox XML method worked like a charm. Here is a
slightly modified version (to only show distinct sports and to remove
the trailing spaces) that worked quite fast:
SELECT p1.name,
( SELECT distinct RTRIM(sport) + ', '
FROM Athletes p2
WHERE p2.name = p1.name
ORDER BY RTRIM(sport) + ', '
FOR XML PATH('') ) AS sports
FROM Athletes p1
GROUP BY name ;
Now, the real database is more complex than this example but it is
surely a great start.
Great to hear that you got a solution.
I don't recall if Anith discusses this, but there is a small problem
with the method above: if the data has charcaters that are special to
XML, they will be itemised. For instance an ampersand will become &.
There are some more or less ugly methods to habdle that, but I don't
recall the details at the moment.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se
Books Online for SQL Server 2005 at http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinf...ons/books.mspx This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: netpurpose |
last post by:
I need to extract data from this table to find the lowest prices of
each product as of today. The product will be listed/grouped by the
name only, discarding the product code - I use SUBSTRING(ProductName,
1, CHARINDEX('(', ProductName)-2).
I can get this result, but I had to use several views (totally
inefficient). I think this can be...
|
by: Simon Bailey |
last post by:
How do you created a query in VB?
I have a button on a form that signifies a certain computer in a
computer suite. On clicking on this button i would like to create a
query searching for all details invovling that computer, for the user
to then view.
Any ideas on some code?
Many thanks for any help.
|
by: d.p. |
last post by:
Hi all,
I'm using MS Access 2003.
Bare with me on this description....here's the situation: Imagine insurance,
and working out premiums for different insured properties. The rates for
calculating premiums are dependant on the country in which the client is in.
Therefore, we have a Country table, with its list of rates, a client table
and...
|
by: Alan Lane |
last post by:
Hello world:
I'm including both code and examples of query output. I appologize if
that makes this message longer than it should be.
Anyway, I need to change the query below into a pivot table query. I'm
having trouble doing it. Help!
Here is my code so far:
Sub OldRegionQuery()
|
by: Liam.M |
last post by:
hey guys,
I have one last problem to fix, and then my database is essentially
done...I would therefore very much appreciate any assistance anyone
would be able to provide me with.
Currently I have set up a Query to show only records that meet a
certain criteria...therefore excluding all of the records that do not
meet this criteria (just for...
| |
by: elitecodex |
last post by:
Hey everyone. I have this query
select * from `TableName` where `SomeIDField` 0
I can open a mysql command prompt and execute this command with no
issues. However, Im trying to issue the same command inside of
mysql_real_query and I keep on getting this error back.
"You have an error in your SQL syntax; check the manual that...
|
by: aaronrm |
last post by:
I have a real simple cross-tab query that I am trying to sum on as the
action but I am getting the "data type mismatch criteria expression"
error. About three queries up the food chain from this cross-tab
query I am using a simple query with no grouping where I am filtering
some data out in the criteria line.
I have been out of access for...
|
by: funky |
last post by:
hello,
I've got a big problem ad i'm not able to resolve it. We have a server
running oracle 10g version 10.1.0. We usually use access as front end
and connect database tables for data extraction. We have been using
oracle client 10.1.0.2 with it's odbc for a while without problem. The
problem arose when we decided to reconnect all the tables...
|
by: Doris |
last post by:
It does not look like my message is posting....if this is a 2nd or 3rd
message, please forgive me as I really don't know how this site works.
I want to apologize ahead of time for being a novice with MS Access
and VBA. I desperately need help with 2 queries that I am trying to
put together. I want to thank anyone that can help me out with...
|
by: jsacrey |
last post by:
Hey everybody, got a secnario for ya that I need a bit of help with.
Access 97 using linked tables from an SQL Server 2000 machine.
I've created a simple query using two tables joined by one field
between them. The join field in both tables are indexed and I'm
selecting 1 field from each table to lookup. The Access query is
taking more...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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...
|
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...
|
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...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |