0

我认为可能有办法对此进行分区以找到我想要的,但我看不到它。我有关于产品集的一些略微非标准化数据:SQL Server分区非标准化数据

SetItemID ComponentItemID quantity IsPrimary 
123   234    1   1 
123   345    2   0 
456   567    3   1 
456   678    2   0 

我需要找到套其中quantity“描述s为,例如,IsPrimary组件的quantity>”值isPrimary = 0 component's量。我还需要找到集合,其中数量是组件之间的平等,等等这是确定用于该多条语句,如果必要的话

所有我到目前为止是部分PARTITION声明,可能会或可能不会是正确的办法做到这一点,但我无法弄清楚如何套内比较量:

WITH setdata as 
(select *, row_number() OVER(
    PARTITION by s.setitemid order by s.setitemid) position from set_table s) 
// descending just to get newer sets 
SELECT* from setdata order by setitemid desc 

回答

0

如果我按照您的要求的权利,这应该这样做:

SELECT p1.SetItemId 
from (-- IsPrimary = 1 
     select SetItemID, ComponentItemId, Quantity 
     from SetData 
     where IsPrimary = 1) p1 
    inner join (-- IsPrimary = 0 
       select SetItemID, ComponentItemId, Quantity 
       from SetData 
       where IsPrimary = 0) p0 
    on p0.SetItemID = p1.SetItemID 
    and p1.Quantity > p0.Quantity 
    --and p1.Quantity = p0.Quantity 

将最后一行用于等量的组。

0
SELECT * 
FROM set_table a 
WHERE isPrimary = 1 
     AND quantity > (SELECT quantity 
         FROM set_table b 
         WHERE a.setItemId = b.setItemId 
           AND isPrimary = 0) 
ORDER BY setItemId DESC 

或者

SELECT a.* 
FROM set_table a 
     INNER JOIN set_table b 
       ON a.setItemId = b.setItemId 
        AND a.isPrimary = 1 
        AND b.isPrimary = 0 
WHERE a.quantity > b.quantity