0

我在下面有这个查询

select * from (
    SELECT tjdm.ID as id,  cjb.CORPORATE_ID, 
        tjdm.JOB_TICKET_VALUE, cjb.DISPLAY_NAME
        from Cjd cjb
        join Tjdm tjdm 
        on cjb.CUSTOM_JOB_DATA_ID = tjdm.CUSTOM_JOB_DATA_ID
        where cjb.CUSTOM_DATA_TYPE in (1,2) and cjb.DISPLAY_IS_ACTIVE = 1
    ) AS PivotData
PIVOT (
    max(JOB_TICKET_VALUE)
    FOR DISPLAY_NAME IN
        ([OMXAccount],[Consignee],[Col1],[Col2],[Col3],[Col4])
) AS PivotTable

返回一个数据列,select * from (.....)如下所示。

id  CORPORATE_ID  JOB_TICKET_VALUE  DISPLAY_NAME
--  ------------  ----------------  ------------
53  1             9                 OMXAccount
53  1             199               Consignee
54  6             "No value"        OMXAccount
54  6             "No value"        Consignee
58  1             "No value"        OMXAccount
58  1             "No value"        Consignee

但是当执行“PIVOT”语句时,我得到了这个结果。

id  CORPORATE_ID  OMXAccount  Consignee  [Col1]  [Col2]  [Col3]  [Col4]
--  ------------  ----------  ---------  ------  ------  ------  ------
53  1             9           199        Null    Null    Null    Null
54  6                                    Null    Null    Null    Null
58  1                                    Null    Null    Null    Null

预期的输出应该如下所示。

id  CORPORATE_ID  OMXAccount  Consignee  [Col1]  [Col2]  [Col3]  [Col4]
--  ------------  ----------  ---------  ------  ------  ------  ------
53  1             OMXAccount  Consignee  Null    Null    Null    Null
54  6             OMXAccount  Consignee  Null    Null    Null    Null
58  1             OMXAccount  Consignee  Null    Null    Null    Null

怎么了?

谢谢

RJuy

4

1 回答 1

0
select * from (
    SELECT tjdm.ID as id,  cjb.CORPORATE_ID, 
        cjb.DISPLAY_NAME
        from Cjd cjb
        join Tjdm tjdm 
        on cjb.CUSTOM_JOB_DATA_ID = tjdm.CUSTOM_JOB_DATA_ID
        where cjb.CUSTOM_DATA_TYPE in (1,2) and cjb.DISPLAY_IS_ACTIVE = 1
    ) AS PivotData
PIVOT (
    max(DISPLAY_NAME)
    FOR DISPLAY_NAME IN
        ([OMXAccount],[Consignee],[Col1],[Col2],[Col3],[Col4])
) AS PivotTable
于 2012-12-10T06:26:20.217 回答