Click here to Skip to main content
15,902,845 members
Home / Discussions / Database
   

Database

 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
jkirkerx12-Nov-15 7:55
professionaljkirkerx12-Nov-15 7:55 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
Jörgen Andersson12-Nov-15 9:13
professionalJörgen Andersson12-Nov-15 9:13 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
jkirkerx12-Nov-15 10:00
professionaljkirkerx12-Nov-15 10:00 
GeneralRe: SQL Linq, better idea than a join for as enumerable Pin
Jörgen Andersson12-Nov-15 10:06
professionalJörgen Andersson12-Nov-15 10:06 
QuestionFind who dropped a table or column or view Pin
indian1435-Nov-15 7:50
indian1435-Nov-15 7:50 
AnswerRe: Find who dropped a table or column or view Pin
Corporal Agarn5-Nov-15 7:58
professionalCorporal Agarn5-Nov-15 7:58 
AnswerRe: Find who dropped a table or column or view Pin
RNA Team5-Nov-15 18:48
RNA Team5-Nov-15 18:48 
GeneralRe: Find who dropped a table or column or view Pin
Mycroft Holmes5-Nov-15 19:31
professionalMycroft Holmes5-Nov-15 19:31 
AnswerRe: Find who dropped a table or column or view Pin
Afzaal Ahmad Zeeshan8-Nov-15 1:04
professionalAfzaal Ahmad Zeeshan8-Nov-15 1:04 
QuestionWhat is the default isolation level of sqlserver 2008 R2 and 2005 Pin
MyJoiT3-Nov-15 2:15
MyJoiT3-Nov-15 2:15 
AnswerRe: What is the default isolation level of sqlserver 2008 R2 and 2005 Pin
Richard MacCutchan3-Nov-15 2:44
mveRichard MacCutchan3-Nov-15 2:44 
AnswerRe: What is the default isolation level of sqlserver 2008 R2 and 2005 Pin
Afzaal Ahmad Zeeshan3-Nov-15 3:56
professionalAfzaal Ahmad Zeeshan3-Nov-15 3:56 
QuestionSql Server DataBase Pin
Member 121090622-Nov-15 21:40
Member 121090622-Nov-15 21:40 
AnswerRe: Sql Server DataBase Pin
Chris Quinn3-Nov-15 0:32
Chris Quinn3-Nov-15 0:32 
AnswerRe: Sql Server DataBase Pin
RNA Team3-Nov-15 21:02
RNA Team3-Nov-15 21:02 
GeneralRe: Sql Server DataBase Pin
Mycroft Holmes3-Nov-15 21:15
professionalMycroft Holmes3-Nov-15 21:15 
GeneralRe: Sql Server DataBase Pin
RNA Team3-Nov-15 21:50
RNA Team3-Nov-15 21:50 
GeneralRe: Sql Server DataBase Pin
Mycroft Holmes4-Nov-15 12:57
professionalMycroft Holmes4-Nov-15 12:57 
QuestionRows as column using Pivot but with multiple rows and Column Pin
indian14330-Oct-15 3:55
indian14330-Oct-15 3:55 
AnswerRe: Rows as column using Pivot but with multiple rows and Column Pin
Chris Quinn30-Oct-15 5:43
Chris Quinn30-Oct-15 5:43 
AnswerRe: Rows as column using Pivot but with multiple rows and Column Pin
indian14330-Oct-15 5:44
indian14330-Oct-15 5:44 
AnswerRe: Rows as column using Pivot but with multiple rows and Column Pin
RNA Team30-Oct-15 19:23
RNA Team30-Oct-15 19:23 
@indian1433, here is a way (The first version is static)

DECLARE @T TABLE(StudentNames VARCHAR(20), Id INT, AggregateMarks INT, TotalMarks INT)
INSERT INTO @T VALUES
	('NAME1',1,300,503),
	('NAME2',2,250,500),
	('NAME3',3,378,504),
	('NAME4',4,490,500),
	('NAME5',5,399,500)

SELECT X.ID,X.TotalMarks,t1.[AggregateMarks],X.[NAME1],X.[NAME2],X.[NAME3],X.[NAME4],X.[NAME5]
FROM @T 
PIVOT 
(
    MAX(AggregateMarks)
    FOR StudentNames
    IN ([NAME1],[NAME2],[NAME3],[NAME4],[NAME5])
) AS X
JOIN @T t1 on t1.Id  = X.Id 
ORDER BY 1


I have done this one to show you how it must go like. The dynamic version follows

CREATE TABLE #T (StudentNames VARCHAR(20), Id INT, AggregateMarks INT, TotalMarks INT)
INSERT INTO #T VALUES
	('NAME1',1,300,503),
	('NAME2',2,250,500),
	('NAME3',3,378,504),
	('NAME4',4,490,500),
	('NAME5',5,399,500)

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)
 
select @cols = STUFF((SELECT ',' + QUOTENAME(StudentNames) 
                    from #T
                    group by StudentNames, id
                    order by id
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT X.ID,X.TotalMarks,t1.AggregateMarks, ' + @cols + N' from #T             
            PIVOT 
            (
                 MAX(AggregateMarks)
                 FOR StudentNames IN (' + @cols + N')
            ) AS X 	
			JOIN #T t1 on t1.Id  = X.Id 		
			ORDER BY 1'

print @query

exec sp_executesql @query

DROP TABLE #T


Hope this helps.

modified 31-Oct-15 2:14am.

AnswerRe: Rows as column using Pivot but with multiple rows and Column Pin
sandeepmittal118-Nov-15 0:04
sandeepmittal118-Nov-15 0:04 
GeneralRe: Rows as column using Pivot but with multiple rows and Column Pin
Richard Deeming9-Nov-15 2:01
mveRichard Deeming9-Nov-15 2:01 
GeneralRe: Rows as column using Pivot but with multiple rows and Column Pin
sandeepmittal119-Nov-15 19:25
sandeepmittal119-Nov-15 19:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.