Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-08-26 : 00:10:24
|
Hi, Can anyone help me with the Count sql query? Below are the dataset:SELECT Level, Teacher, StudentIDFROM SchooldatabaseLevel Teacher StudentID A Andy 112233B Tony 112233C Sam 123344D Dean 554455 E Daniel 554455How do I write the sql qeury to have the output as below:Level Teacher StudentID CountA,B Andy, Tony 112233 2C Sam 123344 1D, E Dean, Daniel 554455 2Thank you allSQLBoy |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-08-26 : 01:05:03
|
[code];with aTable(Level,Teacher,StudentID)AS (SELECT 'A', 'Andy', 112233 UNION ALL SELECT 'B', 'Tony', 112233 UNION ALL SELECT 'C', 'Sam', 123344 UNION ALL SELECT 'D', 'Dean', 554455 UNION ALL SELECT 'E', 'Daniel', 554455)SELECT STUFF(x.lvl,1,1,'') as Level ,STUFF(y.Teachers,1,1,'') as Teachers ,Tbl.StudentID ,Tbl.[Count] as [Count]FROM ( SELECT StudentID ,COUNT(StudentID) as [COUNT] FROM aTable GROUP BY StudentID ) AS Tbl CROSS APPLY ( SELECT ', ' + A.Level FROM aTable AS A WHERE A.StudentID = Tbl.StudentID FOR XML PATH(''))X(lvl) CROSS APPLY ( SELECT ', ' + A.Teacher FROM aTable AS A WHERE A.StudentID = Tbl.StudentID FOR XML PATH(''))Y(Teachers)[/code]sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-08-26 : 01:05:20
|
output:Level Teachers StudentID Count A, B Andy, Tony 112233 2 C Sam 123344 1 D, E Dean, Daniel 554455 2 sabinWeb MCP |
|
|
SQLBoy14
Yak Posting Veteran
70 Posts |
Posted - 2014-08-26 : 15:18:04
|
Hello Stepson. On your select statement:(SELECT 'A', 'Andy', 112233 UNION ALL SELECT 'B', 'Tony', 112233 UNION ALL SELECT 'C', 'Sam', 123344 UNION ALL SELECT 'D', 'Dean', 554455 UNION ALL SELECT 'E', 'Daniel', 554455)if I have 1000 records, how can I handle each detail? Thank youSQLBoy |
|
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2014-08-27 : 01:43:48
|
1000 records is not much.You can tested and see how it does.Keep in mind, we don't know your table/structure. Do you have indecs?here is a sample of testing:SQL Server Execution Times: CPU time = 0 ms, elapsed time = 25 ms.SQL Server Execution Times: CPU time = 0 ms, elapsed time = 1 ms.IF Object_ID('tempDB..#aTable') IS NOT NULLBEGIN Drop Table tempDB..#aTableEND/*CREATE TABLE #aTable(iIndex INT IDENTITY(1,1) ,[Level] CHAR(1) ,[Teacher] VARCHAR(30) ,[StudentID] INT)INSERT INTO #aTable(Level,Teacher,StudentID)VALUES(char(rand()*26+65) , char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) +char(rand()*26+65) , 112233--char(rand()*9+48) + char(rand()*9+48) +char(rand()*9+48) +char(rand()*9+48)+char(rand()*9+48)+char(rand()*9+48) )Go 250INSERT INTO #aTable(Level,Teacher,StudentID)VALUES(char(rand()*26+65) , char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) +char(rand()*26+65) , 123344--char(rand()*9+48) + char(rand()*9+48) +char(rand()*9+48) +char(rand()*9+48)+char(rand()*9+48)+char(rand()*9+48) )Go 250INSERT INTO #aTable(Level,Teacher,StudentID)VALUES(char(rand()*26+65) , char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) +char(rand()*26+65) , 554455--char(rand()*9+48) + char(rand()*9+48) +char(rand()*9+48) +char(rand()*9+48)+char(rand()*9+48)+char(rand()*9+48) )Go 250INSERT INTO #aTable(Level,Teacher,StudentID)VALUES(char(rand()*26+65) , char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) + char(rand()*26+65) +char(rand()*26+65) , 778899--char(rand()*9+48) + char(rand()*9+48) +char(rand()*9+48) +char(rand()*9+48)+char(rand()*9+48)+char(rand()*9+48) )Go 250*/--SELECT * from #aTableset statistics io onset statistics time on SELECT STUFF(x.lvl,1,1,'') as Level ,STUFF(y.Teachers,1,1,'') as Teachers ,Tbl.StudentID ,Tbl.[Count] as [Count]FROM ( SELECT StudentID ,COUNT(StudentID) as [COUNT] FROM #aTable GROUP BY StudentID ) AS Tbl CROSS APPLY ( SELECT ', ' + A.Level FROM #aTable AS A WHERE A.StudentID = Tbl.StudentID FOR XML PATH(''))X(lvl) CROSS APPLY ( SELECT ', ' + A.Teacher FROM #aTable AS A WHERE A.StudentID = Tbl.StudentID FOR XML PATH(''))Y(Teachers)set statistics io offset statistics time off sabinWeb MCP |
|
|
stepson
Aged Yak Warrior
545 Posts |
|
|
|
|
|
|