积极答复者
这个Sql如何写?

问题
-
KeyWord Engine SimilarKeyword故宫 google 故宫博物院故宫 google 故宫大使馆故宫 baidu 故宫地图故宫 baidu 故宫百度故宫 soso 故宫搜来故宫 soso 故宫大使馆长城 google google长城1长城 google google长城2长城 baidu 百度长城1长城 baidu 百度长城2长城 soso soso长城1长城 soso soso长城2
表如上所示。
现在要的结果如下:Keyword, GoogleKeyword,BaiduKeyword,SosoKeyword故宫 故宫博物院 故宫地图 故宫搜来故宫 故宫大使馆 故宫百度 故宫大使馆长城 google长城1 百度长城1 soso长城1长城 google长城2 百度长城2 soso长城2
请问大大们这个Sql如何写?
答案
-
KeyWord Engine SimilarKeyword故宫 google 故宫博物院故宫 google 故宫大使馆故宫 baidu 故宫地图故宫 baidu 故宫百度故宫 soso 故宫搜来故宫 soso 故宫大使馆长城 google google长城1长城 google google长城2长城 baidu 百度长城1长城 baidu 百度长城2长城 soso soso长城1长城 soso soso长城2
表如上所示。
现在要的结果如下:Keyword, GoogleKeyword,BaiduKeyword,SosoKeyword故宫 故宫博物院 故宫地图 故宫搜来故宫 故宫大使馆 故宫百度 故宫大使馆长城 google长城1 百度长城1 soso长城1长城 google长城2 百度长城2 soso长城2
请问大大们这个Sql如何写?
------------------------------------ -- Author: flystone (<a href="http://blog.csdn.net/happyflystone)">http://blog.csdn.net/happyflystone) </a>-- Version:V1.001 -- Date:2010-09-19 ------------------------------------ -- Test Data: ta If object_id('ta') is not null Drop table ta Go Create table ta(KeyWord nvarchar(2),Engine nvarchar(6),SimilarKeyword nvarchar(9)) Go Insert into ta select '故宫','google','故宫博物院' union all select '故宫','google','故宫大使馆' union all select '故宫','baidu','故宫地图' union all select '故宫','baidu','故宫百度' union all select '故宫','soso','故宫搜来' union all select '故宫','soso','故宫大使馆' union all select '长城','google','google长城1' union all select '长城','google','google长城2' union all select '长城','baidu','百度长城1' union all select '长城','baidu','百度长城2' union all select '长城','soso','soso长城1' union all select '长城','soso','soso长城2' Go --Start declare @s varchar(1000) select @s = isnull(@s+',','')+'['+engine+'Keyword]=max(case when engine = '''+engine+''' then similarkeyword else null end)' from (select distinct engine from ta) a exec('select keyword,'+ @s+' from (SELECT *,(SELECT COUNT(1) FROM TA WHERE KEYWORD = A.KEYWORD AND Engine = A.Engine AND SIMILARKEYWORD<A.SIMILARKEYWORD ) AS RID FROM TA A) B group by keyword,RID ORDER BY KEYWORD') --Result: /* keyword baiduKeyword googleKeyword sosoKeyword ------- ------------ ------------- ----------- 长城 百度长城1 google长城1 soso长城1 长城 百度长城2 google长城2 soso长城2 故宫 故宫百度 故宫博物院 故宫大使馆 故宫 故宫地图 故宫大使馆 故宫搜来 */ --End
More: blog.csdn.net/happyflystone- 已标记为答案 steven_lsy 2010年9月19日 5:49
全部回复
-
KeyWord Engine SimilarKeyword故宫 google 故宫博物院故宫 google 故宫大使馆故宫 baidu 故宫地图故宫 baidu 故宫百度故宫 soso 故宫搜来故宫 soso 故宫大使馆长城 google google长城1长城 google google长城2长城 baidu 百度长城1长城 baidu 百度长城2长城 soso soso长城1长城 soso soso长城2
表如上所示。
现在要的结果如下:Keyword, GoogleKeyword,BaiduKeyword,SosoKeyword故宫 故宫博物院 故宫地图 故宫搜来故宫 故宫大使馆 故宫百度 故宫大使馆长城 google长城1 百度长城1 soso长城1长城 google长城2 百度长城2 soso长城2
请问大大们这个Sql如何写?
------------------------------------ -- Author: flystone (<a href="http://blog.csdn.net/happyflystone)">http://blog.csdn.net/happyflystone) </a>-- Version:V1.001 -- Date:2010-09-19 ------------------------------------ -- Test Data: ta If object_id('ta') is not null Drop table ta Go Create table ta(KeyWord nvarchar(2),Engine nvarchar(6),SimilarKeyword nvarchar(9)) Go Insert into ta select '故宫','google','故宫博物院' union all select '故宫','google','故宫大使馆' union all select '故宫','baidu','故宫地图' union all select '故宫','baidu','故宫百度' union all select '故宫','soso','故宫搜来' union all select '故宫','soso','故宫大使馆' union all select '长城','google','google长城1' union all select '长城','google','google长城2' union all select '长城','baidu','百度长城1' union all select '长城','baidu','百度长城2' union all select '长城','soso','soso长城1' union all select '长城','soso','soso长城2' Go --Start declare @s varchar(1000) select @s = isnull(@s+',','')+'['+engine+'Keyword]=max(case when engine = '''+engine+''' then similarkeyword else null end)' from (select distinct engine from ta) a exec('select keyword,'+ @s+' from (SELECT *,(SELECT COUNT(1) FROM TA WHERE KEYWORD = A.KEYWORD AND Engine = A.Engine AND SIMILARKEYWORD<A.SIMILARKEYWORD ) AS RID FROM TA A) B group by keyword,RID ORDER BY KEYWORD') --Result: /* keyword baiduKeyword googleKeyword sosoKeyword ------- ------------ ------------- ----------- 长城 百度长城1 google长城1 soso长城1 长城 百度长城2 google长城2 soso长城2 故宫 故宫百度 故宫博物院 故宫大使馆 故宫 故宫地图 故宫大使馆 故宫搜来 */ --End
More: blog.csdn.net/happyflystone- 已标记为答案 steven_lsy 2010年9月19日 5:49
-
2005的,如果engine不固定你改成动态的
------------------------------------------------------------------------ -- Author : happyflystone -- Date : 2010-09-19 -- Version: Microsoft SQL Server 2005 - 9.00.2047.00 (Intel X86) -- Apr 14 2006 01:12:25 -- Copyright (c) 1988-2005 Microsoft Corporation -- Standard Edition on Windows NT 5.2 (Build 3790: Service Pack 2) -- ------------------------------------------------------------------------ -- Test Data: ta IF OBJECT_ID('[ta]') IS NOT NULL DROP TABLE [ta] Go CREATE TABLE ta([KeyWord] NVARCHAR(2),[Engine] NVARCHAR(6),[SimilarKeyword] NVARCHAR(9)) Go INSERT INTO ta SELECT '故宫','google','故宫博物院' UNION ALL SELECT '故宫','google','故宫大使馆' UNION ALL SELECT '故宫','baidu','故宫地图' UNION ALL SELECT '故宫','baidu','故宫百度' UNION ALL SELECT '故宫','soso','故宫搜来' UNION ALL SELECT '故宫','soso','故宫大使馆' UNION ALL SELECT '长城','google','google长城1' UNION ALL SELECT '长城','google','google长城2' UNION ALL SELECT '长城','baidu','百度长城1' UNION ALL SELECT '长城','baidu','百度长城2' UNION ALL SELECT '长城','soso','soso长城1' UNION ALL SELECT '长城','soso','soso长城2' GO --Start SELECT KEYWORD, [GOOGLE] AS GOOGLEKEYWORD, [BAIDU] AS BAIDUKEYWORD, [SOSO] AS SOSOKEYWORD FROM (SELECT *, RID = ROW_NUMBER() OVER (PARTITION BY KEYWORD, ENGINE ORDER BY SIMILARKEYWORD) FROM TA ) A PIVOT (MAX(SIMILARKEYWORD) FOR ENGINE IN( GOOGLE,BAIDU,SOSO) ) AS PVT ORDER BY KEYWORD --Result: /* KEYWORD GOOGLEKEYWORD BAIDUKEYWORD SOSOKEYWORD ------- ------------- ------------ ----------- 长城 google长城1 百度长城1 soso长城1 长城 google长城2 百度长城2 soso长城2 故宫 故宫博物院 故宫百度 故宫大使馆 故宫 故宫大使馆 故宫地图 故宫搜来 (4 行受影响) */ --End
More: blog.csdn.net/happyflystone