我刚接触sql并且最近面临一个问题......
如果订单中的所有商品均为IsRepInbound = True
,则此订单的所有订单项均为R
。
如果订单中至少有一项IsRepInbound = False
,则此订单的所有订单项均为E
。
尝试Group By,但似乎我不需要聚合...也许OVER条款分区By?任何提示?
表A
"Item_Number" "Order Number" "IsRepInbound"
1 OR-1 TRUE
2 OR-1 TRUE
3 OR-1 FALSE
4 OR-1 TRUE
1 OR-2 TRUE
2 OR-2 TRUE
3 OR-2 TRUE
表B(尝试联系):
"Item_Number" "Order Number" "Exchage_Ind"
1 OR-1 E
2 OR-1 E
3 OR-1 E
4 OR-1 E
1 OR-2 R
2 OR-2 R
3 OR-2 R
答案 0 :(得分:1)
如果给定的项目编号对其所有值都有moment("2016-09-14T10:44:55.027Z").format('hh:mm:ss')
,那么您希望报告function convert(s)
return (s:gsub('.',function (s)
if s == ';' then return s end
return s:byte()..':'
end)
:gsub(':;',';')
:gsub(':$',''))
end
print(convert 'Hello;this;is;a;text')
作为结果,但如果它甚至只有一个TRUE
,那么您想要报告{ {1}}。您可以按项目编号使用条件聚合,并计算每个条件聚合以确定其标签。
R
答案 1 :(得分:0)
使用个案陈述:
SELECT
Item_Number,
Order_Number,
CASE IsRepInbound
WHEN 'TRUE' THEN 'E'
ELSE 'R'
END AS Exchage_Ind
FROM TABLE_A;
答案 2 :(得分:0)
与@Tim Biegeleisen相同,但具有Windows功能
SELECT [Item_Number],
[Order_Number],
CASE WHEN
COUNT(CASE WHEN [IsRepInbound] = 'FALSE' THEN 1 END)
OVER (PARTITION BY [Order_Number]) = 0
THEN 'R'
ELSE 'E'
END
FROM Table1
<强>输出强>
答案 3 :(得分:0)
以下是使用Cross Apply
的一种方法Declare @YourTable TABLE(Item_Number int, Order_Number varchar(4), IsRepInbound varchar(5))
Insert into @YourTable values
(1, 'OR-1', 'TRUE'),
(2, 'OR-1', 'TRUE'),
(3, 'OR-1', 'FALSE'),
(4, 'OR-1', 'TRUE'),
(1, 'OR-2', 'TRUE'),
(2, 'OR-2', 'TRUE'),
(3, 'OR-2', 'TRUE')
Select Item_Number
,Order_Number
,B.Exchage_Ind
From @YourTable A
Cross Apply (Select Exchage_Ind=case when min(IsRepInbound)='False' then 'E' else 'R' end
From @YourTable
where Order_Number=A.Order_Number
) B
返回
Item_Number Order_Number Exchage_Ind
1 OR-1 E
2 OR-1 E
3 OR-1 E
4 OR-1 E
1 OR-2 R
2 OR-2 R
3 OR-2 R
答案 4 :(得分:-1)
您可以将查询用作
SELECT Item_Number,CASE
WHEN ORDER = 'TRUE' THEN 'E'
WHEN ORDER = 'FALSE' THEN 'R'
END AS Order_Number, IsRepInbound as Exchage_Ind
FROM TABLE_A;
希望它能为您提供结果