如何获得另一组行的最近子集(行组)

时间:2017-05-16 12:12:25

标签: sql-server sql-server-2008 tsql tree

我将数据以树枝的形式存储在表中。所有这些分支都是在特定的迭代过程之后完成的。 但是,我所做的分支序列和实际所需分支的顺序并不完全相同。为清楚起见,这是分支所需的序列。

enter image description here

我做的分支如下:

enter image description here

所以,分公司没有。正在分配顶部图像中的5分支编号。 4在我的SQL查询中,因为在制作了所有黑色分支后制作了所有红色分支。

因此,有什么办法,我可以改变分支号码。我的sql输出9到所需的分支号。 4从第一个截图,红科没有。 4是Black Branch no的最接近的子集。同样红科没有。 6是Black Branch no的最接近的子集。 5,然后7是6。

我的sql表的内容如下:

    DECLARE @tbl AS TABLE(
        State_ID INT,
        BranchNo INT,
        Share FLOAT
    )

INSERT INTO @tbl
SELECT 30500095,    1   ,1
UNION ALL SELECT 30500096,  1   ,0.0958083832335329
UNION ALL SELECT 30500097,  1   ,0.5625
UNION ALL SELECT 30500098,  1   ,0.222222222222222
UNION ALL SELECT 30500099,  1   ,1
UNION ALL SELECT 30500100,  1   ,0.5
UNION ALL SELECT 30500101,  1   ,1
UNION ALL SELECT 30500095,  2   ,1
UNION ALL SELECT 30500096,  2   ,0.0958083832335329
UNION ALL SELECT 30500097,  2   ,0.5625
UNION ALL SELECT 30500098,  2   ,0.222222222222222
UNION ALL SELECT 30500099,  2   ,1
UNION ALL SELECT 30500102,  2   ,0.5
UNION ALL SELECT 30500095,  3   ,1
UNION ALL SELECT 30500096,  3   ,0.0958083832335329
UNION ALL SELECT 30500097,  3   ,0.5625
UNION ALL SELECT 30500103,  3   ,0.666666666666667
UNION ALL SELECT 30500104,  3   ,0.666666666666667
UNION ALL SELECT 30500105,  3   ,0.75
UNION ALL SELECT 30500095,  4   ,1
UNION ALL SELECT 30500096,  4   ,0.0958083832335329
UNION ALL SELECT 30500097,  4   ,0.5625
UNION ALL SELECT 30500103,  4   ,0.666666666666667
UNION ALL SELECT 30500106,  4   ,0.333333333333333
UNION ALL SELECT 30500107,  4   ,0.5
UNION ALL SELECT 30500095,  5   ,1
UNION ALL SELECT 30500096,  5   ,0.0958083832335329
UNION ALL SELECT 30500108,  5   ,0.4375
UNION ALL SELECT 30500109,  5   ,0.285714285714286
UNION ALL SELECT 30500095,  6   ,1
UNION ALL SELECT 30500096,  6   ,0.0958083832335329
UNION ALL SELECT 30500108,  6   ,0.4375
UNION ALL SELECT 30500110,  6   ,0.714285714285714
UNION ALL SELECT 30500111,  6   ,0.4
UNION ALL SELECT 30500095,  7   ,1
UNION ALL SELECT 30500112,  7   ,0.904191616766467
UNION ALL SELECT 30500113,  7   ,0.417218543046358
UNION ALL SELECT 30500095,  8   ,1
UNION ALL SELECT 30500096,  8   ,0.0958083832335329
UNION ALL SELECT 30500097,  8   ,0.5625
UNION ALL SELECT 30500095,  9   ,1
UNION ALL SELECT 30500096,  9   ,0.0958083832335329
UNION ALL SELECT 30500097,  9   ,0.5625
UNION ALL SELECT 30500103,  9   ,0.666666666666667
UNION ALL SELECT 30500104,  9   ,0.666666666666667
UNION ALL SELECT 30500095,  10  ,1
UNION ALL SELECT 30500096,  10  ,0.0958083832335329
UNION ALL SELECT 30500097,  10  ,0.5625
UNION ALL SELECT 30500103,  10  ,0.666666666666667
UNION ALL SELECT 30500106,  10  ,0.333333333333333
UNION ALL SELECT 30500095,  11  ,1
UNION ALL SELECT 30500096,  11  ,0.0958083832335329
UNION ALL SELECT 30500108,  11  ,0.4375
UNION ALL SELECT 30500110,  11  ,0.714285714285714
UNION ALL SELECT 30500095,  12  ,1
UNION ALL SELECT 30500112,  12  ,0.904191616766467

SELECT * FROM @tbl

2 个答案:

答案 0 :(得分:0)

您可以使用简单的轴,这将产生以下结果:

select * from @tbl 
pivot (max(share) for BranchNo in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10], [11], [12])) p

答案 1 :(得分:0)

最后我找到了我的任务解决方案。

此查询使Branch 9接近Branch:

SELECT TOP 1
    b.branchno AS b2
    ,COUNT(*) AS cnt
FROM @tbl a
INNER JOIN @tbl b
    ON a.state_id = b.state_id
WHERE b.branchno <> a.branchno
AND a.branchno = 3
AND b.branchno >= 3
GROUP BY b.branchno
ORDER BY cnt DESC, b2