Link to home
Start Free TrialLog in
Avatar of gudidi
gudidiFlag for Israel

asked on

cross tab sql query

Hi  Experts

is there any simple query to make a cross tab?

my data looks like this:

id     name
1      gidi1
1      gidi2
1      gidi3
2      gidi4
2      gidi5

and i want it to look like that:

id          name1   name2    name3
1           gidi1      gidi2       gidi3
2           gidi4      gidi5

10x a lot
Avatar of gudidi

ASKER

hi

the main problem is that i cannot use aggregate function for varchar column.
how do i solve this problem?
you can use it... try to use it, it will work
SOLUTION
Avatar of Shiv
Shiv
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudidi

ASKER

i will check it and let you know
changes for above function with your desired table (i avoided the cursors as it kills time) :

line 16 :replace your column id

line 17 : replace your table

line 27 : don't replace "e" -that is table alias name ..you can replace name with your coulmn name

line 28 : replace your column name

line 29 : replace your table name

that's it.. create function and use select query from code snippet
in the code ,replace your cloumn names and tables
select distinct id,[dbo].[Concat_name] (id) from  [dbo].[tbSample]
group by id ,name

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Sharath S
Sharath S
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial