Split statistic string

Source: Internet
Author: User
Statistics | strings
In the database table Tbl1 there is a field keywords, which is a nvarchar type with a length of 1000, the content of which is the keyword ID keywords of the paper to be analyzed------------------------                          -----------------------------------1 Kw1;kw2;kw32 kw2;kw33 Kw3;kw1;kw4

Question 1. For any of the keywords that appear in the keywords field (the keyword collection in the previous example is {KW1,KW2,KW3,KW4}), count the number of times it appears (that is, the number of bars that contain the key's record), and then write to another table.              The final effect is keywords Count-------------------------kw1 2kw2 2kw3 3kw4 1

Question 2. On this basis, the combination of query. That is, you arbitrarily extract two keywords from the entire set of keywords to count the number of times they appear in the database table record. For the above question, the final effect if: keywords Count----------------------------------kw1;kw2 1kw1;kw3 2kw 1;kw4 1kw2;kw3 2kw2;kw4 0kw3;kw4 1

--------------------------------------------------------------------------------------

--statistical example

--a sequence table specifically made for statistical processing select Top 1000 id=identity (int,1,1) into the order tables from syscolumns a,syscolumns balter tables ordinal numbers add constraint p K_id_ ordinal table primary key (ID) go

--Sample Data CREATE TABLE TBL1 (id int,keywords nvarchar (1000)) Insert TBL1 Select 1, ' KW1;KW2;KW3 ' union ALL Select 2, ' KW2;KW3 ' uni On all Select 3, ' Kw3;kw1;kw4 ' Go

--The first statistic (count) Select Keyword=substring (A.keywords,b.id,charindex ('; ', a.keywords+ '; ', b.id)-b.id), [Count]=count ( Distinct a.id) from TBL1 A, ordinal tables bwhere B.id<=len (a.keywords) and substring ('; ') +a.keywords,b.id,1) = '; ' GROUP BY SUBSTRING (A.keywords,b.id,charindex ('; ', a.keywords+ '; ", b.id)-b.id) go

--The second statistic (combinatorial statistics) Select Keyword=substring (A.keywords,b.id,charindex ('; ', a.keywords+ '; ', b.id)-b.id), [Count]=count ( Distinct a.id), A.idinto #tfrom tbl1 A, ordinal tables bwhere B.id<=len (a.keywords) and substring ('; ') +a.keywords,b.id,1) = '; ' GROUP BY SUBSTRING (A.keywords,b.id,charindex ('; ', a.keywords+ '; ', b.id)-b.id), a.id

Select keyword=a.keyword+ '; ' +b.keyword,[count]=sum (case a.id when b.id then 1 else 0 ") from #t A, #t bwhere A.keyword<b.keywordgroup by A.keyword, B.keywordorder by keyword

drop table #tgo

--Delete test environment drop table TBL1, ordinal tables

/*--test Results

--Statistics 1keyword count------------------kw1 2kw2 2kw3 3kw4 1

(The number of rows affected is 4 rows)

--Statistics 2keyword count----------------------------------kw1;kw2 1kw1;kw3 2kw1;kw4 1kw2;kw3 2kw2;kw4 0kw3;kw4 1

(The number of rows affected is 6 rows)--*/


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.