Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pivot

Status
Not open for further replies.

geraldjr30b

Programmer
Aug 27, 2015
9
US
hi. i have the following:

SELECT distinct top 30 studentid,[101], [102], [103],[104]
FROM tbl_TEST s
PIVOT
(count(grade_status)
FOR semester IN
([101], [102], [103],[104]))p

How can I actually list the grade_status value instead of the count? Or how can i group by grade_status?

Thanks
 
If the grade_status recorded is 1-to-1 for student/semester, use MAX().

-----------
With business clients like mine, you'd be better off herding cats.
 
Would it be max(grade status) instead of count(grade status)?
 
OP said:
Would it be max(grade status) instead of count(grade status)?

Yes.

Now, remember my previous caveat: if the relationship among studentID, semester, and grade_status is truly 1-1-1, the statement should work.

Why?

Try this:
Code:
CREATE TABLE MyMaxTest
(myvalue char(1))
INSERT INTO MyMaxTest
SELECT 'A'

SELECT Max(myvalue) from MyMaxTest

The MAX() function reports the max value of the column. Since there's only one record, that's the value it reports.

Now try this (assuming you didn't whack the table):

Code:
INSERT INTO MyMaxTest
SELECT 'B'
SELECT Max(myvalue) from MyMaxTest

Now what do you get?

Of course, SQL performed the MAX() calculation, didn't it? So my caveat stands. You can "trick" the PIVOT statement into returning what you need, if and only if the uniqueness I stipulated applies across the fields.


-----------
With business clients like mine, you'd be better off herding cats.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top