MSSQL排列/组合-使用数据子集查找所有可能的匹配

时间:2018-12-21 17:22:19

标签: sql combinations permutation

在MSSQL中,我有一个表(ProductRecipe),其中包含多达5种不同组件的产品。然后,我有一个包含各个组件产品及其成本的数据集。

我想做的是找到满足我食谱要求的所有不同排列/组合。

CREATE TABLE #ProductRecipe (ProductRecipeID INT, Component1 INT, Component2 INT, Component3 INT, Component4 INT, Component5 INT)
CREATE TABLE #ComponentPricing (RowID INT, PricingID INT, ProductID INT, ProductDescription VARCHAR(50), Cost DECIMAL(18,6))

INSERT INTO #ProductRecipe (ProductRecipeID, Component1, Component2) VALUES (21, 130, 468)
INSERT INTO #ComponentPricing (RowID, PricingID, ProductID, ProductDescription, Cost)
VALUES (1, 314023, 130, 'ULS2', 1.783800)
 , (2, 313616, 130, 'ULS2', 1.783800)
 , (3, 313071, 130, 'ULS2', 1.794000)
 , (4, 312865, 130, 'ULS2', 1.789500)
 , (5, 316323, 468, 'B100', 1.550500)

SELECT * FROM #ProductRecipe
SELECT * FROM #ComponentPricing

DROP TABLE #ProductRecipe
DROP TABLE #ComponentPricing

我要实现的结果是,由于配方的前4个记录(对于ProductID 130)可以与最后一个记录(ProductID 468)混合,因此我得到了4种不同的配方。因为这是我的ProductRecipe表中定义的两个组件产品,所以只能将这两个产品混合。

所需结果: 第1 + 5行并列,第2 + 5行并列,第3 + 5行并列,第4 + 5行并列;返回PricingID列。

ProductRecipeID Component1 Component2 Component3 Component4 Component5
21              314023     316323   
21              313616     316323   
21              313071     316323   
21              312865     316323   

2 个答案:

答案 0 :(得分:1)

查看是否可行。

DECLARE @ProductRecipe TABLE (ProductRecipeID INT, Component1 INT, Component2 INT, Component3 INT, Component4 INT, Component5 INT) 
DECLARE @ComponentPricing TABLE (RowID INT, PricingID INT, ProductID INT, ProductDescription VARCHAR(50), Cost DECIMAL(18,6)) 

INSERT INTO @ProductRecipe (ProductRecipeID, Component1, Component2,Component3) VALUES (21, 130, 468,221)
INSERT INTO @ComponentPricing (RowID, PricingID, ProductID, ProductDescription, Cost)
VALUES (1, 314023, 130, 'ULS2', 1.783800)
 , (2, 313616, 130, 'ULS2', 1.783800)
 , (3, 313071, 130, 'ULS2', 1.794000)
 , (4, 312865, 130, 'ULS2', 1.789500)
 , (5, 316323, 468, 'B100', 1.550500)
 , (6, 316322, 221, 'B1110', 1.5250500)

;WITH UnpivotedRecipe AS
(
    SELECT 
        ProductRecipeID, ComponentID
    FROM
        (SELECT * FROM @ProductRecipe) AS P
        UNPIVOT(ComponentID FOR V IN(Component1,Component2,Component3,Component4,Component5))AS UP
)
, JoinedData AS
(
    SELECT 
        ProductRecipeID, ComponentID, RowID
    FROM 
        UnpivotedRecipe R
        INNER JOIN @ComponentPricing C ON C.ProductID = R.ComponentID

)
SELECT DISTINCT J1.ComponentID,J1.RowID,J2.ComponentID FROM JoinedData J1
CROSS JOIN JoinedData J2
WHERE
    J1.ComponentID<>J2.ComponentID

答案 1 :(得分:0)

在自联接表上考虑多个devtools::install_github('daroczig/fbRads') Downloading GitHub repo daroczig/fbRads@master √ checking for file 'C:\Users\nknauer\AppData\Local\Temp\RtmpWW5UJm\remotes11f43012405a\daroczig- fbRads-2c08d3e/DESCRIPTION' ... - preparing 'fbRads': (459ms) √ checking DESCRIPTION meta-information ... - checking for LF line-endings in source and make files and shell scripts - checking for empty or unneeded directories - building 'fbRads_3.0.0.tar.gz' * installing *source* package 'fbRads' ... ** R ** byte-compile and prepare package for lazy loading ** help *** installing help indices converting help for package 'fbRads' finding HTML links ... done fbRads html fb_api_most_recent_version html fb_api_version html fb_insights html fbad_add_audience html fbad_assign_users_to_account html fbad_check_curl_params html fbad_check_fbacc html fbad_create_account html fbad_create_ad html fbad_create_adset html fbad_create_audience html fbad_create_campaign html fbad_create_creative html fbad_create_image html fbad_create_lookalike_audience html fbad_delete_audience html fbad_get_adaccount_details html fbad_get_adaccounts html fbad_get_client_ad_accounts html fbad_get_client_pages html fbad_get_my_ad_accounts html fbad_get_owned_ad_accounts html fbad_get_owned_pages html fbad_get_pixels html fbad_get_search html fbad_init html fbad_insights_get_async_results html fbad_list_ad html fbad_list_adset html fbad_list_audience html fbad_list_campaign html fbad_preview_ad html fbad_reachestimate html fbad_read_ad html fbad_read_adset html fbad_read_audience html fbad_read_campaign html fbad_read_creative html fbad_remove_audience html fbad_request html fbad_request_next_page html fbad_share_audience html fbad_update_ad html fbad_update_adset html fbad_update_campaign html fromJSONish html is.FB_Ad_Account html print.FB_Ad_Account html this_function_name html url_parse html ** building package indices ** testing if installed package can be loaded *** arch - i386 *** arch - x64 * DONE (fbRads) In R CMD INSTALL Warning messages: 1: In untar2(tarfile, files, list, exdir) : skipping pax global extended headers 2: In untar2(tarfile, files, list, exdir) : skipping pax global extended headers

LEFT JOIN

Rextester Demo