且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

交叉表查询(出勤)

更新时间:2022-10-18 11:37:55

 SELECT [User_Id],
MAX([1])AS [1],
MAX([2])AS [2],
MAX ([3])AS [3],
MAX([4])AS [4]
FROM [first_db]。[dbo]。[MarkA]
PIVOT

MAX(状态)FOR日期([1],[2],[3],[4])
)as pvt
GROUP BY [User_Id] ORDER BY [User_Id]


i m generating a report of the monthly attendance of the students using the cross table query ..i have a table having fields as User_Id,Date,Time and status
the code which i tried shows attendance seperately for each date for the same User_id


User_Id 1 2 3 4
1121 P null null null
1121 null P null null
1121 null null P null

how do i solve it???

What I have tried:

select [User_Id],[1],[2],[3],[4] from
(select [User_Id], day([Date])as date,Time,Status from [first_db].[dbo].[MarkA]) ma
pivot
(
max[status]
for Date in ([1],[2],[3],[4])
)as pvt
order by
pvt.User_Id

SELECT [User_Id],
       MAX([1]) AS [1],
       MAX([2]) AS [2],
       MAX([3]) AS [3],
       MAX([4]) AS [4] 
  FROM [first_db].[dbo].[MarkA]
   PIVOT
       (
         MAX(Status) FOR Date in ([1],[2],[3],[4])
       )as pvt
  GROUP BY [User_Id]  ORDER BY [User_Id]