If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Query Help plaease

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-14-11, 07:16
sanjeeme sanjeeme is offline
Registered User
 
Join Date: May 2011
Posts: 2
Thumbs up Query Help plaease

Hi, I'm new for here and new for mysql too.

so want to get query for crosstab some fields as columns.

As a example

let assume I has simple Student Table like follows

+----------+---------+------------+
--stu_id--||-subject-||-stu_marks-|
+----------+---------+------------+
--1-----|----Word----|----50----|
--2-----|----Excel----|----75----|
--1-----|----Excel----|----80----|
--2-----|---Access---|----68----|
--2-----|----Word----|----33----|


so I want query to alter above table as following

+----------+---------+------------+----------+
|--stu_id---|--Word--|----Excel----|--Access--|
+----------+---------+------------+----------+
|-----1----|----50---|-----80-----|-----------|
|-----2----|----33 --|-----75-----|----68-----|
+----------+---------+------------+----------+

If some one can help me to get exact query its great help

Thanks

Last edited by sanjeeme; 05-14-11 at 07:29.
Reply With Quote
  #2 (permalink)  
Old 05-14-11, 15:28
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT stu_id
     , MAX(CASE WHEN subject = 'Word'
                THEN stu_marks
                ELSE NULL END) AS 'Word'
     , MAX(CASE WHEN subject = 'Excel'
                THEN stu_marks
                ELSE NULL END) AS 'Excel'
     , MAX(CASE WHEN subject = 'Access'
                THEN stu_marks
                ELSE NULL END) AS 'Access'
  FROM student
GROUP
    BY stu_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-16-11, 01:06
sanjeeme sanjeeme is offline
Registered User
 
Join Date: May 2011
Posts: 2
Thumbs up Thanksss

Thanksssss friend. its really help for my other task.
Reply With Quote
  #4 (permalink)  
Old 05-19-11, 04:46
donose.mihai donose.mihai is offline
Registered User
 
Join Date: Apr 2011
Posts: 19
result

see image for result
Attached Thumbnails
Query Help plaease-2011-05-19_114533.jpg  
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On