SQL中的动态数据透视表用于多列

时间:2016-08-20 16:11:17

标签: sql sql-server ssis

我在SSIS中运行作业的查询

SELECT
    CONVERT(varchar(12), GETDATE(), 110) AS Date
,   j.name AS job_name
,   CONVERT(varchar(10), ja.run_requested_date, 108) AS Start_Time
,   CONVERT(varchar(10), CONVERT(datetime, RTRIM(19000101)) 
        + (jh.run_duration * 9 + jh.run_duration % 10000 * 6
        + jh.run_duration % 100 * 10) / 216e4, 108) AS run_duration
,   (ja.run_requested_date + (CONVERT(varchar(10), CONVERT(datetime, RTRIM(19000101)) 
    + (jh.run_duration * 9
    + jh.run_duration
    % 10000 * 6
    + jh.run_duration % 100
    * 10) / 216e4, 108))) AS Completion_Time
FROM
    (msdb.dbo.sysjobactivity ja
        LEFT JOIN msdb.dbo.sysjobhistory jh
        ON ja.job_history_id = jh.instance_id
    )
    JOIN msdb.dbo.sysjobs_view j
    ON ja.job_id = j.job_id
WHERE
    ja.session_id = (
                        SELECT
                            MAX(session_id)
                        FROM
                            msdb.dbo.sysjobactivity
                    )
    AND j.name IN ('a', 'b', 'c', 'd', 'e');

我希望此查询作为附加图像进行旋转。 此日期列也需要从月初到日期。

enter image description here

3 个答案:

答案 0 :(得分:0)

这可能会有所帮助

CREATE VIEW vwJobs
AS
    SELECT  CONVERT(VARCHAR(12), GETDATE(), 110) AS Date ,
            j.name AS job_name ,
            CONVERT(VARCHAR(10), ja.run_requested_date, 108) AS Start_Time ,
            CONVERT(VARCHAR(10), CONVERT(DATETIME, RTRIM(19000101))
            + ( jh.run_duration * 9 + jh.run_duration % 10000 * 6
                + jh.run_duration % 100 * 10 ) / 216e4, 108) AS run_duration ,
            ( ja.run_requested_date
              + ( CONVERT(VARCHAR(10), CONVERT(DATETIME, RTRIM(19000101))
                  + ( jh.run_duration * 9 + jh.run_duration % 10000 * 6
                      + jh.run_duration % 100 * 10 ) / 216e4, 108) ) ) AS Completion_Time
    FROM    ( msdb.dbo.sysjobactivity ja
              LEFT JOIN msdb.dbo.sysjobhistory jh ON ja.job_history_id = jh.instance_id
            )
            JOIN msdb.dbo.sysjobs_view j ON ja.job_id = j.job_id
    WHERE   ja.session_id = ( SELECT    MAX(session_id)
                              FROM      msdb.dbo.sysjobactivity
                            )
            AND j.name IN ( 'a', 'b', 'c', 'd', 'e' );

GO

--QUERY
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX);
DECLARE @ColumnNamesInPivot AS NVARCHAR(MAX);

--Get distinct values of PIVOT Column 
SELECT TOP 100 PERCENT
        @ColumnNamesInPivot = ISNULL(@ColumnNamesInPivot + ',', '')
        + QUOTENAME([Date])
FROM    ( SELECT    DISTINCT
                    [Date]
          FROM      vwJobs
        ) AS P
ORDER BY [Date];

--Prepare the PIVOT query using the dynamic query
SELECT  @DynamicPivotQuery = N'Select job_name, ''Start_Time'' Time,'
        + @ColumnNamesInPivot + ' 
            FROM    ( SELECT * 
          FROM      vwJobs
        ) AS SourceTable PIVOT( MAX(Start_Time) FOR [Date] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable
    UNION ALL
    Select job_name, ''Duration'' Time,' + @ColumnNamesInPivot + ' 
            FROM    ( SELECT * 
          FROM      vwJobs
        ) AS SourceTable PIVOT( MAX(run_duration) FOR [Date] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable
    UNION ALL
    Select job_name, ''End_Time'' Time,' + @ColumnNamesInPivot + ' 
            FROM    ( SELECT * 
          FROM      vwJobs
        ) AS SourceTable PIVOT( MAX(Completion_Time) FOR [Date] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable    
        Order by job_name
        ';

--SELECT  @DynamicPivotQuery;
EXEC sp_executesql @DynamicPivotQuery;

答案 1 :(得分:0)

如果您有一个月(包括年份)列,然后是31列,这可能会更简单。你不需要任何动态的东西。如果你有一个动态列名,你甚至会对SSIS中的那些做什么?

答案 2 :(得分:0)

更新:   - 在视图中选择GetDate()作为日期列。   - 更新了PIVOT查询,以便不返回多行。

所以,这是更新查询:

CREATE VIEW vwJobs
AS
    SELECT  CONVERT(VARCHAR(12), run_requested_date, 110) AS Date ,
            j.name AS job_name ,
            CONVERT(VARCHAR(10), ja.run_requested_date, 108) AS Start_Time ,

            CONVERT(VARCHAR(10), CONVERT(DATETIME, RTRIM(19000101))
            + ( jh.run_duration * 9 + jh.run_duration % 10000 * 6
                + jh.run_duration % 100 * 10 ) / 216e4, 108) AS run_duration ,

            ( ja.run_requested_date
              + ( CONVERT(VARCHAR(10), CONVERT(DATETIME, RTRIM(19000101))
                  + ( jh.run_duration * 9 + jh.run_duration % 10000 * 6
                      + jh.run_duration % 100 * 10 ) / 216e4, 108) ) ) AS Completion_Time
    FROM    ( msdb.dbo.sysjobactivity ja
              LEFT JOIN msdb.dbo.sysjobhistory jh ON ja.job_history_id = jh.instance_id
            )
            JOIN msdb.dbo.sysjobs_view j ON ja.job_id = j.job_id
            JOIN ( SELECT   CONVERT(DATE, run_requested_date) date ,
                        MAX(session_id) session_id
               FROM     msdb.dbo.sysjobactivity
               WHERE    run_requested_date IS NOT NULL
               GROUP BY CONVERT(DATE, run_requested_date)
             ) ss ON ja.session_id = ss.session_id

            AND j.name IN ( 'a', 'b', 'c', 'd', 'e' );

GO


--QUERY
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX);
DECLARE @ColumnNamesInPivot AS NVARCHAR(MAX);

--Get distinct values of PIVOT Column 
SELECT TOP 100 PERCENT
        @ColumnNamesInPivot = ISNULL(@ColumnNamesInPivot + ',', '')
        + QUOTENAME([Date])
FROM    ( SELECT    DISTINCT
                    [Date]
          FROM      vwJobs
        ) AS P
ORDER BY [Date];

--Prepare the PIVOT query using the dynamic query
SELECT  @DynamicPivotQuery = N'Select job_name, ''Start_Time'' Time,'
        + @ColumnNamesInPivot + ' 
            FROM    ( SELECT Date, job_name, Start_Time
          FROM      vwJobs
        ) AS SourceTable PIVOT( MAX(Start_Time) FOR [Date] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable
    UNION ALL
    Select job_name, ''Duration'' Time,' + @ColumnNamesInPivot + ' 
            FROM    ( SELECT Date, job_name, run_duration 
          FROM      vwJobs
        ) AS SourceTable PIVOT( MAX(run_duration) FOR [Date] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable
    UNION ALL
    Select job_name, ''End_Time'' Time,' + @ColumnNamesInPivot + ' 
            FROM    ( SELECT Date, job_name, Completion_Time  
          FROM      vwJobs
        ) AS SourceTable PIVOT( MAX(Completion_Time) FOR [Date] IN ('
        + @ColumnNamesInPivot + ') ) AS PVTTable    
        Order by job_name
        ';

--PRINT  @DynamicPivotQuery;
EXEC sp_executesql @DynamicPivotQuery;

我当地的结果: enter image description here