473,388 Members | 1,405 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

how we can display two columns according to the same rows in each column rowwise

our database is

district field
highland rookies
volcano peewees
volcano rookies
noida mighty
noida peewees
ghaz rookies
ghaz freshman


now i want my output like this:

district rookies peewees mighty freshman
highland 1 null null null
volcano 1 1 null null
noida null 1 1 null
ghaz 1 null null 1
Feb 12 '10 #1
2 1607
ck9663
2,878 Expert 2GB
Check PIVOT/UNPIVOT in BOL.

Happy Coding !!!

~~ CK
Feb 12 '10 #2
nbiswas
149 100+
Try something like this

create table #t (district varchar(50),field varchar(50))
insert into #t
select 'highland','rookies' union all select 'volcano','peewees' union all
select 'volcano','rookies' union all select 'noida','mighty' union all
select 'noida','peewees' union all select 'ghaz','rookies' union all
select 'ghaz','freshman'
--select * from #t
--Program starts
declare @col_list varchar(max)
declare @dynquery varchar(max)
select distinct IsNull(@col_list,'') + ',[' + cast(field as varchar(50)) + ']' as list into #temp from #t
select @col_list = stuff(namelist,1,1,'') from(select cast(list as varchar(max)) from #temp for xml path (''))X(namelist)
set @dynquery = 'select district,' + @col_list + 'from #t pivot ( max(field) for field in (' + @col_list + ') ' + ') as pvt'
exec (@dynquery)
--Program ends
drop table #temp
drop table #t
Feb 15 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: Rich | last post by:
I'm sure it sounds kinda nutty to display 200 columns and 500,000 rows of data. But I have been pulling data from a Lotus Notes database into Sql Server for a while now, but Lotus Notes is...
10
by: VA | last post by:
I got the following function to swap table columns from somewhere on the Internet function swapColumns (table, colIndex1, colIndex2) { if (table && table.rows && table.insertBefore && colIndex1...
18
by: Alpha | last post by:
Hi, I'm working on a Windows applicaton with VS 2003 on windows 2000. I have a listbox that I have binded to a dataset table, "source" which has 3 columns. I would like to display 2 of those...
4
by: Joe Van Meer | last post by:
Hi, Is the datalist meant for one table or can I use it to display information from 2 different tables? Or should I be looking at a datagrid instead? I began my code and kinda ran into a snag...
9
by: tshad | last post by:
I have a datagrid that I want to add a new column to. This column will only be visible under certain conditions. So I want to set the column visible=false. Then when the right condition happens...
2
by: CSL | last post by:
I am using the DataGrid in a Windows Application, how can I adjust the widths of each column individually.
13
by: scorpion53061 | last post by:
Very urgent and I am very close but need a little help to get me over the edge........ I need to write these columns to a html file with each row containing these columns (seperated by breaks)....
2
by: Stephen | last post by:
Hi, Can a datagrid display be split into 2 rows. for e.g: suppose my resultset has 20 columns and I want to display 10 in the first row and and rest in another so that the display is not kludgy....
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
9
by: Ben R. | last post by:
Hi guys, I've got a DB table of timecards with these fields in the table: ID (Int) UserID (Int) DateWorked (DateTime) HoursWorkedOnThatDate (Double) I'd like to display a grid, with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.