1

我的结果集如下:

MinID       MaxID   ColumnName
----------  ------- -------------------
167         34458   AccountManager
174         174     ClientDeliveryDate
184         184     LocalContactEmail
192         34459   ProjectDeveloper
194         194     ServiceType

我想要一个结果集,如:

AccountManager ClientDeliveryDate LocalContactEmail ProjectDeveloper ServiceType
-------------- ------------------ ----------------- ---------------- -----------
167            174                184               192              194
34458          174                184               34459            194
4

2 回答 2

7
select [AccountManager], [ClientDeliveryDate], [LocalContactEmail],[ProjectDeveloper] ,[ServiceType]
from
(
    SELECT ColumnName, TestType, score
    FROM 
    (
        SELECT ColumnName, MinID, MaxID  from table_name
    ) PivotData
    UNPIVOT
    (
        score for TestType IN (MinID, MaxID)
    ) as initialUnPivot
) as PivotSource
PIVOT 
(
MIN(score) FOR ColumnName IN ([AccountManager], [ClientDeliveryDate], [LocalContactEmail],[ProjectDeveloper] ,[ServiceType])
) AS PivotTable
order by [AccountManager] desc

SQL 小提琴

于 2013-05-10T08:45:51.323 回答
-1

试试这个查询。

SELECT 
max(case when `ColumnName` = 'AccouontManager'  then `MaxID` end) as AccouontManager, 
max(case when `ColumnName` = 'ClientDeliveryDate'  then `MaxID` end) as ClientDeliveryDate,
max(case when `ColumnName` = 'LocalContactEmail'  then `MaxID` end) as LocalContactEmail,
max(case when `ColumnName` = 'ProjectDeveloper'  then `MaxID` end) as ProjectDeveloper,
max(case when `ColumnName` = 'ServiceType'  then `MaxID` end) as ServiceType
FROM `your_table` 
union all 
SELECT 
max(case when `ColumnName` = 'AccouontManager'  then `MinID` end) as AccouontManager, 
max(case when `ColumnName` = 'ClientDeliveryDate'  then `MinID` end) as ClientDeliveryDate,
max(case when `ColumnName` = 'LocalContactEmail'  then `MinID` end) as LocalContactEmail,
max(case when `ColumnName` = 'ProjectDeveloper'  then `MinID` end) as ProjectDeveloper,
max(case when `ColumnName` = 'ServiceType'  then `MinID` end) as ServiceType
FROM `your_table` 
于 2013-05-10T08:46:15.770 回答