English 中文(简体)
不同格式的显示台
原标题:Display table in different format
  • 时间:2011-07-06 03:14:26
  •  标签:
  • sql-server

i have the sql code that returns the result

Set1   Value
A       A1      
A       A2
A       A3
A       A4

如何显示类似结果

Set1    Value1      Value2       Value3      Value4
A           A1        A2           A3          A4

增 编

问题回答

接下来是trick。 使用<代码>ROW_NUMBER 允许除<代码>A1外的数值。

SQL Statement

SELECT  [Set1] = Set1
        , [Value1] = MIN(CASE WHEN rn = 1 THEN Value END)
        , [Value2] = MIN(CASE WHEN rn = 2 THEN Value END)
        , [Value3] = MIN(CASE WHEN rn = 3 THEN Value END)
        , [Value4] = MIN(CASE WHEN rn = 4 THEN Value END)
FROM    (       
            SELECT  Set1
                    , Value
                    , rn = ROW_NUMBER() OVER (PARTITION BY Set1 ORDER BY Value)
            FROM    AResult
        ) q
GROUP BY
        Set1        




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签