我不确定这是否可能我不知道我的表:tbchecked
[id] [name] [status]
001 john present
001 john present
001 john absent
001 john absent
001 john leave
001 john present
001 john present
002 ace present
002 ace absent
002 ace leave
...
有可能吗?在gridview1中输出如何查询? 1-7不是在一周我想要计数1到xxx[id] [name] [1] [2] [3] [4] [5] [6] [7]
001 john present present absent absent leave present present
002 ace present absent leave ...
答案 0 :(得分:0)
你可以试试这样的东西
DECLARE @TABLE TABLE
(
ID INT,
STATUS VARCHAR(10)
)
INSERT INTO @TABLE
VALUES (1, 'present'),
(1, 'present'),
(1, 'absent'),
(1, 'absent'),
(1, 'leave'),
(1, 'present'),
(1, 'present'),
(2, 'present'),
(2, 'absent'),
(2, 'leave')
SELECT T1.ID,
STUFF(
(
SELECT ',' + T2.STATUS
FROM @TABLE T2
WHERE T1.ID = T2.ID
FOR XML PATH ('')
),1,1,'')
FROM @TABLE T1
GROUP BY T1.ID
否则,您可以使用光标执行循环Declare Cursor
答案 1 :(得分:0)
如果您的ID只有七行,那么这可能很有用。 如果ID是任意随机数,那么你必须去 对于动态sql或程序(如果可能)。
select ID,
SUBSTRING_INDEX(status,',','1'),
SUBSTRING_INDEX(SUBSTRING_INDEX(status,',','2'),',','-1'),
SUBSTRING_INDEX(SUBSTRING_INDEX(status,',','3'),',','-1'),
SUBSTRING_INDEX(SUBSTRING_INDEX(status,',','4'),',','-1'),
SUBSTRING_INDEX(SUBSTRING_INDEX(status,',','5'),',','-1'),
SUBSTRING_INDEX(SUBSTRING_INDEX(status,',','6'),',','-1'),
SUBSTRING_INDEX(SUBSTRING_INDEX(status,',','7'),',','-1')
from
(
SELECT ID, group_concat(status) as status
FROM TABLE T
GROUP BY ID
) A