简体   繁体   English

如何串联不同表中的列

[英]How to concatenate columns from different tables

I'm using Sybase V11 and I'm encountering an issue I hope you can help me with. 我正在使用Sybase V11,遇到一个问题,希望您能为我提供帮助。

I have 3 products and services associated to these products. 我有3个产品以及与这些产品相关的服务。 They are in 3 tables, one for products, one for services and one for the relations. 它们在3个表中,一个用于产品,一个用于服务,一个用于关系。

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 问候亨利

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? 如何使用GROUP BY连接SQL Server中的字符串?

Basically, you use the FOR XML functionality to STUFF the column with the additional fields which are aggregated by a GROUP BY. 基本上,您使用FOR XML功能将列与由GROUP BY聚合的其他字段进行存储。

The below quoted came from the user Kevin Fairchild 以下引用来自用户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! 我希望至少能将您或其他人推向正确的方向!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM