在sql server 2005中,如何在一天内完成数量的打孔次数?

时间:2013-11-20 11:08:57

标签: sql-server sql-server-2005

CREATE TABLE ALL_PUNCHES_PIVOT2 (ID INT,strcardid NVARCHAR(100),SwipeDate VARCHAR(60),TIME VARCHAR(40));

INSERT INTO ALL_PUNCHES_PIVOT2 (ID,strcardid,SwipeDate,PUNCHTIME)
VALUES (1,'3716817970','01/08/2013','08:47:53')
INSERT INTO ALL_PUNCHES_PIVOT2 (ID,strcardid,SwipeDate,PUNCHTIME)
VALUES (2,'3716817970','01/08/2013','08:47:56')
INSERT INTO ALL_PUNCHES_PIVOT2 (ID,strcardid,SwipeDate,PUNCHTIME)
VALUES (3,'3716817970','01/08/2013','08:52:29')
INSERT INTO ALL_PUNCHES_PIVOT2 (ID,strcardid,SwipeDate,PUNCHTIME)
VALUES (4,'3716817970','01/08/2013','08:52:31')
INSERT INTO ALL_PUNCHES_PIVOT2 (ID,strcardid,SwipeDate,PUNCHTIME)
VALUES (5,'3716817970','01/08/2013','17:50:14')
INSERT INTO ALL_PUNCHES_PIVOT2 (ID,strcardid,SwipeDate,PUNCHTIME)
VALUES (6,'3716817970','01/08/2013','17:50:17')

SELECT * FROM ALL_PUNCHES_PIVOT2;

ID  strcardid   SwipeDate   PunchTime
1   3716817970  01/08/2013  08:47:53
2   3716817970  01/08/2013  08:47:56
3   3716817970  01/08/2013  08:52:29
4   3716817970  01/08/2013  08:52:31
5   3716817970  01/08/2013  17:50:14
6   3716817970  01/08/2013  17:50:17

现在我使用下面的Pivot来获得结果

SELECT * FROM
    (
        SELECT strcardid,SwipeDate,PunchTime FROM ALL_PUNCHES_PIVOT2
    ) P 
            PIVOT (
            min([PunchTime]) FOR SwipeDate in ([01/08/2013])    
              ) as Pvt

以下是我得到的结果

strcardid   01/08/2013
3716817970  08:47:53

我希望输出如下所示

strcardid   Swipedate  time1     time2    time3    time4    time5    time6
3716817970  01/08/2013 08:47:53  08:47:56 08:52:29 08:52:31 17:50:14 17:50:17

请帮我解决这个问题

2 个答案:

答案 0 :(得分:4)

您可以使用ROW_NUMBER()获取次数,然后执行数据

select * from 
(
    select strcardid, SwipeDate, PunchTime, ROW_NUMBER() OVER (PARTITION BY strcardid, SwipeDate ORDER BY PunchTime) rowno
    from ALL_PUNCHES_PIVOT2
) p
PIVOT (min(PunchTime) FOR rowno in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15])) as pvt

答案 1 :(得分:3)

有什么好处吗?

;WITH CTE as(
    SELECT  *, ROW_NUMBER( ) OVER (PARTITION BY strcardid ORDER BY SwipeDate ASC) AS RowNum
    FROM    ALL_PUNCHES_PIVOT2
) 
SELECT  cte.strcardid
    ,   cte.SwipeDate
    ,   MAX(CASE cte.RowNum WHEN 1 THEN cte.PUNCHTIME ELSE '' END) as time1
    ,   MAX(CASE cte.RowNum WHEN 2 THEN cte.PUNCHTIME ELSE '' END) as time2
    ,   MAX(CASE cte.RowNum WHEN 3 THEN cte.PUNCHTIME ELSE '' END) as time3
    ,   MAX(CASE cte.RowNum WHEN 4 THEN cte.PUNCHTIME ELSE '' END) as time4
    ,   MAX(CASE cte.RowNum WHEN 5 THEN cte.PUNCHTIME ELSE '' END) as time5
    ,   MAX(CASE cte.RowNum WHEN 6 THEN cte.PUNCHTIME ELSE '' END) as time6
    ,   MAX(CASE cte.RowNum WHEN 7 THEN cte.PUNCHTIME ELSE '' END) as time7
    ,   MAX(CASE cte.RowNum WHEN 8 THEN cte.PUNCHTIME ELSE '' END) as time8
    ,   MAX(CASE cte.RowNum WHEN 9 THEN cte.PUNCHTIME ELSE '' END) as time9
    ,   MAX(CASE cte.RowNum WHEN 10 THEN cte.PUNCHTIME ELSE '' END) as time10
    ,   MAX(CASE cte.RowNum WHEN 11 THEN cte.PUNCHTIME ELSE '' END) as time11
    ,   MAX(CASE cte.RowNum WHEN 12 THEN cte.PUNCHTIME ELSE '' END) as time12
    ,   MAX(CASE cte.RowNum WHEN 13 THEN cte.PUNCHTIME ELSE '' END) as time13
    ,   MAX(CASE cte.RowNum WHEN 14 THEN cte.PUNCHTIME ELSE '' END) as time14
    ,   MAX(CASE cte.RowNum WHEN 15 THEN cte.PUNCHTIME ELSE '' END) as time15
FROM    CTE
GROUP BY cte.strcardid, cte.SwipeDate

结果:

strcardid   SwipeDate   time1   time2   time3   time4   time5   time6   time7   time8   time9   time10  time11  time12  time13  time14  time15
3716817970  01/08/2013  08:47:53    08:47:56    08:52:29    08:52:31    17:50:14    17:50:17

SQL Fiddle

编辑:Szymon有更好的答案,但我可以将此作为另一个支点的示例。