久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

T-SQL 返回表的所有組合

T-SQL Return All Combinations of a Table(T-SQL 返回表的所有組合)
本文介紹了T-SQL 返回表的所有組合的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在另一個問題中,我詢問了表格的所有可能的 3 向組合,假設有 3 篇文章.在這個問題中,我想進一步擴展問題以返回具有 n 個不同文章的表的所有 n 向組合.一篇文章可以有多個供應商.我的目標是有一組組合,每個組都有每篇文章.

In a different question, I asked about all possible 3-way combinations of a table, assuming that there are 3 articles. In this question, I would like to further extend the problem to return all n-way combinations of a table with n distinct articles. One article can have multiple suppliers. My goal is to have groups of combinations with each group having each article.

下面是一個示例表,但請記住,可能不止這 3 篇文章.

Below is a sample table but keep in mind that there could be more than those 3 articles.

+---------+----------+
| Article | Supplier |
+---------+----------+
|    4711 | A        |
|    4712 | B        |
|    4712 | C        |
|    4712 | D        |
|    4713 | C        |
|    4713 | E        |
+---------+----------+

對于上面的示例,18 個數據集可能有 6 個組合對.下面是上面示例的解決方案應該是什么樣子:

For the example above, there would be 6 combination pairs possible with 18 datasets. Below is how the solution for the example above is supposed to look like:

+----------------+---------+----------+
| combination_nr | article | supplier |
+----------------+---------+----------+
|              1 |    4711 | A        |
|              1 |    4712 | B        |
|              1 |    4713 | C        |
|              2 |    4711 | A        |
|              2 |    4712 | B        |
|              2 |    4713 | E        |
|              3 |    4711 | A        |
|              3 |    4712 | C        |
|              3 |    4713 | C        |
|              4 |    4711 | A        |
|              4 |    4712 | D        |
|              4 |    4713 | E        |
|              5 |    4711 | A        |
|              5 |    4712 | D        |
|              5 |    4713 | C        |
|              6 |    4711 | A        |
|              6 |    4712 | D        |
|              6 |    4713 | E        |
+----------------+---------+----------+

我在另一個問題中問這個,因為在另一個問題中我沒有指定我需要這是動態的,而不僅僅是上面的 3 篇文章.在這里您可以找到舊問題.

I am asking this in a different question because in the other question I have not specified that I need this to be dynamic and not only the 3 articles from above. Here you can find the old question.

推薦答案

要創建此結果,您需要確定兩件事:

To create this result you'll need to determine two things:

  • 一組包含可能組合數量的 ID 的數據.
  • 一種確定商品/供應商對何時對應于組合 ID 的方法.

為了找出組合的集合,您需要先弄清楚組合的數量.為此,您需要確定為每件商品設置的每個供應商的規模.使用 count 聚合函數可以輕松獲取大小,但為了找出組合,我需要所有值的乘積,這并不容易.幸運的是,在另一個 SO 問題中有一個關于如何做到這一點的答案.

In order to figure out the set of combinations, you need to first figure out the number of combinations. To do that, you need to figure out the size of each supplier set for each article. Getting the size is accomplished easily with a count aggregate function, but in order to figure out the combinations, I needed a product of all the values, which is not easily done. Fortunately there was an answer on how to do this in another SO questions.

既然確定了組合的數量,就必須生成 id.在 TSQL 中沒有很好的方法可以做到這一點.我最終使用了一個簡單的遞歸 CTE.這樣做的缺點是最多只能產生 32767 個組合.如果您需要更多,還有其他方法可以產生這些值.

Now that the number of combinations is determined, the ids have to be generated. There is no great way to do this in TSQL. I ended up using a simple recursive CTE. This has the drawback of only being able to produce up to 32767 combos. There are other methods to produce these values if you're going to need more.

為了確定物品/供應商對何時與組合對齊,我使用 ROW_NUMBER 窗口函數按文章分區并按供應商排序,以獲得每對的序列號.然后它使用了一個老技巧,通過序列號使用組合數的模數來確定是否顯示一對.

To determine when an article/supplier pair lines up with a combination I use ROW_NUMBER window function partition by Article and sorted by Supplier to get a sequence number for each pair. Then it's using an old trick of using Modulo of the combination number by the sequence number to determine if a pair is displayed.

仍然存在一個問題,即無法保證冗余對不會配對在一起.為了解決這個問題,添加了一個 CTE,用于計算出現在文章之前的可能組合的數量.目的是在顯示下一個順序之前,將后面文章中的值重復組合的次數.我稱之為乘數(即使我用它除以 comboId),這將確保不同的結果.

There is still an issue in that there is no guarantee that redundant pairs won't be paired together. In order to address this a CTE was added that calculates the number of possible combinations that come before an article. The intention is so that a value in a later article is repeated the number of times for a combination before the next in sequence is displayed. I called it multiplier (even though I divide the comboId by it) and this is what will ensure distinct results.

WITH ComboId AS (
    -- Product from https://stackoverflow.com/questions/3912204/why-is-there-no-product-aggregate-function-in-sql
    SELECT 0 [ComboId], exp (sum (log (SequenceCount))) MaxCombos
    FROM (
        SELECT COUNT(*) SequenceCount
        FROM src 
        GROUP BY Article
    ) x
    UNION ALL
    SELECT ComboId + 1, MaxCombos
    FROM ComboId
    WHERE ComboId + 1 < MaxCombos
)
, Multiplier AS (
    SELECT s.Article
        , COALESCE(exp (sum (log (SequenceCount)) OVER (ORDER BY Article ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)), 1) [Multiplier]
    FROM (
        SELECT Article, COUNT(*) SequenceCount
        FROM src
        GROUP BY Article
    ) s
)
, Sequenced AS (
    SELECT s.Article, s.Supplier, m.Multiplier
        , ROW_NUMBER() OVER (PARTITION BY s.Article ORDER BY s.Supplier) - 1 ArtSupplierSeqNum
        , COUNT(*) OVER (PARTITION BY s.Article) MaxArtSupplierSeqNum
    FROM src s
    INNER JOIN Multiplier m ON m.Article = s.Article
)
SELECT c.ComboId + 1 [ComboId], s.Article, s.Supplier
FROM ComboId c
INNER JOIN Sequenced s ON s.ArtSupplierSeqNum = CAST(c.ComboId / Multiplier as INT) % s.MaxArtSupplierSeqNum
ORDER BY ComboId, Article
OPTION (MAXRECURSION 32767)

這篇關于T-SQL 返回表的所有組合的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 日日噜噜噜夜夜爽爽狠狠视频, | 91中文字幕在线观看 | 九色91视频 | 一级h片 | 亚洲免费成人av | 国内精品视频一区二区三区 | 亚洲一视频 | 免费激情网站 | 欧美又大粗又爽又黄大片视频 | 欧美精品一区二区三区四区 在线 | 亚州av | 精品国产一区探花在线观看 | 成人免费小视频 | 国产日韩一区二区三免费 | 91麻豆精品国产91久久久久久 | 伊人在线| 免费美女网站 | 国产精品三级 | xxxxx免费视频 | 毛片一区二区三区 | 精品自拍视频在线观看 | 亚洲成人午夜电影 | 国产精品综合一区二区 | 欧美日一区二区 | 久久国产高清 | 欧美中文在线 | 久久久久久久久久久国产 | 亚洲精品一二三区 | 国户精品久久久久久久久久久不卡 | 国产98色在线 | 国产1区2区在线观看 | 久久精品一区二区三区四区 | 久久久国产一区二区三区 | 国产免费一区二区三区最新6 | 欧美精品一区二区三区在线 | 日本三级电影在线观看视频 | 日韩中文字幕 | 久久国产精品99久久久久久丝袜 | 综合一区 | 久久亚洲综合 | 免费在线播放黄色 |