Click here to Skip to main content
15,917,859 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi I am new to SQL Server 2008 r2
Actually, I have a table like this

SQL
STUDENTID STUDENTSUBJECT        YEAR1           MARKS1 RANK1     YEAR2                 MARKS2 
1          MATHS       2013-06-07-2014-04-05    10    DGRADE     NULL                   NULL     
1          BPC            NULL                 NUll   NULL    2012-06-11-2013-05-14      6


RANK2 YEAR3 MARKS3  RANK3
null   null  null    null
DGRADE null   null   null


i worte a query like this

SQL
SELECT max(STUDENTID)as STUDENTID,MAX(STUDENTSUBJECT )AS STUDENTSUBJECT ,MAX(YEAR1),MAX(MARKS1),MAX(RANK1),MAX(YAER2),MAX(MARKS2),MAX(RANK2),MAX(YEAR3),MAX(MARKS3),MAX(RANK3) FROM STUDENT  GROUP BY STUDENTSUBJECT  


This above query is working for SAME SUBJECT like (MATHS,MATHS, etc.) but it is not working for diff subjects like above table I mention.
please help me


excepted o/p:
o/p:
SQL
STUDENTID STUDENTSUBJECT        YEAR1        MARKS1 RANK1    YEAR2               MARKS2 
1          MATHS       2013-06-07-2014-04-05  10    DGRADE  2012-06-11-2013-05-14 null
1          BPC            NULL               NUll    NULL    null                  6
 

RANK2 YEAR3 MARKS3  RANK3
null  null  null    null
DGrade null   null   nul


i have to get all in first row in a table like above one
Posted
Updated 25-Sep-13 5:44am
v4
Comments
ArunRajendra 25-Sep-13 7:40am    
what's the output you are expecting?
Member 9846414 25-Sep-13 11:29am    
o/p:
STUDENTID STUDENTSUBJECT YEAR1 MARKS1 RANK1 YEAR2 MARKS2
1 MATHS 2013-06-07-2014-04-05 10 DGRADE 2012-06-11-2013-05-14 6
1 BPC NULL NUll NULL null


RANK2 YEAR3 MARKS3 RANK3
DGRADE null null null
null null null null
[no name] 25-Sep-13 7:44am    
You write a WHERE clause
Madhu Nair 25-Sep-13 7:46am    
why are you taking MAX(STUDENTSUBJECT)? Try Removing this from the query and see if you are getting the desired output
coded007 25-Sep-13 7:46am    
use "StudentSubject" instead of "Max(StudentSubject)" query will work

Use the ISNULL() built-in function.

SQL
SELECT 
MAX(STUDENTID) AS STUDENTID,
MAX(STUDENTSUBJECT) AS STUDENTSUBJECT,
ISNULL(MAX(YEAR1),"") As Year1,
ISNULL(MAX(MARKS1),"") As Marks1,
ISNULL(MAX(RANK1),"") As Rank1,
ISNULL(MAX(YEAR2),"") As Year2,
ISNULL(MAX(MARKS2),"") As Marks2,
ISNULL(MAX(RANK2),"") As Rank2,
ISNULL(MAX(YEAR3),"") As Year3,
ISNULL(MAX(MARKS3),"") As Marks3,
ISNULL(MAX(RANK3),"") As Rank3
FROM STUDENT
GROUP BY STUDENTSUBJECT  



I do not understand why you use MAX(StudentID) and MAX(StudentSubject). Maybe you should use "Improve Question" and explain exactly what you are trying to get as a result (the requirements statement in simple terms).
 
Share this answer
 
v3
SELECT	STUDENTID 
,	STUDENTSUBJECT
,	ISNULL(YEAR1,'')	AS 'YEAR1'
,	ISNULL(MARKS1,'')	AS 'MARKS1'
,	ISNULL(RANK1,'')	AS 'RANK1'
,	ISNULL((Select MAX(Year2) FROM Table26),'') AS 'YEAR2'
,	ISNULL(MARKS2,'')	AS 'MARKS2'
,	ISNULL(RANK2,'')	AS 'RANK2'
,	ISNULL(YEAR3,'')	AS 'YEAR3'
,	ISNULL(MARKS3,'')	AS 'MARKS3'
,	ISNULL(RANK3,'')	AS 'RANK3'
	INTO #TableName
	FROM Table26 

	UPDATE #TableName
	SET YEAR2 = '' 
	WHERE STUDENTSUBJECT = 'BPC'
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900