我有一个订单和订单行表,我希望能够从订单表中选择一些记录到临时表中。这很容易,困难的部分是我想在特定产品中使用特定产品时选择临时表" order"。这是我写的查询,但我无法使用它,因为它会为每个订单返回多个结果,具体取决于存在多少个订单行。 任何人都可以帮忙吗?我只想查看订单是否包含特定或特定产品:
DECLARE @Begin_Date DATETIME; SET @Begin_Date = '2015-04-01'
DECLARE @End_Date DATETIME; SET @End_Date = '2015-06-30'
SELECT o.Location_Code,
o.Order_Number,
o.Order_Date,
o.OrderIdealFoodCost
o.OrderFinalPrice,
CASE WHEN ol.ProductCode IN ('172352','172353','172355','172357','172360','172422','172429','172343','172344','172346','172348','172351','172427','172428') THEN 1 ELSE 0 END AS Promo
INTO #OrderCostPrice
FROM Order_Lines ol (NOLOCK)
Inner Join Orders o (NOLOCK) on ol.Location_Code = o.Location_Code
and ol.Order_Date = o.Order_Date
and ol.Order_Number = o.Order_Number
WHERE o.Order_Date BETWEEN @Begin_Date AND @End_Date
and o.Order_Status_Code <= 98
and ol.Deleted = 0
订单表:
Location_Code|Order_Number|Order_Date|OrderIdealFoodCost|OrderFinalPrice|Order_Status_Code
30542 |1 |2015-01-01| 5.21 | 21.25 |4
30548 |1 |2015-01-01| 8.25 | 58.26 |4
订单行表:
Location_Code|Order_Number|Order_Date|Order_Line_Number|Order_Product|Deleted
30542 |1 |2015-01-01|1 |172352 |0
30542 |1 |2015-01-01|2 |72352 |0
30542 |1 |2015-01-01|3 |72355 |0
30542 |1 |2015-01-01|4 |72358 |0
30548 |1 |2015-01-01|1 |72352 |0
30548 |1 |2015-01-01|2 |72358 |0
30548 |1 |2015-01-01|3 |72359 |0
期望的结果:
Location_Code|Order_Date|Order_Number|OrderIdealFoodCost|OrderFinalPrice|Promo
30542 |2015-01-01|1 |5.21 |21.25 |1
30542 |2015-01-01|1 |8.25 |58.26 |0
有什么想法吗?
答案 0 :(得分:0)
如果我理解正确,请在exists
子句中使用where
而不是join
。您的列似乎都来自orders
表:
SELECT o.*
FROM Orders o (NOLOCK)
WHERE o.Order_Date BETWEEN @Begin_Date AND @End_Date and
o.Order_Status_Code <= 98 and
EXISTS (SELECT 1
FROM Order_Lines ol
WHERE ol.Location_Code = o.Location_Code and
ol.Order_Date = o.Order_Date and
ol.Order_Number = o.Order_Number and
ol.Deleted = 0 and
ol.ProductCode IN ('172352', '172353', '172355', '172357', '172360', '172422', '172429', '172343', '172344','172346', '172348', '172351', '172427', '172428')
)
编辑:
如果你想要一个标志,那么只需将逻辑移到select
1:
SELECT o.*,
(CASE WHEN EXISTS (SELECT 1
FROM Order_Lines ol
WHERE ol.Location_Code = o.Location_Code and
ol.Order_Date = o.Order_Date and
ol.Order_Number = o.Order_Number and
ol.Deleted = 0 and
ol.ProductCode IN ('172352', '172353', '172355', '172357', '172360', '172422', '172429', '172343', '172344','172346', '172348', '172351', '172427', '172428')
)
THEN 1 ELSE 0 END) as Flag
FROM Orders o (NOLOCK)
WHERE o.Order_Date BETWEEN @Begin_Date AND @End_Date and
o.Order_Status_Code <= 98;
答案 1 :(得分:0)
如果您从订单中获得所有结果并标记是否存在某些内容,则可以将检查包括在以下选择语句中:
DECLARE @Begin_Date DATETIME; SET @Begin_Date = '2015-01-01'
DECLARE @End_Date DATETIME; SET @End_Date = '2015-01-30'
--INSERT INTO #OrderCostPrice
SELECT o.Location_Code,
o.Order_Number,
o.Order_Date,
o.OrderIdealFoodCost,
o.OrderFinalPrice,
Promo = CASE WHEN (
SELECT count(*) FROM Order_Lines WHERE Location_Code = o.Location_Code
AND Order_Date = o.Order_Date
AND Order_Number = o.Order_Number
AND Order_Product IN ('172352','172353','172355','172357','172360','172422','172429','172343','172344','172346','172348','172351','172427','172428')
) > 0
THEN 1 ELSE 0 END
FROM Orders o (NOLOCK)
WHERE o.Order_Date BETWEEN @Begin_Date AND @End_Date
AND o.Order_Status_Code <= 98