2

わかりました、私は非常にばかげた質問をしているかもしれませんが、どういうわけか私は以下を実行する方法を得ることができません.

以下のように2つの列を含むテーブルがあります

+-------+-------------------------------------------------------------------------------------------------------------------------------------------------+
| SL No |                                                                      Work                                                                       |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------+
|     1 | Identify Process Champs across all teams for BCUK processes                                                                                     |
|     2 | Impart short training on FMEA to all the Process Champs                                                                                         |
|     2 | List down all critical steps involved in the Process to ascertain the risk involved, feed the details back to FMEA template to analyze the risk |
|     3 | Prioritize the process steps based on Risk Priority Number                                                                                      |
|     4 | Identity the Process Gaps, suggest process improvement ideas to mitigate/mistake proof or reduce the risk involved in the process               |
+-------+-------------------------------------------------------------------------------------------------------------------------------------------------+

今、私は以下のような「キーワード」を保持する他のテーブルを持っています

+-------+----------+
| Sl No |   Tags   |
+-------+----------+
|     1 | BCUK     |
|     2 | FMEA     |
|     3 | Priority |
|     4 | Process  |
+-------+----------+

ここで、2番目のテーブルの「タグ」に基づいて最初のテーブルで「文字列を検索」し、次のようなものを返したいと思います

+----------+-------+
|   Tags   | Count |
+----------+-------+
| BCUK     |     1 |
| FMEA     |     2 |
| Priority |     1 |
| Process  |     8 |
+----------+-------+

"Process"キーワードがeight times複数の行にわたってテーブル全体 (最初のテーブル) に表示されるため、カウントは 8 として返されます。

私は使っているSQL Server 2014 Express Edition

4

2 に答える 2

2

Adam Machanic には、GetSubstringCountこの種の操作のための機能があります。あなたのニーズに合わせて少し変更しました。詳細情報: http://dataeducation.com/counting-occurrences-of-a-substring-within-a-string/

サンプルデータ

CREATE TABLE MyTable(
    SLNo    INT,
    Work    VARCHAR(4000)
)
INSERT INTO MyTable VALUES
(1, 'Identify Process Champs across all teams for BCUK processes'),
(2, 'Impart short training on FMEA to all the Process Champs'),
(2, 'List down all critical steps involved in the Process to ascertain the risk involved, feed the details back to FMEA template to analyze the risk'),
(3, 'Prioritize the process steps based on Risk Priority Number'),
(4, 'Identity the Process Gaps, suggest process improvement ideas to mitigate/mistake proof or reduce the risk involved in the process');

CREATE TABLE KeyWord(
    SLNo    INT,
    Tag     VARCHAR(20)
)
INSERT INTO KeyWord VALUES
(1, 'BCUK'),
(2, 'FMEA'),
(3, 'Priority'),
(4, 'Process');

解決

;WITH E1(N) AS(
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
    SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
)
,E2 AS(SELECT 1 AS N FROM E1 a, E1 b)
,E4 AS(SELECT 1 AS N FROM E2 a, E2 b)
,Tally(N) AS(
    SELECT TOP(11000) ROW_NUMBER() OVER(ORDER BY(SELECT NULL))FROM E4 a, e4 b
)
SELECT
    k.Tag,
    [Count] = SUM(x.cc)
FROM KeyWord k
CROSS JOIN MyTable m
CROSS APPLY(
    SELECT COUNT(*) AS cc
    FROM Tally
    WHERE
        SUBSTRING(m.Work, N, LEN(k.tag)) = k.tag
)x
GROUP BY k.tag

結果

Tag                  Count
-------------------- -----------
BCUK                 1
FMEA                 2
Priority             1
Process              8
于 2015-02-17T08:25:04.223 に答える
1

一致を数える代わりに、それらを余分な文字に置き換えて、長さを元の長さと比較しています。そうすれば、カウントは非常に簡単かつ高速です。

テーブルとデータのテスト

DECLARE @texts table(SL_No int identity(1,1),Work varchar(max))

INSERT @texts VALUES
  ('Identify Process Champs across all teams for BCUK processes'),
  ('Impart short training on FMEA to all the Process Champs'),
  ('List down all critical steps involved in the Process to ascertain the risk involved, feed the details back to FMEA template to analyze the risk'),
  ('Prioritize the process steps based on Risk Priority Number'),
  ('Identity the Process Gaps, suggest process improvement ideas to mitigate/mistake proof or reduce the risk involved in the process')

DECLARE @searchvalues table(S1_No int identity(1,1),Tags varchar(max))

INSERT @searchvalues
VALUES('CUK'),('FMEA'),('Priority'),('Process')

クエリ:

SELECT 
  sum(len(replace(txt.work, sv.tags, sv.tags + '@')) - len(txt.work)) count, 
  tags
FROM 
  @texts txt
CROSS APPLY
  @searchvalues sv
WHERE charindex(sv.tag, txt.work) > 0
GROUP BY tags

結果:

count   tags
1   CUK
2   FMEA
1   Priority
8   Process
于 2015-02-17T08:39:41.943 に答える