2

MSSQL2005テーブルがあります。

[Companies](
    [CompanyID] [int] IDENTITY(1,1) NOT NULL,
    [Title] [nvarchar](128),
    [Description] [nvarchar](256),
    [Keywords] [nvarchar](256)
)

この会社のタグクラウドを生成したいと思います。ただし、すべてのキーワードをコンマで区切った1つの列に保存しました。最もよく使用されるキーワードによってタグクラウドを生成する方法に関する提案。1社あたり約10個のキーワードが数百万の企業に存在する可能性があります。

ありがとうございました。

4

3 に答える 3

4

ステップ1:キーワードを適切な関係(表)に分離します。

CREATE TABLE Keywords (KeywordID int IDENTITY(1,1) NOT NULL
  , Keyword NVARCHAR(256)
  , constraint KeywordsPK primary key (KeywordID)
  , constraint KeywordsUnique unique (Keyword));

ステップ2:すべての多対多関係のように、会社とタグ間の多対多関係を別のテーブルにマップします。

CREATE TABLE CompanyKeywords (
   CompanyID int not null
   , KeywordID int not null
   , constraint CompanyKeywords primary key (KeywordID, CompanyID)
   , constraint CompanyKeyword_FK_Companies
      foreign key (CompanyID)
      references Companies(CompanyID)
   , constraint CompanyKeyword_FK_Keywords
      foreign key (KeywordID)
      references Keywords (KeywordID));

ステップ3:単純なGROUP BYクエリを使用して、「クラウド」を生成します(たとえば、「クラウド」を最も一般的な100個のタグを意味します)。

with cte as (
SELECT TOP 100 KeywordID, count(*) as Count
FROM CompanyKeywords
group by KeywordID
order by count(*) desc)
select k.Keyword, c.Count
from cte c
join Keyword k on c.KeywordID = k.KeywordID;

ステップ4:結果はほとんど変更されず、計算コストが高くなるため、結果をキャッシュします。

于 2010-08-12T21:22:44.323 に答える
1

Remusが提案するように、デザインが正規化されていることを確認したいのですが、デザインを変更できない場合は...

解析関数(ここで使用する例を参照)を使用して、キーワードを解析してカウントすることができます。

CREATE FUNCTION [dbo].[fnParseStringTSQL] (@string NVARCHAR(MAX),@separator NCHAR(1))
RETURNS @parsedString TABLE (string NVARCHAR(MAX))
AS 
BEGIN
   DECLARE @position int
   SET @position = 1
   SET @string = @string + @separator
   WHILE charindex(@separator,@string,@position) <> 0
      BEGIN
         INSERT into @parsedString
         SELECT substring(@string, @position, charindex(@separator,@string,@position) - @position)
         SET @position = charindex(@separator,@string,@position) + 1
      END
     RETURN
END
go

create table MyTest (
    id int identity,
    keywords nvarchar(256)
)

insert into MyTest
    (keywords)
    select 'sql server,oracle,db2'
    union
    select 'sql server,oracle'
    union
    select 'sql server'

select k.string, COUNT(*) as count
    from MyTest mt
        cross apply dbo.fnParseStringTSQL(mt.keywords,',') k
    group by k.string
    order by count desc

drop function dbo.fnParseStringTSQL
drop table MyTest
于 2010-08-12T21:25:48.403 に答える
1

RemusとJoeはどちらも正しいですが、Joeが言ったように、選択の余地がない場合は、それと一緒に暮らす必要があります。XMLデータ型を使用することで簡単な解決策を提供できると思います。このクエリを実行することで、解析された列をすでに簡単に表示できます

WITH myCommonTblExp AS (
    SELECT CompanyID,
    CAST('<I>' + REPLACE(Keywords, ',', '</I><I>') + '</I>' AS XML) AS Keywords
    FROM Companies
)
SELECT CompanyID, RTRIM(LTRIM(ExtractedCompanyCode.X.value('.', 'VARCHAR(256)'))) AS Keywords
FROM myCommonTblExp
CROSS APPLY Keywords.nodes('//I') ExtractedCompanyCode(X)

これができることがわかったので、グループ化してカウントするだけですが、XMLメソッドをグループ化することはできないため、上記のクエリのビューを作成することをお勧めします。

CREATE VIEW [dbo].[DissectedKeywords]
AS
WITH myCommonTblExp AS (
    SELECT 
    CAST('<I>' + REPLACE(Keywords, ',', '</I><I>') + '</I>' AS XML) AS Keywords
    FROM Companies
)
SELECT RTRIM(LTRIM(ExtractedCompanyCode.X.value('.', 'VARCHAR(256)'))) AS Keywords
FROM myCommonTblExp
CROSS APPLY Keywords.nodes('//I') ExtractedCompanyCode(X)
GO

そのビューでカウントを実行します

SELECT Keywords, COUNT(*) AS KeyWordCount FROM DissectedKeywords
GROUP BY Keywords
ORDER BY Keywords

とにかくここに完全な記事があります-> http://anyrest.wordpress.com/2010/08/13/converting-parsing-delimited-string-column-in-sql-to-rows/

于 2010-08-12T23:24:45.913 に答える