這是我的查詢,因爲它是現在。如果ItemID等於另一個表列(BuildComponents.ComponentID)中的任何值,我想向結果檢查中添加另一列。例如。如果它在那裏寫'Y',否則寫'N'。我不希望這會影響返回的行。我使用MS SQL 2005的邏輯是這樣的:SQL,將值邏輯返回到另一個查詢字段
如果(項目ID是(從BuildComponents選擇的ComponentID) 回報 'Y' 其他 回報 'N'
SELECT
'Item' AS type,
i.ItemID,
i.ItemNumber AS prodid,
i.ItemName AS 'desc',
i.SellUnitMeasure AS unit,
i.SellUnitQuantity AS quantity,
i.VATExclusiveStandardCost AS pricein,
i.BaseSellingPrice AS priceout,
v1.VATPercentageRate AS vatRateIn,
1 AS vatTypeIn,
v1.VATCode AS VATCodeIn,
v1.VATCodeDescription AS VATCodeDescriptionIn,
v2.VATPercentageRate AS vatRateOut,
2 AS vatTypeOut,
v2.VATCode AS VATCodeOut,
v2.VATCodeDescription AS VATCodeDescriptionOut,
i.IsInactive AS inactive,
s.CardRecordID AS VendID,
i.SupplierItemNumber AS VendCode,
i.VATExclusiveStandardCost AS VendInPrice,
i.ItemDescription AS ProductNote,
i.CustomField1,
i.CustomField2,
i.CustomField3,
cl1.CustomListText AS CustomField4,
cl1.CustomListName AS CustomField4Name,
cl2.CustomListText AS CustomField5,
cl2.CustomListName AS CustomField5Name,
cl3.CustomListText AS CustomField6,
cl3.CustomListName AS CustomField6Name,
'' AS QuantityOnHand,
'' AS LocationName,
i.PriceIsInclusive,
i.ItemIsStocked,
ISNULL(l1.LocationName, ISNULL(l2.LocationName, 'Default Warehouse')) AS DefaultLocation,
i.PositiveAverageCost as cost
FROM Items i
LEFT JOIN ItemLocations il ON il.ItemID = i.ItemID AND il.ItemID IS NULL
LEFT JOIN VATCodes v2 ON v2.VATCodeID = i.SellVATCodeID
LEFT JOIN VATCodes v1 ON v1.VATCodeID = i.BuyVATCodeID
LEFT JOIN Suppliers s ON s.SupplierID = i.PrimarySupplierID
LEFT JOIN CustomLists cl1 ON cl1.CustomListID = i.CustomList1ID
LEFT JOIN CustomLists cl2 ON cl2.CustomListID = i.CustomList2ID
LEFT JOIN CustomLists cl3 ON cl3.CustomListID = i.CustomList3ID
LEFT JOIN Locations l1 ON l1.LocationID = i.DefaultSellLocationID
LEFT JOIN Locations l2 ON l2.LocationID = 1