从选择查询创建列

时间:2013-08-29 11:56:36

标签: sql sql-server

Sample Data

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次 enter image description here

2 个答案:

答案 0 :(得分: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'

答案 1 :(得分:0)

您只需使用“AS”重命名列

即可

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