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

sql query with sum and max in two tables

3
table a
id_A'''''''''''''''''''name
1''''''''''''''''''''''''''''tom
2''''''''''''''''''''''''''''frank
3 ''''''''''''''''''''''''john

table b:
id_A'''''''''''''''''''''''''''''''''''id_C'''''''''''points
1'''''''''''''''''''''''''''''''''''''''''''''''''1'''''''''''''''''''''1
1'''''''''''''''''''''''''''''''''''''''''''''''''2'''''''''''''''''''''5
1'''''''''''''''''''''''''''''''''''''''''''''''''3'''''''''''''''''''''7
2'''''''''''''''''''''''''''''''''''''''''''''''''1''''''''''''''''''' 5
2''''''''''''''''''''''''''''''''''''''''''''''' 2'''''''''''''''''''''12

table C:
id_C, sport

i have to write query that have everything from table A with sum(points) for every person from B along with id_C and points for sport with highest score.table should lok like this:

id_A, name, sum(pts), max(pts), id_C for max(pts)

example for tom:
id_A:1
name:tom
sum(pts): 13
max(pts):7
idC for max(pts):3
Feb 26 '10 #1
4 2215
deepuv04
227 Expert 100+
this is the query for my understand
Try,
SELECTtable_a.id_a,table_a.Name,
SUM(Points),MAX(Points),Max(Id_C)
from Table_a inner join
table_b on table_a.id_a = table_b.id_a
group by table_a.id_a,table_a.Name

I have a question why did you mention the table C

Thanks,
Sandeep
Feb 26 '10 #2
babsi
3
I have a question why did you mention the table C
because table b's primary key is consist of id_a and ib_c

table c:
id_c'''''''''''''''''''game
1'''''''''''''''''''''''''''poker
2''''''''''''''''''''''''''free cell
3''''''''''''''''''''''''''solitaire


tom played 3 games and he won 13 points (sum of pts in b). highest score he achieved in game number 3 solitaire (he won 7 pts in solitaire). result table looks like this

id_A'''name''''''sum(p)''''''max(p)'''''''''game
1'''''''''''''tom''''''''''''''13'''''''''''''''''''7'''''''''''''''''''''''solitaire
2''''''''''''frank''''''''''''17'''''''''''''''''''12'''''''''''''''''''free cell
3''''''''''''john'''''''''''''0''''''''''''''''''''''0'''''''''''''''''''''''0

name of games are not important. it could only be id of games.
Feb 26 '10 #3
deepuv04
227 Expert 100+
ok use the following for getting the id of the game
similarly you can ge the game name also
SELECT table_a.id_a,table_a.Name,
SUM(Points),MAX(Points),
(SELECT TOP 1 Id_C FROM table_B WHERE ID_A = table_A.ID_A ORDER BY Points desc)
from table_a inner join
table_b on table_a.id_a = table_b.id_a
group by table_a.id_a,table_a.Name
Feb 26 '10 #4
babsi
3
thanks deepuv04, it works. I just changed inner join to left join to include people without pts. but thanks a lot once more.
Feb 26 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Phin | last post by:
I need your HELP! I've seen all the posts on using Crystal Reports within vs.net (vb.net) and changing a SQL query at runtime. When I tried to pass in a dataset into the crystal report at...
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
3
by: dk | last post by:
Hi all, Would appreciate some advice on the following: I am trying to speed up an Access database connected to a SQL Server back-end. I know I can use a pass-through query to pass the sql...
3
by: Hyphessobricon | last post by:
Hallo, Indeed, a count of a query with a group by function gives more records than there are and so for-next structures don't function. How is this to be mended. Anyone? Everyone in fact....
2
by: =?Utf-8?B?Q2hyaXM=?= | last post by:
How can I run this query against a table in my Access database? I don't know hwo to use it in C#. In VB I would use .Recordset = "some sql statement". How do I do this in C#? //I get a vlaue...
3
by: KNN | last post by:
Hi I have some tables with hidden attribute set to 1. In the query desgn view , I do not see these tables as expected. But, If I choose the query wizard to create a new query, then i do see...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
6
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...
2
by: existential.philosophy | last post by:
This is a new problem for me: I have some queries that open very slowly in design view. My benchmark query takes about 20 minutes to open in design view. That same query takes about 20 minutes...
9
by: Sinner | last post by:
Hi, I have a field name 'USER' in tableMAIN. How do I replace the user names with corresponding user names. I can do that in xl using vlookup but now I'm trying to find a way to do that in...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.