在SQL中将一列数据显示为两列

时间:2015-01-23 06:02:39

标签: sql-server sql-server-2008 tsql sql-server-2008-r2 sql-server-2012

我已在表格中保存了语言字符串资源。对于每个文化代码,这些都保存为行 如下图所示:

 ---------------------------------------------
  CULTURE      KEYFIELD                 VALUEFIELD
 ----------------------------------------------

   en-US       AadhaarUID              Aadhaar UID
   en-US       Abbreviation            Abbreviation
   en-US       Abbreviation_CD         Abbreviation
   en-US       Abbreviation_DM         Abbreviation
   en-US       AboutPortal             About Portal
   hi-IN       AadhaarUID              आधार यूआईडी नंबर लिखें
   hi-IN       Abbreviation            संक्षिप्त
   hi-IN       Abbreviation_CD         संक्षिप्त
   hi-IN       Abbreviation_DM         संक्षिप्त
   hi-IN       AboutPortal             पोर्टल के बारे में

我想将列ValueField显示为两列一列用于英语,一列用于印地语。每种文化的行数相等。PFFIELD仅来自hi-IN

表示输出应为:

   -------------------------------------------------------
     PKFIELD   ENGLISH                         HINDI
    ------------------------------------------------------
        2       Aadhaar UID                 आधार यूआईडी नंबर लिखें
        4       Abbreviation                 संक्षिप्त
        6       Abbreviation                 संक्षिप्त
        8       Abbreviation                 संक्षिप्त
        10       About Portal                  पोर्टल के बारे में

如何在SQL中执行此操作?

由于

4 个答案:

答案 0 :(得分:2)

试试这个:

SELECT
    PKField = MAX(CASE WHEN Culture = 'hi-IN' THEN PKField END),
    English = MAX(CASE WHEN Culture = 'en-US' THEN ValueField END),
    Hindi = MAX(CASE WHEN Culture = 'hi-IN' THEN ValueField END) 
FROM temp
GROUP BY KeyField

答案 1 :(得分:1)

为了更好的衡量,你也可以转动 - 这对更多的语言更有用:

SELECT [KEYFIELD], [en-US], [hi-IN]
FROM Abbreviations
PIVOT
(
  MAX([VALUEFIELD])
  FOR [CULTURE] IN ([en-US], [hi-IN])
) y;

SqlFiddle here

答案 2 :(得分:0)

SELECT  [English] = E.[ValueFiled]
        ,[Hindi] = H.[ValueFiled]
FROM    [TableName] E
INNER JOIN (SELECT * FROM [TableName] H WHERE H.[Culture] = 'hi-IN') H
        ON E.[KeyField] = H.[KeyField]
WHERE   E.[Culture] = 'en-US'

答案 3 :(得分:0)

select a.valuefield as "ENGLISH", b.valuefield as "HINDI"
from tab a, tab b
where
    a.keyfield = b.keyfield
    and a.culture = 'en-US'
    and b.culture = 'hi-IN'
;