Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 getting 1 to many records to show up on 1 row

Author  Topic 

robertnzana
Starting Member

42 Posts

Posted - 2008-06-13 : 23:17:22
I have a 1 to many table...
"ONE TABLE" - Cars (CarId, CarMake, CarModel)
"MANY TABLE" - Accessories (AccId, CarId, AccessoryTypeId, AccessoryName)
"Reference Table" - AccessoryTypes (AccessoryTypeId, AccessoryType)

Example Data:
Table: Cars Record
10, Honda, Accord

Table: AccessoryTypes
2, Engine
3, Exterior
4, Interior

Table: Accessories
13, 10, 2, SomeValve
15, 10, 3, CoolPaint
17, 10, 4, SomeGadget

I would like a query that results in ONE row only, for example...
CarMake, CarModel, AccessoryType1, AccessoryName1, AccessoryType2, AccessoryName2, AccessoryType3, AccessoryName3

One row only.

Is there a way to do this? Thanks!

Using SQL Server 2000, and ASP.NET/VB.NET

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2008-06-14 : 05:14:22
You Require to write the sp based on this code and then call it ...

and there is an additional function which you require to create for using this.


--Function Script
Create Function Dbo.GetAccessoryType(@AccessoryTypeId Int)
Returns Varchar(10)
As
Begin
Declare @AccessoryType Varchar(10)
Select @AccessoryType = AccessoryType From AccessoryTypes Where AccessoryTypeId = @AccessoryTypeId
return @AccessoryType
End
--Function Script End Here

--Pivot Script
/* Intial Creating Schema */
Create Table Cars
(
CarId Int,
CarMake Varchar(20),
CarModel Varchar(20)
)
Go
Create Table AccessoryTypes
(
AccessoryTypeId Int,
AccessoryType Varchar(20)
)
Go
Create Table Accessories
(
AccId Int ,
CarId Int ,
AccessoryTypeId Int ,
AccessoryName Varchar(20)
)

/* Inserting the Records in the Table for Testing */

Insert Cars
Select 10, 'Honda', 'Accord' Union All
Select 11, 'Toyta', 'Corolla'

Insert AccessoryTypes
Select 2, 'Engine' Union All
Select 3, 'Exterior' Union All
Select 4, 'Interior'

Insert Accessories
Select 13, 10, 2, 'SomeValve' Union All
Select 15, 10, 3, 'CoolPaint' Union All
Select 17, 10, 4, 'SomeGadget' Union All
Select 18, 11, 2, 'SomeGadget'


-- Processing of the Query
Declare @sQry Varchar(8000),
@AccessoryTypeId Int ,
@Counter Int

Select @sQry = 'Select C.CarMake,C.CarModel, ', @AccessoryTypeId =0 , @Counter=0

While @AccessoryTypeId < (Select Distinct Max(AccessoryTypeId) From Accessories )
Begin
Select @AccessoryTypeId = Min(AccessoryTypeId) From Accessories Where AccessoryTypeId > @AccessoryTypeId
Select @Counter = @Counter+1
Select @sQry = @sQry + 'Max(Case When AccessoryTypeId = ' + Cast(@AccessoryTypeId As Varchar(10)) +
' Then AccessoryTypeId End ) As AccessoryTypeId' + Cast (@Counter As Varchar(10)) +
', Max(Case When AccessoryTypeId = ' + Cast(@AccessoryTypeId As Varchar(10)) +
' Then Dbo.GetAccessoryType(AccessoryTypeId) End ) As AccessoryTypes' + Cast (@Counter As Varchar(10)) + ', '
End

Select @sQry = left(@sQry,len(@sQry)-1)
Select @sQry = @sQry + ' From Accessories A Inner Join Cars C On A.CarId = C.CarId Group by CarMake, CarModel'
-- Output...
Exec(@sQry)
print @sQry

---Droping the Table
--Drop Table Cars
--Drop Table AccessoryTypes
--Drop Table Accessories

--Pivot Scripts End here..


Chirag

http://www.chirikworld.com
Go to Top of Page
   

- Advertisement -