我是SQL的新手,我有一个表,用于存储客户要求的成对选项。此表如下所示:
**CustomerID, Product 1, Product2**
15 , 338 , 298
15 , 161 , 241
我想在检查库存数据时最终得到可用产品对的列表,例如:
**StockData**
298
338
161
91
241
96
99
102
104
股票数据如下所示:
static FTThreeStateCheckBox()
{
IsCheckedProperty.OverrideMetadata(typeof(FTThreeStateCheckBox), new FrameworkPropertyMetadata(null, IsCheckedChanged));
}
public static void IsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((d as FTThreeStateCheckBox).IsChecked == null)
{
(d as FTThreeStateCheckBox).IsThreeState = false;
(d as FTThreeStateCheckBox).IsChecked = false;
}
}
答案 0 :(得分:1)
如果您想要的是每个客户有2个产品选项的每种组合,那么这样的自我加入应该有效:
select p1.customerID, p1.productID product1, product2.productID product2
from options p1
join options p2 on p2.customerID=p1.customerID
and p1.productID<p2.productID
where p1.productID in (select productID from stock)
and p2.productID in (select productID from stock)
如果您想要第一批可用的产品,优先考虑选项编号,那么您可以这样做:
;with productpairs as
(
select p1.customerID, p1.productID product1, product2.productID product2,
p1.optionID+p2.optionID priority
from options p1
join options p2 on p2.customerID=p1.customerID
and p1.productID<p2.productID
),
orderedpairs as
(
select *, ROW_NUMBER() OVER (PARTITION BY customerID ORDER BY priority) AS rn
from productpairs
)
select customerID, product1, product2
from oderedpairs
where rn=1