1

I'm using Sybase V11 and I'm encountering an issue I hope you can help me with.

I have 3 products and services associated to these products. They are in 3 tables, one for products, one for services and one for the relations.

The products:

IdProducto  Nombre              
[char    ]  [char              ]
----------  --------------------
1           Telemática Conductor
2           Telemática Empresas 
3           EComparte

The services:

IdServicio  Nombre                              Precio
[char    ]  [char                             ] [money

1           Dongle                              10.50
2           Fleet Intelligence                  5.00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
3           App. Móvil                          3.00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
4           App. Gestor                         13.75                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
5           Web Usuarios                        13.75                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
6           Id Conductor                        7.50

The Relations:

IdProducto  IdServicio  Opcional
[char    ]  [char    ]  [bit   ]
1           1           0       
1           3           0       
2           1           0       
2           2           0       
2           3           1       
2           6           1       
3           1           0       
3           4           0       
3           5           0       
3           6           0       
3           2           1

What I want to achieve is something like this but dynamically:

Name                                            Price                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
[char                                         ] [money                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
EComparte                                       45.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
EComparte / Fleet Intelligence                  50.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Conductor                            13.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas                             15.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas / App. Móvil                18.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas / App. Móvil / Id Conductor 26.00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas / Id Conductor              23.00

Getting like a list of packs of Products + optional services.

What I did is I used a temporary table:

create table #temp(
Libelle varchar(50) NULL,
Montant TypMonnaie NULL)

insert into #temp
select TP.Nombre, SUM(TS.Precio) 
from SPEESP..TelProductos TP, SPEESP..TelProductosServicios TPS, SPEESP..TelServicios TS 
where TP.IdProducto = TPS.IdProducto and TPS.IdServicio = TS.IdServicio
and TPS.Opcional = 0
group by TP.Nombre

insert into #temp
select Libelle = t.Libelle + ' / ' + TS.Nombre, Montant = t.Montant + TS.Precio 
from #temp t, SPEESP..TelProductos TP, SPEESP..TelProductosServicios TPS, SPEESP..TelServicios TS 
where TP.IdProducto = TPS.IdProducto and TPS.IdServicio = TS.IdServicio and TP.Nombre = t.Libelle
and TPS.Opcional = 1

With these queries I get :

Libelle                             Montant                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
[char                            ]  [money                                                                                                                                                                                                                                                                                                                                                                            
EComparte                           45.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
EComparte / Fleet Intelligence      50.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Conductor                13.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas                 15.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas / App. Móvil    18.50                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
Telemática Empresas / Id Conductor  23.00                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

But I don't know how to obtain this line:

Telemática Empresas / App. Móvil / Id Conductor 26.00

And anyway, It must be dynamical. Like if an optional service is later added the new lines must be returned. like :

Telemática Empresas / App. Móvil / Id Conductor / New Service 26.00 + New Price

I would really appreciate any help you could give me.

Regards Henri

Mike Gardner
  • 6,611
  • 5
  • 24
  • 34

1 Answers1

0

It has been a while since you asked this but in case anyone stumbles upon it, the following topic might help.

How to use GROUP BY to concatenate strings in SQL Server?

Basically, you use the FOR XML functionality to STUFF the column with the additional fields which are aggregated by a GROUP BY.

The below quoted came from the user Kevin Fairchild

SELECT 
  [ID],
  STUFF((
    SELECT ', ' + [Name] + ':' + CAST([Value] AS VARCHAR(MAX)) 
    FROM #YourTable 
    WHERE (ID = Results.ID) 
    FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
  ,1,2,'') AS NameValues
FROM #YourTable Results
GROUP BY ID

Adjust as needed to fit your situation. I hope that at least pushes you or others in the right direction!

Community
  • 1
  • 1
CMoltedo
  • 3
  • 1
  • 5