SELECT语句中的动态列标题

时间:2013-11-14 15:51:03

标签: sql sql-server variables dynamic

我有2个表和一个存储过程来显示来自它们的数据。

表1是结果,如下所示:

ResultName    ActivityDate     ID

Result1       2013-11-14       1
Result1       2013-11-14       2
Result2       2013-11-14       3
Result3       2013-11-14       4

表2是 ResultDetail ,如下所示:

ResultName    ActivityDateType

Result1       DateType1
Result2       DateType1
Result3       DateType2       

当我运行我的存储过程从中选择数据时,我目前有类似的东西:

ResultName    ActivityDateType    ActivityDate    ID

Result1       DateType1           2013-11-14      1  
Result1       DateType1           2013-11-14      2  
Result2       DateType1           2013-11-14      3  
Result3       DateType2           2013-11-14      4  

当我选择某个ResultName时,我想在ActivityDateType的select结果中建立ActivityDate列名。

例如,如果我只选择了Result1,我希望它是:

ResultName    DateType1    ID

Result1       2013-11-14   1
Result1       2013-11-14   2

我试图将它们放在一个变量中,同时将表连接在一起,然后使用str()函数     ActivityDate为str(@ActivityDateType)

但它不起作用(可能是因为我没有得到明显的原因)

有没有人有任何想法?

非常感谢

1 个答案:

答案 0 :(得分:0)

这是在准备桌子。

    SET NOCOUNT ON
    IF OBJECT_ID('tempdb..#result') IS NOT NULL
        DROP TABLE #result
    CREATE TABLE #result
    (   
        resultname      sysname NOT NULL
        ,ActivityDate   DATE NOT NULL
        ,id             INT NOT NULL
    )
    IF OBJECT_ID('tempdb..#resultdetail') IS NOT NULL
        DROP TABLE #resultdetail
    CREATE TABLE #resultdetail
    (   
        resultname          sysname NOT NULL
        ,ActivityDateType   sysname NOT NULL
    )

    INSERT INTO #result( resultname, ActivityDate, id )
                SELECT 'Result1','11/14/2013',1
    UNION ALL   SELECT 'Result1','11/14/2013',2
    UNION ALL   SELECT 'Result2','11/14/2013',3
    UNION ALL   SELECT 'Result3','11/14/2013',4

    INSERT INTO #resultdetail( resultname, ActivityDateType )
                SELECT 'Result1','DateType1'
    UNION ALL   SELECT 'Result2','DateType1'
    UNION ALL   SELECT 'Result3','DateType2'

    SET NOCOUNT OFF

这是最终查询,按照您的解释返回数据。

请注意,我已选择返回“Result1”和“Result3”的结果

    SET NOCOUNT ON

    IF OBJECT_ID('tempdb..#DynamicColumn_Select') IS NOT NULL
        DROP TABLE #DynamicColumn_Select
    CREATE TABLE #DynamicColumn_Select
    (
        ColumnName      sysname NOT NULL PRIMARY KEY
    )

    /*Prepare the user input for which ResultName we need to generate Final Results*/
    INSERT INTO #DynamicColumn_Select( ColumnName )
    SELECT 'Result1' UNION ALL SELECT 'Result3'

    DECLARE @CsvDynamicColumn_Select NVARCHAR(MAX)=''
            ,@CsvDynamicColumn_PvtIn NVARCHAR(MAX)=''
            ,@SQL NVARCHAR(MAX)=''

    /*Prepare the Dynamic PIVOT IN Column List as CSV*/
    SELECT @CsvDynamicColumn_PvtIn = 
            CASE WHEN @CsvDynamicColumn_PvtIn = '' 
                THEN '' 
                ELSE @CsvDynamicColumn_PvtIn + ',' 
            END
            + N'['+ActivityDateType+N']'
    FROM
    (
        Select DISTINCT t1.ActivityDateType
        FROM #resultdetail t1
        WHERE resultname IN 
            (SELECT ColumnName FROM #DynamicColumn_Select)
    )qry


    /*Prepare the Dynamic Select Column list as CSV*/
    SELECT @CsvDynamicColumn_Select = 
            CASE WHEN @CsvDynamicColumn_Select = '' 
                THEN '' 
                ELSE @CsvDynamicColumn_Select + ',' 
            END
            + ActivityDateType
    FROM
    (
        Select DISTINCT t1.ActivityDateType
        FROM #resultdetail t1
        WHERE resultname IN 
            (SELECT ColumnName FROM #DynamicColumn_Select)
    )qry

    PRINT @CsvDynamicColumn_PvtIn
    PRINT @CsvDynamicColumn_Select


    SET @SQL = N'
        SELECT resultname,id, '+ @CsvDynamicColumn_Select
        +N'
        FROM
        (
            SELECT resultname,id, '+@CsvDynamicColumn_Select
            +N'
            FROM
            (
                SELECT t1.resultname,t2.ActivityDateType,t1.ActivityDate,t1.id
                FROM #result t1
                JOIN #resultdetail t2
                    ON t1.resultname=t2.resultname
                Where t1.resultname in (Select ColumnName From #DynamicColumn_Select)
            )q1 
            PIVOT
            (MAX(ActivityDate) FOR ActivityDateType IN ('+@CsvDynamicColumn_PvtIn+N'))pvt
        )qry
    '
    PRINT @SQL
    EXECUTE sp_executesql @SQL

    SET NOCOUNT OFF

这是上述查询的结果

enter image description here