2013-08-29 44 views
0

Sample DataSELECT查詢

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複製所以很多時候在本質上根據原始樣本數據是根據日期和時間 enter image description here

+0

Peraphs你想爲別名使用別名嗎? –

+0

這實際上與MySQL沒有任何關係嗎? – Strawberry

回答

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' 
+0

我把結果作爲屏幕截圖發送到頂端。其複製一些數據... – user1546143

+0

因此,例如PO 005331必須更改1. @時間9530687從PreviousForeign價格85至114,然後@時間9533838從PreviousForeign價格114更改爲28.61,並且某些您的返回結果不相符。 – user1546143

+0

@ user1546143對不起,沒有仔細閱讀您的數據,忘記了連接的一些條件。 – bummi

0

改變只有2次你可以使用「AS」重命名列

選擇一些東西AS somethingelse