2013-11-14 98 views
0

我有2個表和一個存儲過程來顯示它們的數據。SELECT語句中的動態列標題

表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時將ActivityDate列名稱放在ActivityDateType的選擇結果中。

因此,舉例來說,如果我選擇的只是結果1的,我想它是:

ResultName DateType1 ID 

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

我試圖把它放在一個變量,同時加入表一起,然後使用STR()與 ActivityDate爲STR(@ActivityDateType)

,但它不工作(可能是顯而易見的原因,我沒有得到)

沒有人有任何想法的功能?

非常感謝

+0

如果「Result1」有兩個不同的ActivityDateType「DateType1」和「DatetYpe2」,你可以將它們顯示爲列和它下面的正確值,ri ght? –

+0

ResultDetail表每個ResultName有1行,沒有重複,因此它們只會有1個ActivityDateType – user2992750

+0

需要動態SQL和PIVOT。如果你想爲多個結果名稱過濾結果,發佈的解決方案將覆蓋該結果。 –

回答

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 

這是因爲你解釋,返回數據的最終查詢。

注意,我選擇返回的結果爲 「結果1」 和 「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, '[email protected]_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 ('[email protected]_PvtIn+N'))pvt 
     )qry 
    ' 
    PRINT @SQL 
    EXECUTE sp_executesql @SQL 

    SET NOCOUNT OFF 

這是上面查詢的結果

enter image description here