簡體   English   中英

SQL - 查找所有組合

[英]SQL - find all combinations

從這樣的數據集中,我需要在同一個房間中獲得所有可能的案例組合,以便沒有案例與另一個重疊。

room        case        start               end
a           1           2019-11-27 09:00    2019-11-27 10:15
a           2           2019-11-27 10:30    2019-11-27 12:00
a           3           2019-11-27 12:00    2019-11-27 12:30
b           4           2019-11-27 08:30    2019-11-27 10:30
b           5           2019-11-27 10:00    2019-11-27 12:00
b           6           2019-11-27 11:00    2019-11-27 12:20

預期的結果是

room        combination         cases   
a           1                   1
a           2                   1,2
a           3                   1,2,3
a           4                   2
a           5                   2,3
a           6                   3
b           1                   4
b           2                   4,6
b           3                   5
b           4                   6

我能夠獲得哪種情況可以與其他情況相結合的單行結果,例如:

room        case        combinewithcase
a           1           2
a           1           3
a           2           1
a           2           3
a           3           1
a           3           2
b           4           6
b           6           4

我已經嘗試了一些遞歸,但我無法獲得我需要的結果類型,我很感激任何人都可以分享的任何指導。

這是使用字符串聚合的一種方法。 要按時間獲得重疊:

select rd.room, rd.dte,
       (select string_agg(t2.case, ',') within group (order by t2.case)
        from t t2
        where t2.room = t.room and
              t2.start <= t.dte and
              t2.end > t.dte
       ) as cases
from ((select room, start as dte from t
      ) union -- on purpose to remove duplicates
      (select room, end from t
      )
     ) rd;

然后,您可以將其用作子查詢/CTE 來枚舉組合:

select room, cases, count(*),
       row_number() over (partition by room order by combination) as combination
from (select rd.room, rd.dte,
             (select string_agg(t2.case, ',') within group (order by t2.case)
              from t t2
              where t2.room = t.room and
                    t2.start <= t.dte and
                    t2.end > t.dte
             ) as cases
      from ((select room, start as dte from t
            ) union -- on purpose to remove duplicates
            (select room, end from t
            )
           ) rd
     ) rd
group by room, cases
order by room, cases;

編輯:

在早期版本的 SQL Server 中,您可以使用 XML 方法進行字符串聚合:

select room, cases, count(*),
       row_number() over (partition by room order by combination) as combination
from (select rd.room, rd.dte,
             stuff( (select concat(',', t2.case)
                     from t t2
                     where t2.room = t.room and
                           t2.start <= t.dte and
                           t2.end > t.dte
                     order by t2.case
                     for xml path
                    ), 1, 1, '')
             ) as cases
      from ((select room, start as dte from t
            ) union -- on purpose to remove duplicates
            (select room, end from t
            )
           ) rd
     ) rd
group by room, cases
order by room, cases;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM