473,898 Members | 4,293 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Transposing rows to columns

I hope someone can help me with my problem. I have searched the
internet for days for a solution, but nothing i found seemed to work.
The following is what i have now:

appartmentid code
100306 Wip
100306 Zandbak
100306 Glijbaan
100306 Klimrek
100306 Schommel
100321 Glijbaan
100321 Schommel

This results are made with this query:

select
appartment.appa rtmentid,
listvalue.code
from appartment
inner join appartmentlistv alue on appartmentlistv alue.appartment id =
appartment.appa rtmentid
inner join listvalue on appartmentlistv alue.listvaluei d =
listvalue.listv alueid

The following is what i need:

100306 Wip, Zandbak, Glijbaan, Klimrek, Schommel
100312 Glijbaan, Schommel

As you can see is this example, not all appartments have the same
number of results. Can anyone tell me if this is possible?

Many thanks,
Sakymoto

Jul 31 '06 #1
5 3914

"SakyMoto" <je***********@ gmail.comwrote in message
news:11******** **************@ s13g2000cwa.goo glegroups.com.. .
I hope someone can help me with my problem. I have searched the
internet for days for a solution, but nothing i found seemed to work.
The following is what i have now:

What version of SQL are you using?

SQL 2005 has a PIVOT commandbuilt in.

Otherwise you can google for some code examples of how to do it in SQL 2000
(it ain't overly pretty.)

>
appartmentid code
100306 Wip
100306 Zandbak
100306 Glijbaan
100306 Klimrek
100306 Schommel
100321 Glijbaan
100321 Schommel

This results are made with this query:

select
appartment.appa rtmentid,
listvalue.code
from appartment
inner join appartmentlistv alue on appartmentlistv alue.appartment id =
appartment.appa rtmentid
inner join listvalue on appartmentlistv alue.listvaluei d =
listvalue.listv alueid

The following is what i need:

100306 Wip, Zandbak, Glijbaan, Klimrek, Schommel
100312 Glijbaan, Schommel

As you can see is this example, not all appartments have the same
number of results. Can anyone tell me if this is possible?

Many thanks,
Sakymoto

Jul 31 '06 #2
SakyMoto (je***********@ gmail.com) writes:
I hope someone can help me with my problem. I have searched the
internet for days for a solution, but nothing i found seemed to work.
The following is what i have now:

appartmentid code
100306 Wip
100306 Zandbak
100306 Glijbaan
100306 Klimrek
100306 Schommel
100321 Glijbaan
100321 Schommel

This results are made with this query:

select
appartment.appa rtmentid,
listvalue.code
from appartment
inner join appartmentlistv alue on appartmentlistv alue.appartment id =
appartment.appa rtmentid
inner join listvalue on appartmentlistv alue.listvaluei d =
listvalue.listv alueid

The following is what i need:

100306 Wip, Zandbak, Glijbaan, Klimrek, Schommel
100312 Glijbaan, Schommel

As you can see is this example, not all appartments have the same
number of results. Can anyone tell me if this is possible?
On SQL 2005 you can use:

SELECT a.appartmentid,
substring(x.Cod eList, 1, len(CodeList) - 1)
FROM appartment a
CROSS APPLY (select l.code + ',' AS [text()]
from appartmentlistv alue alv ond
join listvalue l on alv.listvalueid = l.listvalueid
WHERE alv.appartmenti d = a.appartmenti
ORDER BY a.appartmentid
FOR XML PATH ('')) AS x(CodeList)

The syntax is a bit obscure, but it works.

On SQL 2000, you cannot do this in a single SQL statement, but you need to
run a cursor to build the lists. You may be better off to compose the
lists in the client layer.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Jul 31 '06 #3

Erland Sommarskog schreef:
SakyMoto (je***********@ gmail.com) writes:
I hope someone can help me with my problem. I have searched the
internet for days for a solution, but nothing i found seemed to work.
The following is what i have now:

appartmentid code
100306 Wip
100306 Zandbak
100306 Glijbaan
100306 Klimrek
100306 Schommel
100321 Glijbaan
100321 Schommel

This results are made with this query:

select
appartment.appa rtmentid,
listvalue.code
from appartment
inner join appartmentlistv alue on appartmentlistv alue.appartment id =
appartment.appa rtmentid
inner join listvalue on appartmentlistv alue.listvaluei d =
listvalue.listv alueid

The following is what i need:

100306 Wip, Zandbak, Glijbaan, Klimrek, Schommel
100312 Glijbaan, Schommel

As you can see is this example, not all appartments have the same
number of results. Can anyone tell me if this is possible?

On SQL 2005 you can use:

SELECT a.appartmentid,
substring(x.Cod eList, 1, len(CodeList) - 1)
FROM appartment a
CROSS APPLY (select l.code + ',' AS [text()]
from appartmentlistv alue alv ond
join listvalue l on alv.listvalueid = l.listvalueid
WHERE alv.appartmenti d = a.appartmenti
ORDER BY a.appartmentid
FOR XML PATH ('')) AS x(CodeList)

The syntax is a bit obscure, but it works.

On SQL 2000, you cannot do this in a single SQL statement, but you need to
run a cursor to build the lists. You may be better off to compose the
lists in the client layer.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
It's a shame but we haven't yet migrated to SQL server 2005. I will try
to accomplish the result with a cursor

Aug 1 '06 #4
Hi,

Here is an example that may meet your needs. It's a bit rough but it
does work.
USE Northwind
GO
CREATE FUNCTION dbo.ConcatOrder Products(@Order ID int)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Output VARCHAR(8000)
SELECT @Output = COALESCE(@Outpu t+', ', '') + CONVERT(varchar (20),
P.ProductName)
FROM dbo.[Order Details] OD
JOIN dbo.Products P
ON P.ProductID = OD.ProductID
WHERE OD.OrderID = @OrderID
ORDER BY P.ProductName

RETURN @Output
END
GO

SELECT OrderID, CustomerID, dbo.ConcatOrder Products(OrderI D)
FROM Orders
GO
DROP FUNCTION dbo.ConcatOrder Products
GO
thanks

Bryan

SakyMoto wrote:
I hope someone can help me with my problem. I have searched the
internet for days for a solution, but nothing i found seemed to work.
The following is what i have now:

appartmentid code
100306 Wip
100306 Zandbak
100306 Glijbaan
100306 Klimrek
100306 Schommel
100321 Glijbaan
100321 Schommel

This results are made with this query:

select
appartment.appa rtmentid,
listvalue.code
from appartment
inner join appartmentlistv alue on appartmentlistv alue.appartment id =
appartment.appa rtmentid
inner join listvalue on appartmentlistv alue.listvaluei d =
listvalue.listv alueid

The following is what i need:

100306 Wip, Zandbak, Glijbaan, Klimrek, Schommel
100312 Glijbaan, Schommel

As you can see is this example, not all appartments have the same
number of results. Can anyone tell me if this is possible?

Many thanks,
Sakymoto
Aug 3 '06 #5
Bryan (br**********@b tinternet.com) writes:
Here is an example that may meet your needs. It's a bit rough but it
does work.
USE Northwind
GO
CREATE FUNCTION dbo.ConcatOrder Products(@Order ID int)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE @Output VARCHAR(8000)
SELECT @Output = COALESCE(@Outpu t+', ', '') + CONVERT(varchar (20),
P.ProductName)
FROM dbo.[Order Details] OD
JOIN dbo.Products P
ON P.ProductID = OD.ProductID
WHERE OD.OrderID = @OrderID
ORDER BY P.ProductName
Nah, it seems to work, but there is not really any guarantee. It relies
on undefined behaviour, and you may get the result you expect.
--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4728
by: Fabio | last post by:
Hello, is there any quite easy solution for the problem of transposing the rows into the columns? I have the following table with some data in it: dealer date 09.00 10.00 11.00 12.00 13.00 14.00 ----------------------------------------------------------------- 1 2004-10-01 1/1 2/3 3/3 3/4 4/5 0/3 1 2004-10-02 0/1 1/3 1/3 1/4 3/5 1/3
8
2870
by: Leszek Gruszka | last post by:
Hello! I need to transpose some columns into rows and rows into columns. I know, tha i can do it by cursor, but i don't know how make it... I read a lot about it, but still don't understand... Can someone help me? *** Sent via Developersdex http://www.developersdex.com ***
2
6273
by: Howard William | last post by:
Help. I am a bit flummoxed by the problem of how to transpose "normalized" (in the database sense) data records into columns of for a data entry form, and then back again when the user is finished. My previous solution to this was to filter by query or SQL for the observations for a particular set of criteria, then go through the resulting records and assign them to a non-normalized work table containing ONE RECORD with the rows x...
3
4896
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that the best method? Do you have a sample of how to do this?
68
6858
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
10681
by: Classic-Car-World Ltd | last post by:
Hi, I have some data in a table in columns which I need to convert into rows. This is specification data for some tools etc. The data is currently in the following format: Product No, order, description, value 1000, 1, model No, Ak272 1000, 2, Size, 10mm 1000, 3, Length, 20mm 1001, 1, Model No, Ak273
1
2106
by: trizub | last post by:
I have a table of populations of cities (identified by country, state, cities) on particular dates. How do I transpose the date values in my rows to a date value column that lists populations for that date. In other words go from this: Date Country State City Population 12/20 US IL Flora 1432 12/19 US IL Flora 1427 12/18 US IL Flora 1425 12/20 US AR Flora 1432
1
1486
by: TimHop12 | last post by:
Lets consider the following query: Select Col1, Col2 from Table1 where Colx = 'A5100650867' This gives result as: Col1 Col2 E-mail Address xyz@GMAIL.com Web Site www.GOOGLE.com I want the outcome as:
11
2209
by: Haydee Zimmerman | last post by:
Hoping someone can help me. I have 2 Access tables with a one to many relationship. the 1 side table holds the billing information, the many table side holds the billing id and modifier#. Now I need to transpose the data into: billing id, modifier1, and modifier 2. If I do a crosstab query I get different modifiers as column headings but I only want the 2 columns. for example: billing id modifier 1 modifier2 1100 50...
2
1646
by: owuraku | last post by:
Hi guys I am have two excel workbooks. One book has data formatted according rows. Example: ROW 1: Loan # ; Principal Amount ; Interest Rate ; Date The other book needs to be formatted so that it contains a column that looks like: COLUMN:
0
9993
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9842
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11265
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10954
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9662
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8036
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5882
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4297
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3308
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.