如何在前端将PIVOT SQL Server查询与ASP.NET集成

时间:2013-08-31 07:39:23

标签: c# asp.net sql-server

USE [SAFLA]

DECLARE 
      @cols AS NVARCHAR(MAX)
    , @query AS NVARCHAR(MAX)

SET @cols = STUFF((
    SELECT DISTINCT ',' + QUOTENAME(w.week_range)
    FROM Weekly_Attendance AS w
    FOR XML PATH (''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')

SET @query = '
    SELECT stu_id, name, ' + @cols + ' 
    FROM (
        SELECT 
              stu.stu_id
            , stu.name
            , w.total_present_lec
            , w.week_range
        FROM Student_Registration AS stu 
        JOIN Weekly_Attendance w ON stu.stu_id = w.stu_id
    ) x
    PIVOT 
    (
        SUM(total_present_lec)
        FOR week_range in (' + @cols + ')
        --GROUP BY total_present_lec
    ) p'

EXECUTE (@query)

这是一个我希望与ASP.NET页面集成的SQL查询。

0 个答案:

没有答案