|
導讀數據庫,簡而言之可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數據進行新增、截取、更新、刪除等操作。所謂“數據庫”是以一定方式儲存在一起、能與多個用戶共享、具有盡可能小的冗余度、與應... 數據庫,簡而言之可視為電子化的文件柜——存儲電子文件的處所,用戶可以對文件中的數據進行新增、截取、更新、刪除等操作。所謂“數據庫”是以一定方式儲存在一起、能與多個用戶共享、具有盡可能小的冗余度、與應用程序彼此獨立的數據集合。 在數據庫表tbl1中有一個字段Keywords,它是nvarchar類型,長度為1000,該字段的內容是所要分析的論文的關鍵字 id keywords ----------------------------------------------------------- 1 kw1;kw2;kw3 2 kw2;kw3 3 kw3;kw1;kw4 問題1。 對于在keywords字段中出現的所有關鍵字集合(上例中關鍵字集合為{kw1,kw2,kw3,kw4})中的任意一個關鍵字,要統計它出現的次數(也就是包含該關鍵字的紀錄的條數),然后寫到另一張表中。最后的效果就是 keywords count ------------------------- kw1 2 kw2 2 kw3 3 kw4 1 問題2。 在此基礎上,要進行組合查詢。也就是說在整個關鍵字集合中任意抽出兩個關鍵字,統計它們在數據庫表紀錄中同時出現的次數。對于上題,最后效果要是: keywords count ---------------------------------- kw1;kw2 1 kw1;kw3 2 kw1;kw4 1 kw2;kw3 2 kw2;kw4 0 kw3;kw4 1 -------------------------------------------------------------------------------------- --統計示例 --為統計處理專門做的序數表 select top 1000 id=identity(int,1,1) into 序數表 from syscolumns a,syscolumns b alter table 序數表 add constraint pk_id_序數表 primary key(id) go --示例數據 create table tbl1(id int,keywords nvarchar(1000)) insert tbl1 select 1,’kw1;kw2;kw3’ union all select 2,’kw2;kw3’ union all select 3,’kw3;kw1;kw4’ go --第一種統計(計數) select keyword=substring(a.keywords,b.id,charindex(’;’,a.keywords+’;’,b.id)-b.id) ,[count]=count(distinct a.id) from tbl1 a,序數表 b where 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 --第二種統計(組合統計) select keyword=substring(a.keywords,b.id,charindex(’;’,a.keywords+’;’,b.id)-b.id) ,[count]=count(distinct a.id),a.id into #t from tbl1 a,序數表 b where 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 end) from #t a,#t b where a.keyword<b.keyword group by a.keyword,b.keyword order by keyword drop table #t go --刪除測試環境 drop table tbl1,序數表 /*--測試結果 --統計1 keyword count ---------- -------- kw1 2 kw2 2 kw3 3 kw4 1 (所影響的行數為 4 行) --統計2 keyword count ----------------------- ----------- kw1;kw2 1 kw1;kw3 2 kw1;kw4 1 kw2;kw3 2 kw2;kw4 0 kw3;kw4 1 (所影響的行數為 6 行) --*/ 全新的路由器不僅讓你更穩定快速地連接無線網絡,更可以讓家中的智能設備連接在一起。 |
溫馨提示:喜歡本站的話,請收藏一下本站!