Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Cross Tab in sql 2008

Posted on 2012-04-06
2
Medium Priority
?
244 Views
Last Modified: 2012-06-27
I am new to SQL SERVER 2008. I have a table tblUserInterest (id, User_Id, Interest_word, Weight) And other table as tblUser (id, UserName, Password, UserType)

I have data in tblUserInterest as,
id  User_Id   Interest_word                     Weight
--------------------------------------------------------------------------------
1        3         www.cooltoad.com                   6
2        3         www.google.co.in                     3
3        2         www.cooltoad.com                   1
4        2         www.google.co.in                     18

User 2 : Swati
User 3 : Sonali
I want to attach a result to grid which should be in format


UserName       www.cooltoad.com        www.google.co.in                               
---------------------------------------------------------------------------------------------
Swati                          1                                  18
Sonali                         6                                    3

How it can be get this rows in table are tblUserInterest  can increase with diffrent Interest_word?

Thanks in Advance.
0
Comment
Question by:swatiadeshpande
2 Comments
 
LVL 43

Expert Comment

by:Eugene Z
ID: 37815824
use Pivot operator   (more Using PIVOT  http://msdn.microsoft.com/en-us/library/ms177410.aspx)

try this below basic  code  (if you need more - use dynamic code)



   SELECT *
   FROM (
   select case when USER_ID=2 then 'Swati'
              when USER_ID=3 then 'Sonali ' end UserName , Interest_word ,  Weight from tblUserInterest
   ) AS PivotData
   PIVOT (
     max(Weight)
     FOR Interest_word  IN (
       [www.cooltoad.com],[www.google.co.in]
     )
   ) AS PivotTable
0
 
LVL 7

Accepted Solution

by:
Cboudroz earned 2000 total points
ID: 37816352
The only way I know without having to "hardcode" the Interest_word it's whit a loop and temp table.

I will be happy to see someone do it without a loop!!

CREATE TABLE tblUser 
(
	id int
	, username varchar(150)
)
CREATE TABLE tblUserInterest
(
	iUser_id int
	, Interest_word varchar(150)
	, Weight int
)

INSERT INTO tblUser
SELECT 1, 'TEST1' UNION ALL 
SELECT 2, 'TEST2' UNION ALL 
SELECT 3, 'TEST3' 

INSERT INTO tblUserInterest
SELECT 1, 'INTEREST1', 100 UNION ALL 
SELECT 1, 'INTEREST2', 150 UNION ALL 
SELECT 1, 'INTEREST3', 110 UNION ALL 
SELECT 2, 'INTEREST1', 110 UNION ALL 
SELECT 2, 'INTEREST3', 100 


DROP TABLE #final_Results
CREATE TABLE #final_Results
(
	iUser_id int
	, UserName varchar(150)
)

INSERT INTO #final_results
(
	iUser_id
	, UserName
)
SELECT DISTINCT
	id
	, UserName
FROM 
	tblUser 



DECLARE @Interest_word	VARCHAR(255)
DECLARE @TSQL			VARCHAR(MAX)

DECLARE cur_Interest	CURSOR
FOR 
	SELECT DISTINCT
		Interest_word
	FROM 
		tblUserInterest 

OPEN cur_Interest


FETCH NEXT FROM cur_Interest INTO @Interest_word
	


WHILE @@FETCH_STATUS = 0 
BEGIN 

	-- ADD Interest_word as column
	SET @TSQL = 'ALTER TABLE #final_Results ADD '+@Interest_word+' INT NULL'
	EXECUTE (@TSQL)
	
	-- UPDATE Weight
	SET @TSQL = 
	'
		UPDATE T
			SET '+@Interest_word+' = ISNULL(Weight,0)
		FROM 
			#final_results T
			LEFT JOIN tblUserInterest I
				ON I.iUser_Id = T.iUser_Id
				AND I.Interest_word = ''' + @Interest_word + ''' 
			
	'
	EXECUTE (@TSQL)
	

	FETCH NEXT FROM cur_Interest INTO @Interest_word
END

CLOSE cur_Interest
DEALLOCATE cur_Interest


SELECT 
	*
FROM 
	#final_results

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question