2012-06-16 118 views
0

我想添加一個額外的字段到我的查詢將具有值'現金'或'檢查'根據字段(CheckNum)是否返回一個檢查號碼與否。SQL語句添加字段並填充基於另一個字段的值

SELECT t1.DocNum, t0.DocDate, t0.CardName, t0.CardCode, t1.CheckNum, t0.DocTotal 
FROM OVPM t0 INNER JOIN 
VPM1 t1 ON t1.docnum = t0.docnum LEFT OUTER JOIN 
OACT t2 ON t2.AcctCode = t1.CheckAct 

UNION ALL 

SELECT t1.DocNum, t0.DocDate, t0.CardName, t0.CardCode, t1.CheckNum, t0.DocTotal 
FROM ORCT t0 INNER JOIN 
     RCT1 t1 ON t1.docnum = t0.docnum LEFT OUTER JOIN 
     OACT t2 ON t2.AcctCode = t1.CheckAct 

以上查詢樣品結果:

          CheckNum  DocTotal 
3 Alibhai shariff & Sons Ltd S0003  67901  39663.000000 
5 Silverstone Tyres (K) Ltd S0191  0   93200.000000 
6 Lam Limited     S0167  1372   66380.000000 
7 Osho Chemicals Industries S0198  0   21100.000000 

我想什麼:

          CheckNum  DocTotal   PayType 
3 Alibhai shariff & Sons Ltd S0003  67901  39663.000000  Check 
5 Silverstone Tyres (K) Ltd S0191  0   93200.000000  Cash 
6 Lam Limited     S0167  1372   66380.000000  Check 
7 Osho Chemicals Industries S0198  0   21100.000000  Cash 

任何幫助表示讚賞。

回答

1

您可以在這裏使用CASE。像這樣 -

SELECT t1.DocNum, t0.DocDate, t0.CardName, t0.CardCode, t1.CheckNum, t0.DocTotal, 
     CASE WHEN t1.CheckNum = 0 THEN 'Cash' ELSE 'Check' END AS PayType 
FROM OVPM t0 INNER JOIN 
     VPM1 t1 ON t1.docnum = t0.docnum LEFT OUTER JOIN 
     OACT t2 ON t2.AcctCode = t1.CheckAct 

UNION ALL 

SELECT t1.DocNum, t0.DocDate, t0.CardName, t0.CardCode, t1.CheckNum, t0.DocTotal, 
     CASE WHEN t1.CheckNum = 0 THEN 'Cash' ELSE 'Check' END AS PayType 
FROM ORCT t0 INNER JOIN 
     RCT1 t1 ON t1.docnum = t0.docnum LEFT OUTER JOIN 
     OACT t2 ON t2.AcctCode = t1.CheckAct 
+0

感謝您的留言。 –

相關問題