以倍数行制动(分割)一行

时间:2014-07-26 00:33:50

标签: sql sql-server sql-server-2005 sql-view

我有一个数据库,可以作为SQL Server 2005上员工的日志。所有信息都在月末填写在一个表中。它有登记入住早晨,退房早晨,登记入住下午,退房午后和评论。 根据要求,该表的结构如下:

USE [BitacoraTrabajo]
GO
/****** Objeto:  Table [dbo].[B_Bitacora]    Fecha de la secuencia de comandos: 07/28/2014 10:25:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[B_Bitacora](
    [BBIT_IdBitacora] [int] IDENTITY(1,1) NOT NULL,
    [BUSU_IdUsuario] [int] NOT NULL,
    [BBIT_Fecha] [datetime] NOT NULL,
    [BBIT_01ManEntra] [datetime] NULL,
    [BBIT_01ManSale] [datetime] NULL,
    [BBIT_01TarEntra] [datetime] NULL,
    [BBIT_01TarSale] [datetime] NULL,
    [BBIT_01Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_02ManEntra] [datetime] NULL,
    [BBIT_02ManSale] [datetime] NULL,
    [BBIT_02TarEntra] [datetime] NULL,
    [BBIT_02TarSale] [datetime] NULL,
    [BBIT_02Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_03ManEntra] [datetime] NULL,
    [BBIT_03ManSale] [datetime] NULL,
    [BBIT_03TarEntra] [datetime] NULL,
    [BBIT_03TarSale] [datetime] NULL,
    [BBIT_03Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_04ManEntra] [datetime] NULL,
    [BBIT_04ManSale] [datetime] NULL,
    [BBIT_04TarEntra] [datetime] NULL,
    [BBIT_04TarSale] [datetime] NULL,
    [BBIT_04Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_05ManEntra] [datetime] NULL,
    [BBIT_05ManSale] [datetime] NULL,
    [BBIT_05TarEntra] [datetime] NULL,
    [BBIT_05TarSale] [datetime] NULL,
    [BBIT_05Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_06ManEntra] [datetime] NULL,
    [BBIT_06ManSale] [datetime] NULL,
    [BBIT_06TarEntra] [datetime] NULL,
    [BBIT_06TarSale] [datetime] NULL,
    [BBIT_06Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_07ManEntra] [datetime] NULL,
    [BBIT_07ManSale] [datetime] NULL,
    [BBIT_07TarEntra] [datetime] NULL,
    [BBIT_07TarSale] [datetime] NULL,
    [BBIT_07Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_08ManEntra] [datetime] NULL,
    [BBIT_08ManSale] [datetime] NULL,
    [BBIT_08TarEntra] [datetime] NULL,
    [BBIT_08TarSale] [datetime] NULL,
    [BBIT_08Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_09ManEntra] [datetime] NULL,
    [BBIT_09ManSale] [datetime] NULL,
    [BBIT_09TarEntra] [datetime] NULL,
    [BBIT_09TarSale] [datetime] NULL,
    [BBIT_09Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_10ManEntra] [datetime] NULL,
    [BBIT_10ManSale] [datetime] NULL,
    [BBIT_10TarEntra] [datetime] NULL,
    [BBIT_10TarSale] [datetime] NULL,
    [BBIT_10Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_11ManEntra] [datetime] NULL,
    [BBIT_11ManSale] [datetime] NULL,
    [BBIT_11TarEntra] [datetime] NULL,
    [BBIT_11TarSale] [datetime] NULL,
    [BBIT_11Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_12ManEntra] [datetime] NULL,
    [BBIT_12ManSale] [datetime] NULL,
    [BBIT_12TarEntra] [datetime] NULL,
    [BBIT_12TarSale] [datetime] NULL,
    [BBIT_12Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_13ManEntra] [datetime] NULL,
    [BBIT_13ManSale] [datetime] NULL,
    [BBIT_13TarEntra] [datetime] NULL,
    [BBIT_13TarSale] [datetime] NULL,
    [BBIT_13Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_14ManEntra] [datetime] NULL,
    [BBIT_14ManSale] [datetime] NULL,
    [BBIT_14TarEntra] [datetime] NULL,
    [BBIT_14TarSale] [datetime] NULL,
    [BBIT_14Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_15ManEntra] [datetime] NULL,
    [BBIT_15ManSale] [datetime] NULL,
    [BBIT_15TarEntra] [datetime] NULL,
    [BBIT_15TarSale] [datetime] NULL,
    [BBIT_15Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_16ManEntra] [datetime] NULL,
    [BBIT_16ManSale] [datetime] NULL,
    [BBIT_16TarEntra] [datetime] NULL,
    [BBIT_16TarSale] [datetime] NULL,
    [BBIT_16Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_17ManEntra] [datetime] NULL,
    [BBIT_17ManSale] [datetime] NULL,
    [BBIT_17TarEntra] [datetime] NULL,
    [BBIT_17TarSale] [datetime] NULL,
    [BBIT_17Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_18ManEntra] [datetime] NULL,
    [BBIT_18ManSale] [datetime] NULL,
    [BBIT_18TarEntra] [datetime] NULL,
    [BBIT_18TarSale] [datetime] NULL,
    [BBIT_18Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_19ManEntra] [datetime] NULL,
    [BBIT_19ManSale] [datetime] NULL,
    [BBIT_19TarEntra] [datetime] NULL,
    [BBIT_19TarSale] [datetime] NULL,
    [BBIT_19Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_20ManEntra] [datetime] NULL,
    [BBIT_20ManSale] [datetime] NULL,
    [BBIT_20TarEntra] [datetime] NULL,
    [BBIT_20TarSale] [datetime] NULL,
    [BBIT_20Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_21ManEntra] [datetime] NULL,
    [BBIT_21ManSale] [datetime] NULL,
    [BBIT_21TarEntra] [datetime] NULL,
    [BBIT_21TarSale] [datetime] NULL,
    [BBIT_21Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_22ManEntra] [datetime] NULL,
    [BBIT_22ManSale] [datetime] NULL,
    [BBIT_22TarEntra] [datetime] NULL,
    [BBIT_22TarSale] [datetime] NULL,
    [BBIT_22Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_23ManEntra] [datetime] NULL,
    [BBIT_23ManSale] [datetime] NULL,
    [BBIT_23TarEntra] [datetime] NULL,
    [BBIT_23TarSale] [datetime] NULL,
    [BBIT_23Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_24ManEntra] [datetime] NULL,
    [BBIT_24ManSale] [datetime] NULL,
    [BBIT_24TarEntra] [datetime] NULL,
    [BBIT_24TarSale] [datetime] NULL,
    [BBIT_24Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_25ManEntra] [datetime] NULL,
    [BBIT_25ManSale] [datetime] NULL,
    [BBIT_25TarEntra] [datetime] NULL,
    [BBIT_25TarSale] [datetime] NULL,
    [BBIT_25Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_26ManEntra] [datetime] NULL,
    [BBIT_26ManSale] [datetime] NULL,
    [BBIT_26TarEntra] [datetime] NULL,
    [BBIT_26TarSale] [datetime] NULL,
    [BBIT_26Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_27ManEntra] [datetime] NULL,
    [BBIT_27ManSale] [datetime] NULL,
    [BBIT_27TarEntra] [datetime] NULL,
    [BBIT_27TarSale] [datetime] NULL,
    [BBIT_27Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_28ManEntra] [datetime] NULL,
    [BBIT_28ManSale] [datetime] NULL,
    [BBIT_28TarEntra] [datetime] NULL,
    [BBIT_28TarSale] [datetime] NULL,
    [BBIT_28Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_29ManEntra] [datetime] NULL,
    [BBIT_29ManSale] [datetime] NULL,
    [BBIT_29TarEntra] [datetime] NULL,
    [BBIT_29TarSale] [datetime] NULL,
    [BBIT_29Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_30ManEntra] [datetime] NULL,
    [BBIT_30ManSale] [datetime] NULL,
    [BBIT_30TarEntra] [datetime] NULL,
    [BBIT_30TarSale] [datetime] NULL,
    [BBIT_30Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
    [BBIT_31ManEntra] [datetime] NULL,
    [BBIT_31ManSale] [datetime] NULL,
    [BBIT_31TarEntra] [datetime] NULL,
    [BBIT_31TarSale] [datetime] NULL,
    [BBIT_31Comentario] [varchar](100) COLLATE Modern_Spanish_CI_AS NULL,
 CONSTRAINT [PK_B_Bitacora] PRIMARY KEY CLUSTERED 
(
    [BBIT_IdBitacora] ASC
)WITH (PAD_INDEX  = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[B_Bitacora]  WITH CHECK ADD  CONSTRAINT [FK_B_Bitacora_B_Usuario] FOREIGN KEY([BUSU_IdUsuario])
REFERENCES [dbo].[B_Usuario] ([BUSU_IdUsuario])
GO
ALTER TABLE [dbo].[B_Bitacora] CHECK CONSTRAINT [FK_B_Bitacora_B_Usuario]

我每天都使用存储过程来填充它:

EXEC    @return_value = [dbo].[Sp_InsertBitacora]
        @BUSU_IdUsuario = 1416127,
        @BBIT_Fecha =           '2014/22/06 00:00:00.000',

        @BBIT_01ManEntra =      '2014/01/05 07:50:00.000',
        @BBIT_01ManSale =       '2014/01/05 14:05:00.000',
        @BBIT_01TarEntra =      '2014/01/05 15:55:00.000',
        @BBIT_01TarSale =       '2014/01/05 19:10:00.000',
        @BBIT_01Comentario =    null,
        @BBIT_02ManEntra =      '2014/02/05 07:51:00.000',
        @BBIT_02ManSale =       '2014/02/05 14:04:00.000',
        @BBIT_02TarEntra =      '2014/02/05 15:50:00.000',
        @BBIT_02TarSale =       '2014/02/05 19:20:00.000',
        @BBIT_02Comentario =    null,
        @BBIT_03ManEntra =      '2014/03/05 07:52:00.000',
        @BBIT_03ManSale =       '2014/03/05 14:06:00.000',
        @BBIT_03TarEntra =      '2014/03/05 15:57:00.000',
        @BBIT_03TarSale =       '2014/03/05 19:11:00.000',
        @BBIT_03Comentario =    null,
        @BBIT_04ManEntra =      '2014/04/05 07:53:00.000',
        @BBIT_04ManSale =       '2014/04/05 14:07:00.000',
        @BBIT_04TarEntra =      '2014/04/05 15:58:00.000',
        @BBIT_04TarSale =       '2014/04/05 19:13:00.000',
        @BBIT_04Comentario =    null,
        @BBIT_05ManEntra =      '2014/05/05 07:54:00.000',
        @BBIT_05ManSale =       '2014/05/05 14:00:00.000',
        @BBIT_05TarEntra =      '2014/05/05 15:59:00.000',
        @BBIT_05TarSale =       '2014/05/05 19:14:00.000',
        @BBIT_05Comentario =    null,
        @BBIT_06ManEntra =      null,
        @BBIT_06ManSale =       null,
        @BBIT_06TarEntra =      null,
        @BBIT_06TarSale =       null,
        @BBIT_06Comentario =    'Fin de Semana',
        @BBIT_07ManEntra =      null,
        @BBIT_07ManSale =       null,
        @BBIT_07TarEntra =      null,
        @BBIT_07TarSale =       null,
        @BBIT_07Comentario =    'Fin de Semana',
        @BBIT_08ManEntra =      '2014/08/05 07:58:00.000',
        @BBIT_08ManSale =       '2014/08/05 14:02:00.000',
        @BBIT_08TarEntra =      '2014/08/05 15:58:00.000',
        @BBIT_08TarSale =       '2014/08/05 19:18:00.000',
        @BBIT_08Comentario =    null,
        @BBIT_09ManEntra =      '2014/09/05 07:59:00.000',
        @BBIT_09ManSale =       '2014/09/05 14:04:00.000',
        @BBIT_09TarEntra =      '2014/09/05 15:54:00.000',
        @BBIT_09TarSale =       '2014/09/05 19:19:00.000',
        @BBIT_09Comentario =    null,
        @BBIT_10ManEntra =      '2014/10/05 07:40:00.000',
        @BBIT_10ManSale =       '2014/10/05 14:15:00.000',
        @BBIT_10TarEntra =      '2014/10/05 15:45:00.000',
        @BBIT_10TarSale =       '2014/10/05 19:20:00.000',
        @BBIT_10Comentario =    null,
        @BBIT_11ManEntra =      '2014/11/05 07:41:00.000',
        @BBIT_11ManSale =       '2014/11/05 14:11:00.000',
        @BBIT_11TarEntra =      '2014/11/05 15:46:00.000',
        @BBIT_11TarSale =       '2014/11/05 19:21:00.000',
        @BBIT_11Comentario =    null,
        @BBIT_12ManEntra =      '2014/12/05 07:50:00.000',
        @BBIT_12ManSale =       '2014/12/05 14:05:00.000',
        @BBIT_12TarEntra =      '2014/12/05 15:55:00.000',
        @BBIT_12TarSale =       '2014/12/05 19:10:00.000',
        @BBIT_12Comentario =    null,
        @BBIT_13ManEntra =      null,
        @BBIT_13ManSale =       null,
        @BBIT_13TarEntra =      null,
        @BBIT_13TarSale =       null,
        @BBIT_13Comentario =    'Fin de Semana',
        @BBIT_14ManEntra =      null,
        @BBIT_14ManSale =       null,
        @BBIT_14TarEntra =      null,
        @BBIT_14TarSale =       null,
        @BBIT_14Comentario =    'Fin de Semana',
        @BBIT_15ManEntra =      '2014/15/05 07:50:00.000',
        @BBIT_15ManSale =       '2014/15/05 14:05:00.000',
        @BBIT_15TarEntra =      '2014/15/05 15:55:00.000',
        @BBIT_15TarSale =       '2014/15/05 19:10:00.000',
        @BBIT_15Comentario =    null,
        @BBIT_16ManEntra =      '2014/16/05 07:50:00.000',
        @BBIT_16ManSale =       '2014/16/05 14:05:00.000',
        @BBIT_16TarEntra =      '2014/16/05 15:55:00.000',
        @BBIT_16TarSale =       '2014/16/05 19:10:00.000',
        @BBIT_16Comentario =    null,
        @BBIT_17ManEntra =      '2014/17/05 07:50:00.000',
        @BBIT_17ManSale =       '2014/17/05 14:05:00.000',
        @BBIT_17TarEntra =      '2014/17/05 15:55:00.000',
        @BBIT_17TarSale =       '2014/17/05 19:10:00.000',
        @BBIT_17Comentario =    null,
        @BBIT_18ManEntra =      '2014/18/05 07:50:00.000',
        @BBIT_18ManSale =       '2014/18/05 14:05:00.000',
        @BBIT_18TarEntra =      '2014/18/05 15:55:00.000',
        @BBIT_18TarSale =       '2014/18/05 19:10:00.000',
        @BBIT_18Comentario =    null,
        @BBIT_19ManEntra =      '2014/19/05 07:50:00.000',
        @BBIT_19ManSale =       '2014/19/05 14:05:00.000',
        @BBIT_19TarEntra =      '2014/19/05 15:55:00.000',
        @BBIT_19TarSale =       '2014/19/05 19:10:00.000',
        @BBIT_19Comentario =    null,
        @BBIT_20ManEntra =      null,
        @BBIT_20ManSale =       null,
        @BBIT_20TarEntra =      null,
        @BBIT_20TarSale =       null,
        @BBIT_20Comentario =    'Fin de Semana',
        @BBIT_21ManEntra =      null,
        @BBIT_21ManSale =       null,
        @BBIT_21TarEntra =      null,
        @BBIT_21TarSale =       null,
        @BBIT_21Comentario =    'Fin de Semana',
        @BBIT_22ManEntra =      '2014/22/05 07:50:00.000',
        @BBIT_22ManSale =       '2014/22/05 14:05:00.000',
        @BBIT_22TarEntra =      '2014/22/05 15:55:00.000',
        @BBIT_22TarSale =       '2014/22/05 19:10:00.000',
        @BBIT_22Comentario =    null,
        @BBIT_23ManEntra =      '2014/23/05 07:50:00.000',
        @BBIT_23ManSale =       '2014/23/05 14:05:00.000',
        @BBIT_23TarEntra =      '2014/23/05 15:55:00.000',
        @BBIT_23TarSale =       '2014/23/05 19:10:00.000',
        @BBIT_23Comentario =    null,
        @BBIT_24ManEntra =      '2014/24/05 07:50:00.000',
        @BBIT_24ManSale =       '2014/24/05 14:05:00.000',
        @BBIT_24TarEntra =      '2014/24/05 15:55:00.000',
        @BBIT_24TarSale =       '2014/24/05 19:10:00.000',
        @BBIT_24Comentario =    null,
        @BBIT_25ManEntra =      '2014/25/05 07:50:00.000',
        @BBIT_25ManSale =       '2014/25/05 14:05:00.000',
        @BBIT_25TarEntra =      '2014/25/05 15:55:00.000',
        @BBIT_25TarSale =       '2014/25/05 19:10:00.000',
        @BBIT_25Comentario =    null,
        @BBIT_26ManEntra =      '2014/26/05 07:50:00.000',
        @BBIT_26ManSale =       '2014/26/05 14:05:00.000',
        @BBIT_26TarEntra =      '2014/26/05 15:55:00.000',
        @BBIT_26TarSale =       '2014/26/05 19:10:00.000',
        @BBIT_26Comentario =    null,
        @BBIT_27ManEntra =      null,
        @BBIT_27ManSale =       null,
        @BBIT_27TarEntra =      null,
        @BBIT_27TarSale =       null,
        @BBIT_27Comentario =    'Fin de Semana',
        @BBIT_28ManEntra =      null,
        @BBIT_28ManSale =       null,
        @BBIT_28TarEntra =      null,
        @BBIT_28TarSale =       null,
        @BBIT_28Comentario =    'Fin de Semana',
        @BBIT_29ManEntra =      '2014/29/05 07:50:00.000',
        @BBIT_29ManSale =       '2014/29/05 14:05:00.000',
        @BBIT_29TarEntra =      '2014/29/05 15:55:00.000',
        @BBIT_29TarSale =       '2014/29/05 19:10:00.000',
        @BBIT_29Comentario =    null,
        @BBIT_30ManEntra =      '2014/30/05 07:50:00.000',
        @BBIT_30ManSale =       '2014/30/05 14:05:00.000',
        @BBIT_30TarEntra =      '2014/30/05 15:55:00.000',
        @BBIT_30TarSale =       '2014/30/05 19:10:00.000',
        @BBIT_30Comentario =    null,
        @BBIT_31ManEntra =      '2014/31/05 07:50:00.000',
        @BBIT_31ManSale =       '2014/31/05 14:05:00.000',
        @BBIT_31TarEntra =      '2014/31/05 15:55:00.000',
        @BBIT_31TarSale =       '2014/31/05 19:10:00.000',
        @BBIT_31Comentario =    null

SELECT  'Return Value' = @return_value

GO

问题是我需要以特定格式显示此信息,每个月的每一天都有一行:

Day | CheckinMornig |  CheckOutMonrning | CheckInAfternoon | CheckOutAfternoon | Comment

  1  |   7:00 pm   |        2:00 pm     |       4:00 pm    |        7:00 pm     | none

  2  |   7:05 pm   |        1:50 pm     |      3:55 pm     |        7:10 pm     | none

  3  |   null      |         null       |       null       |         null       | weekend

  4  |   null      |         null       |       null       |         null       | weekend

  ...

  31 |  7:05 pm    |      1:50 pm       |     3:55 pm      |       7:10 pm      |    none

我每天都尝试使用UNION

SELECT
     LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_01ManEntra, 100), 7)) as CheckInMorning
    ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_01ManSale, 100), 7)) as CheckOutMorning
    ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_01TarEntra, 100), 7)) as CheckInAfternoon
    ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_01TarSale, 100), 7)) as CheckOutAfternoon
    ,BBIT_01comentario as Comment
FROM [BitacoraTrabajo].[dbo].[B_Bitacora]
where BUSU_IdUsuario = 1416127
union
SELECT  
     LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_02ManEntra, 100), 7)) as CheckInMorning
    ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_02ManSale, 100), 7)) as CheckOutMorning
    ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_02TarEntra, 100), 7)) as CheckInAfternoon
    ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_02TarSale, 100), 7)) as CheckOutAfternoon
    ,BBIT_02comentario as Comment
FROM [BitacoraTrabajo].[dbo].[B_Bitacora]   
where BUSU_IdUsuario = 1416127   

但除了看起来像是一个非常糟糕的想法和浪费资源之外,它也不起作用,因为它将具有相同时间和周末的日期分组为空。

那么,如何使用我需要的格式获取视图或存储过程?

1 个答案:

答案 0 :(得分:0)

你可以尝试:

DECLARE @USERID INT = 1416127
DECLARE @FECHA DATETIME = '2014-06-22'

IF OBJECT_ID('dbo.TBL_DESTINATION', 'U') IS NOT NULL DROP TABLE dbo.TBL_DESTINATION

DECLARE TABLE dbo.TBL_DESTINATION (
    DAY INT, 
    CheckInMorning datetime, 
    CheckOutMorning datetime, 
    CheckInAfternoon datetime, 
    CheckOutAfternoon diatomite,
    Comentario VARCHAR(250)
)

DECLARE @DAY INT = 1
DECLARE @SQL NVARCHAR(MAX)

WHILE @DAY <= 31
BEGIN
   SET @SQL = 'INSERT INTO TBL_DESTINATION  SELECT ' + CAST(@DAY as VARCHAR) + ',
        LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_' + case when @DAY < 10 then '0' + cast(@DAY as varchar) else cast(@DAY as varchar) end + 'ManEntra, 100), 7)) as CheckInMorning
       ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_' + case when @DAY < 10 then '0' + cast(@DAY as varchar) else cast(@DAY as varchar) end + 'ManSale, 100), 7)) as CheckOutMorning
       ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_' + case when @DAY < 10 then '0' + cast(@DAY as varchar) else cast(@DAY as varchar) end + 'TarEntra, 100), 7)) as CheckInAfternoon
       ,LTRIM(RIGHT(CONVERT(VARCHAR(20), BBIT_' + case when @DAY < 10 then '0' + cast(@DAY as varchar) else cast(@DAY as varchar) end + 'TarSale, 100), 7)) as CheckOutAfternoon
       ,BBIT_' + case when @DAY < 10 then '0' + cast(@DAY as varchar) else cast(@DAY as varchar) end + 'comentario as Comment
   FROM [BitacoraTrabajo].[dbo].[B_Bitacora]
   where BUSU_IdUsuario = ' + cast(@USERID as varchar)
     ' and BBIT_Fecha = ' +  cast(@FECHA as varchar)

   EXECUTE sp_executesql @SQL

   SET @DAY = @DAY + 1
END

SELECT * FROM dbo.TBL_DESTINATION

您肯定需要定义月份结束时的日期以及执行该月份的用户的ID。您可以创建一个运行此代码并接收BUSU_IdUsuarioBBIT_Fecha值作为参数的过程。如果您将此参数放在查询结果的SELECT中,则取决于您。