0

样本数据

SELECT TOP 20
        TMPPO.PurchaseOrder ,
        TMPPO.LineItem ,
        ASLD.SignatureDate ,
        ASLD.SignatureTime ,
        ASLD.Operator ,
        ASLD.Variable ,
        ASLD.VariableDesc ,
        ASLD.VarNumericValue
FROM    #POAMENDMENTS TMPPO
        LEFT OUTER JOIN [SysproCompanyR].[dbo].[AdmSignatureLogDet] ASLD ON TMPPO.TransactionId = ASLD.TransactionId
                                                              AND TMPPO.SignatureDate = ASLD.SignatureDate
                                                              AND TMPPO.SignatureTime = ASLD.SignatureTime
WHERE   YEAR(TMPPO.SignatureDate) = 2013
        AND MONTH(TMPPO.SignatureDate) = 08
        AND VariableDesc IN ( 'Previous foreign price', 'Previous price',
                              'Foreign price', 'Price' )
ORDER BY PurchaseOrder ,
        LineItem 

我有下表,但不想按以下方式返回记录。在列标题变量描述下,我有外国价格、以前的外国价格、以前的价格和价格,我想将这些作为标题替换变量、变量描述和 VarNumberic。
所以例如第一行将是

Purchase Order LineItem SignatureDate SignatureTime Operator PrevFPrice FPrice PrevPrice Price
002074         0001     2013-02-23     9523598       UPOFA0  19.68      21.51   19.68    21.51
004931         0001     2013-08-09     7485253       PVWYK0  980.00     840.00  980.00   840.00  

抱歉,但很难将示例数据放在这里不知道如何...

这可能吗?

@Bummi 它为我提供了这样的数据,为什么采购订单 005331 重复了这么多次,而本质上根据原始样本数据,它根据日期和时间仅更改了 2 次 在此处输入图像描述

4

2 回答 2

1

据我了解,您正在寻找第一个查询的连接

;With CTE as
(
SELECT TOP 20 TMPPO.PurchaseOrder, TMPPO.LineItem, ASLD.SignatureDate,ASLD.SignatureTime,ASLD.Operator, ASLD.Variable, ASLD.VariableDesc, ASLD.VarNumericValue  FROM #POAMENDMENTS TMPPO
LEFT OUTER JOIN [SysproCompanyR].[dbo].[AdmSignatureLogDet] ASLD ON TMPPO.TransactionId = ASLD.TransactionId and TMPPO.SignatureDate = ASLD.SignatureDate and TMPPO.SignatureTime = ASLD.SignatureTime   
WHERE YEAR(TMPPO.SignatureDate) = 2013
and MONTH(TMPPO.SignatureDate) = 08 
and VariableDesc IN ('Previous foreign price','Previous price','Foreign price','Price')
ORDER BY PurchaseOrder, LineItem  
)
Select c1.PurchaseOrder,c1.LineItem,c1.SignatureDate,c1.SignatureTime,c1.Operator
,c1.VarNumericValue as [Previous foreign price]
,c2.VarNumericValue as [Previous price]
,c3.VarNumericValue as [Foreign price]
,c4.VarNumericValue as [Price]

FROM CTE c1
JOIN CTE c2 on c2.PurchaseOrder=c1.PurchaseOrder and c2.VariableDesc='Previous price'    
               and c2.LineItem=c1.LineItem and c2.SignatureDate=c1.SignatureDate and c2.SignatureTime=c1.SignatureTime
JOIN CTE c3 on c3.PurchaseOrder=c1.PurchaseOrder and c3.VariableDesc='Foreign price'    
               and c3.LineItem=c1.LineItem and c3.SignatureDate=c1.SignatureDate and c3.SignatureTime=c1.SignatureTime
JOIN CTE c4 on c4.PurchaseOrder=c1.PurchaseOrder and c4.VariableDesc='Price'    
               and c4.LineItem=c1.LineItem and c4.SignatureDate=c1.SignatureDate and c4.SignatureTime=c1.SignatureTime
Where c1.VariableDesc='Previous foreign price'
于 2013-08-29T12:31:01.360 回答
0

您可以使用“AS”重命名列

选择一些东西作为其他东西

于 2013-08-29T12:27:08.407 回答