2016-10-05 57 views
1

我想在我的查詢中使用此:Max在SQL服務器

MAX(CASE WHEN year = 2014 THEN remark ELSE '-' END) 

表樣本:

EmpID Year Remark 
1  2016 Normal 
1  2014 Good 
1  2015 Very Good 
1  2016 Good 
1  2010 Poor 
1  2016 Well 

報告我得到:

EmpID 2010 2014 2015  2016 
1  Poor Good Very Good Normal 

報告其實我是想:

EmpID 2010 2014 2015   2016 
1  Poor Good Very Good Normal - Good - Well 

當我打電話查詢我只得到一行,但我想連接各年的所有行。

怎麼辦?

預先感謝

+0

你想一年列是動態或靜態的? –

+1

我已經得到這個動態使用不同的一年查詢但那不是問題 – Pravin

回答

4

這是無關的MAX,因爲你可以使用旋轉+ STUFF與FOR XML PATH:

SELECT * 
FROM (
    SELECT DISTINCT t.EmpID, 
      t.[Year], 
      STUFF((
      SELECT '-'+Remark 
      FROM YourTable 
      WHERE t.EmpID = EmpID AND t.[Year] = [Year] 
      FOR XML PATH('') 
      ),1,1,'') Remark 
    FROM YourTable t 
) as d 
PIVOT (
    MAX(Remark) FOR [Year] IN ([2010],[2014],[2015],[2016]) 
) as pvt 

d部分您將獲得:

EmpID Year Remark 
1  2010 Poor 
1  2014 Good 
1  2015 Very Good 
1  2016 Normal-Good-Well 
  • STUFF用於去除第一-

    用於從行使值作爲1行
  • FOR XML PATH

  • PIVOT到行轉換成列

輸出:

EmpID 2010 2014 2015  2016 
1  Poor Good Very Good Normal-Good-Well 

EDIT#1

與MAX的另一種方式和CASE WHEN(相同的輸出):

SELECT EmpID, 
     MAX(CASE WHEN [Year] = 2010 THEN Remark END) as [2010], 
     MAX(CASE WHEN [Year] = 2014 THEN Remark END) as [2014], 
     MAX(CASE WHEN [Year] = 2015 THEN Remark END) as [2015], 
     MAX(CASE WHEN [Year] = 2016 THEN Remark END) as [2016] 
FROM (
    SELECT DISTINCT t.EmpID, 
      t.[Year], 
      STUFF((
      SELECT '-'+Remark 
      FROM YourTable 
      WHERE t.EmpID = EmpID AND t.[Year] = [Year] 
      FOR XML PATH('') 
      ),1,1,'') Remark 
    FROM YourTable t 
) as d 
GROUP BY EmpID 

編輯#2

動態SQL:

DECLARE @sql nvarchar(max), 
     @col nvarchar(max) 

SELECT @col = (
    SELECT DISTINCT ','+QUOTENAME([Year]) 
    FROM YourTable 
    FOR XML PATH('') 
) 

SELECT @sql = N' 
SELECT * 
FROM (
    SELECT DISTINCT t.EmpID, 
      t.[Year], 
      STUFF((
      SELECT ''-''+Remark 
      FROM YourTable 
      WHERE t.EmpID = EmpID AND t.[Year] = [Year] 
      FOR XML PATH('''') 
      ),1,1,'''') Remark 
    FROM YourTable t 
) as d 
PIVOT (
    MAX(Remark) FOR [Year] IN ('+STUFF(@col,1,1,'')+') 
) as pvt' 

EXEC sp_executesql @sql 
+1

最好的答案,因爲這說明使用'STUFF','XML'和'PIVOT' – GauravKP

+0

感謝答覆 – Pravin

+0

它爲我工作,但還有一兩件事,如果我的想在備註欄附上多列像重視假設此言+「‘+級 - 備註+’」 +級是有可能 – Pravin

0

我想你可以連接你的字符串這樣的:

SELECT T.EmpID 
    , T.[Year] 
    , STUFF(T2.[Remark], 1, 3, '') AS Remark 
FROM (SELECT DISTINCT EmpID, [Year] FROM #Test) AS T 
CROSS APPLY (
    SELECT CONCAT(' - ', T2.Remark) 
    FROM #Test AS T2 
    WHERE T2.EmpID = T.EmpID 
     AND T2.[Year] = T.[Year] 
    FOR XML PATH ('') 
    ) AS T2(Remark); 

查詢abo VE將在輸出這樣的:

╔═══════╦══════╦══════════════════════╗ 
║ EmpID ║ Year ║  Remark  ║ 
╠═══════╬══════╬══════════════════════╣ 
║  1 ║ 2010 ║ Poor     ║ 
║  1 ║ 2014 ║ Good     ║ 
║  1 ║ 2015 ║ Very Good   ║ 
║  1 ║ 2016 ║ Normal - Good - Well ║ 
╚═══════╩══════╩══════════════════════╝ 

現在你只需要它的基礎上EmpIDYear值,你已經知道該怎麼做,根據您的意見轉動。

+0

感謝您的回覆 – Pravin

1

可以查詢這樣

;WITH cte 
AS (SELECT 
    empid, year, 
    STUFF((SELECT ' - ' + remark FROM #table WHERE empid = t.empid AND year = t.year 
    FOR xml PATH ('')), 1, 3, '') AS testRemark 
FROM #table t 
GROUP BY empid, year) 
SELECT * 
FROM cte 
PIVOT (MAX(testRemark) FOR year IN ([2010], [2014], [2015], [2016])) p 

輸出:

empid 2010 2014 2015  2016 
1  Poor Good Very Good Normal - Good - Well 
+0

感謝您的回覆 – Pravin