2014-11-03 119 views
0

我的SQL Server 2008R2找到匹配的記錄集

消息表

ContrlNo| LineNo  | Msg 
    1  |  1  | Tiger1 Text 
    1  |  2  | Tiger1 Text 
    1  |  3  | Tiger1 Text 
    1  |  4  | Tiger1 Text 
    2  |  1  | Tiger1 Text1 
    2  |  2  | Tiger1 Text2 
    2  |  3  | Tiger1 Text3 
    2  |  4  | Tiger1 Text4 
    3  |  1  | Horse 1 
    3  |  2  | Horse 2 
    3  |  3  | Horse 3 
    3  |  4  | Horse 4 

RuleTable

RuleNo| MsgLineNo | RuleStartingPos | RuleMsg 
1  |  1  | 1    | Tiger1 Text 
2  |  1  | 1    | Tiger1 Text 
2  |  3  | 1    | Tiger1 Text3 

對於每一組ControlNo記錄對下面的表MESSAGETABLE我想應用規則fr om RULETABLE並列出RULENo,如果有任何mataches。

如果您看到規則表,則規則2與規則1重疊。要求是爲每個控制號獲得最匹配的規則編號。該預期的結果,

ContrlNo | RuleNo 
1 | 1 
2 | 2 
3 | NULL 

感謝, 周杰倫

+0

我不明白背後的預期結果你的邏輯。你能更徹底地解釋一下嗎? – 2014-11-03 19:36:00

+0

我們需要爲每個ContrlNo獲取所有記錄(LineNo,Msg),並將其與RuleTable(MsgLineNo,RuleMsg)進行匹配。 – Jay 2014-11-03 19:46:54

+0

例如,如果我們從MessageTable獲取屬於控件號1的記錄並將其與RuleTable匹配,則RuleNo 1匹配100%。但RuleNo 2有兩個條件,規則2的第二個條件不匹配。所以結果是,ContrlNo | RuleNo(1 | 1)。 如果我們從MessageTable中獲取屬於ContrlNo 2的記錄並將其與RuleTable進行匹配,則RuleNo 1匹配100%。但RuleNo 2有兩個條件,它們都匹配100%。因爲我們需要最大的匹配規則,所以在這種情況下,結果是ContrlNo | RuleNo(2 | 2)。 – Jay 2014-11-03 19:56:03

回答

0

我相信以下將檢索列出的結果:它會顯示與擁有最匹配線(相關規則的控制數,如果像你的第一個情況下,兩個規則具有相同數量的匹配,它將檢查MatchPercent)。

SELECT MT.ContrlNo, r.RuleNo, r.MatchPercent 
FROM 
    MessageTable MT 
    LEFT JOIN 
    (
     SELECT 
     ContrlNo, 
     RuleNo, 
     MatchedRules/AvailableRules AS MatchPercent, 
     ROW_NUMBER() OVER (PARTITION BY ContrlNo ORDER BY MatchedRules DESC, MatchedRules/AvailableRules DESC) AS rn 
     FROM 
     (
      SELECT 
      ContrlNo, 
      R.RuleNo, 
      COUNT(*) as MatchedRules, 
      (SELECT COUNT(*) FROM RuleTable WHERE RuleTable.RuleNo = R.RuleNo) + 0.0 AS AvailableRules 
      FROM 
       MessageTable M 
       INNER JOIN 
       RuleTable R ON 
        M.[LineNo] = R.MsgLineNo AND 
        SUBSTRING(M.Msg,R.RuleStartingPos,LEN(R.RuleMsg)) LIKE '%' + R.RuleMsg + '%' 
      GROUP BY M.ContrlNo, R.RuleNo 
     ) q 
    ) r ON 
    MT.ContrlNo = r.ContrlNo AND 
    r.rn = 1 
GROUP BY MT.ContrlNo, r.RuleNo, r.MatchPercent 

SQL Fiddle

+0

謝謝你這麼多.. ..真的釘上了它。 – Jay 2014-11-04 21:35:16

0

第一子查詢獲得總匹配規則和第二子查詢得到的總規則,當這兩個條件相匹配,那麼我們展示RuleNo其他明智NULL因爲我們使用LEFT JOIN

Select A.ContrlNo, ISNULL(T.RuleNo,0) as RuleNo FROM 
( select ContrlNo, COUNT(R.RuleNo) as MatchedRules 
    FROM Messages M 
    LEFT JOIN Rules R 
    on M.[LineNo] = R.MsgLineNo 
    and SUBSTRING(M.Msg,R.RuleStartingPos,LEN(R.RuleMsg)) = R.RuleMsg 
    AND M.ContrlNo = R.RuleNo 
GROUP BY M.ContrlNo) A 
LEFT JOIN ( 
    select COUNT(MsgLineNo) as TotalRules, RuleNo 
    from Rules R1 
    group by RuleNo) T 
    ON A.MatchedRules = T.TotalRules 
+0

選擇A.ContrlNo,T.RuleNo FROM (選擇ContrlNo,COUNT(R.RuleNo)作爲MatchedRules FROM消息表中號 LEFT上M. [LINENO] = R.MsgLineNo 和M.Msg = R JOIN RuleTableř .RuleMsg GROUP BY M.ContrlNo)甲 LEFT JOIN( SELECT COUNT(MsgLineNo)作爲TotalRules,從RuleTable R1 組RuleNo 由RuleNo)筆 ON A.MatchedRules = T.TotalRules用於我需要消息匹配做下面的事情,SUBSTRING(M.Msg,R.RuleStartingPos,LEN(R.RuleMsg))= R.RuleMsg我們需要得到最匹配的RuleNo foreach ContrlNo。 – Jay 2014-11-03 20:20:30

+0

@Jay,將該條件添加到子句中,它確實給出了最大匹配規則號。 – radar 2014-11-03 20:24:18

+0

謝謝....但我們無法比較M.ContrlNo = R.RuleNo ...他們完全不同 – Jay 2014-11-03 20:36:35