dbTalk Databases Forums  

SQL How-to question

comp.databases comp.databases


Discuss SQL How-to question in the comp.databases forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
davepkz@hotmail.com
 
Posts: n/a

Default SQL How-to question - 01-03-2007 , 04:14 PM






Suppose I have the following data:

student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


....and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-

Any ideas on how to do such a thing in SQL?
many thanks
Dave


Reply With Quote
  #2  
Old   
Jason Lepack
 
Posts: n/a

Default Re: SQL How-to question - 01-03-2007 , 04:23 PM






Assuming that in every quarter there is only one grade for each
student, you could create a Crosstab Query.

I called the table "tbl_grades" and here is the SQL.

TRANSFORM First(tbl_grades.grade) AS QGrade
SELECT tbl_grades.student
FROM tbl_grades
GROUP BY tbl_grades.student
PIVOT tbl_grades.quarter;

Cheers,
Jason Lepack

davepkz (AT) hotmail (DOT) com wrote:
Quote:
Suppose I have the following data:

student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


...and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-

Any ideas on how to do such a thing in SQL?
many thanks
Dave


Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: SQL How-to question - 01-03-2007 , 05:32 PM



(davepkz (AT) hotmail (DOT) com) writes:
Quote:
Suppose I have the following data:

student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


...and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-

Any ideas on how to do such a thing in SQL?
many thanks
In SQL Server (and ANSI SQL) you can do:

SELECT student,
MAX(CASE quarter WHEN 1 THEN grade END) AS AS Q1grade,
MAX(CASE quarter WHEN 2 THEN grade END) AS AS Q2grade,
MAX(CASE quarter WHEN 3 THEN grade END) AS AS Q3grade,
MAX(CASE quarter WHEN 4 THEN grade END) AS AS Q4grade
FROM tbl
GROUP BY student


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) 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


Reply With Quote
  #4  
Old   
Van T. Dinh
 
Posts: n/a

Default Re: SQL How-to question - 01-03-2007 , 08:18 PM



If this is done in Access, check Access Help on Crosstab Queries ...

--
HTH
Van T. Dinh
MVP (Access)



<davepkz (AT) hotmail (DOT) com> wrote

Quote:
Suppose I have the following data:

student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


...and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-

Any ideas on how to do such a thing in SQL?
many thanks
Dave




Reply With Quote
  #5  
Old   
strawberry
 
Posts: n/a

Default Re: SQL How-to question - 01-04-2007 , 06:57 AM



Quote:
student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


...and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-

You could also do this (although that transform method looks
smarter...)

SELECT g.student,q1.grade q1grade,q2.grade q2grade,q3.grade
q3grade,q4.grade q4grade FROM grades g
LEFT JOIN grades q1 ON q1.student = g.student AND q1.quarter =1
LEFT JOIN grades q2 ON q2.student = g.student AND q2.quarter=2
LEFT JOIN grades q3 ON q3.student = g.student AND q3.quarter=3
LEFT JOIN grades q4 ON q4.student = g.student AND q4.quarter=4
GROUP BY g.student



Reply With Quote
  #6  
Old   
Walt
 
Posts: n/a

Default Re: SQL How-to question - 01-04-2007 , 07:19 AM




<davepkz (AT) hotmail (DOT) com> wrote

Quote:
Suppose I have the following data:

student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


...and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-

Any ideas on how to do such a thing in SQL?
many thanks
Dave

It's called crosstabulation, or crosstab for short. A quick web search on
crosstab SQL query should give you some pointers.





Reply With Quote
  #7  
Old   
davepkz@hotmail.com
 
Posts: n/a

Default Re: SQL How-to question - 01-04-2007 , 12:18 PM





Thanks to all. The examples you provided were exactly what I needed.
Dave


Quote:
student quarter grade
------- ------- -----
Jones 1 A
Jones 2 B+
Jones 3 A
Jones 4 B-
Smith 1 B+
Smith 2 B
Smith 3 B
Smith 4 B-


...and I want a SELECT statement to output something like this:

student Q1grade Q2grade Q3grade Q4grade
------- ------- ------- ------- -------
Jones A B+ A B-
Smith B+ B B B-


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.