473,421 Members | 1,500 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,421 software developers and data experts.

Transpose a query result

I have a table which contains ID and Email col.
The data is stored as follows:
ID Email
---------------
1 A
2 A0
2 A1
3 A2
3 A3
3 A4
4 A5

I want to show the value as

ID Email1 Email2 Email3
1 A
2 A1 A0
3 A2 A3 A4
4 A5

Is there any way possible to do this
Sep 7 '10 #1
2 2874
gpl
152 100+
See my response (#2) in http://bytes.com/topic/sql-server/an...end-fields-sql
this will very nearly do what you want, otherwise look here: http://www.sqlteam.com/search.aspx?c...=crosstab#1161 which is a search page with links to articles about doing crosstabs

Good Luck
Graham
Sep 7 '10 #2
ck9663
2,878 Expert 2GB
Here's something that will not require dynamic sql...

Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @tbl_email table (id int, email varchar(20))
  3. insert into @tbl_email 
  4. select 1, 'A'
  5. union all
  6. select 2, 'A0'
  7. union all
  8. select 2, 'A1'
  9. union all
  10. select 3, 'A2'
  11. union all
  12. select 3, 'A3'
  13. union all
  14. select 3, 'A4'
  15. union all
  16. select 4, 'A5'
  17.  
  18. select * from @tbl_email
  19.  
  20. ;with Numbered
  21. as 
  22.    (
  23.       select       
  24.          rownum = row_number() over(partition by id order by id, email),
  25.          id, email
  26.       from @tbl_email
  27.    )
  28. ,FirstRecords
  29. as 
  30.    (
  31.       select rownum, id, email from Numbered where rownum = 1
  32.    )
  33. ,ReturnAllRecords
  34. as
  35.    (
  36.       select rownum, id, cast(email as varchar(500)) as email_list from FirstRecords  
  37.       union all      
  38.       select Numbered.rownum, Numbered.id,  cast(email_list + ', ' + Numbered.email as varchar(500))
  39.       from Numbered
  40.          inner join ReturnAllRecords 
  41.             on ReturnAllRecords.id = Numbered.id and
  42.                ReturnAllRecords.rownum + 1 = Numbered.Rownum
  43.    )
  44. select id, max(email_list)
  45. from ReturnAllRecords
  46. group by id
  47.  
  48.  
Make sure to create the necessary index.


Happy Coding!!!

~~ CK
Sep 7 '10 #3

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

Similar topics

2
by: Lin Ma | last post by:
Greetings, In my search application, user can type a number to search. I use LIKE in my query. If a query result generates over 10,000 recordsets, it may several minutes to run. Is there a...
11
by: Surajit Laha | last post by:
I am firing a query like: SELECT TaskName, StartDate FROMTasks WHERE StartDate >= '01-Aug-2003' Now the result comes as: TaskName StartDate -------------------------- Task1 ...
2
by: Wei Wang | last post by:
Hi, I want to do a select in dynamic command, something like: TRIGGER FUNCTION DECLARE table_name_suffix text; temp_result RECORD; temp_result2 RECORD;
2
by: Martin Sarsale | last post by:
Dear All: Im looking for solutions (Free Software is better) to do query result caching. Thanks to the people from #postgresql I know that postgres doesn't do that by himself and the solution...
1
by: RookieDan | last post by:
Greetings fellow Accessers! Im new but in Access, but I have some background in different coding. I have a programme loading customer data into Access belonging to BMW dealers in Europe. ...
1
by: Maarten van der Cammen | last post by:
Hi, In a table I have two numeric fields (e.g. FieldA and FieldB). Both fields have double precicion (field size "double"), Format "Fixed" and Decimal places "3" since I want to calculate with...
7
by: Muddasir | last post by:
i am having problem in printing the query result from MySQL db... actually i am developing a very simple search module. when the user select category from the given categories in drop down...
1
by: CCHDGeek | last post by:
How can I tell if a query result empty (ie there are no records with the specified criteria). I want to change a form's design based on the result of the query it is based how. Does anyone know how...
1
ddtpmyra
by: ddtpmyra | last post by:
how can I capture the query result in PHP? I have two queries below: # Fetch the file information $query ="update filestorage set approved ='Y' where FileID = {$id}"; $query1 ="select...
2
by: reeba | last post by:
I want to store the query result, in an servlet, into an xml file and display the contents of the xml file on the browser...... my code is as follows: public void doPost(HttpServletRequest...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.