How to show all names for identical contract in T-SQL

霸气de小男生 提交于 2019-12-25 00:51:04

问题


I have query like this, and i shows me sort of good results

select 
    vup.UgovorId, 
    count(P.Naziv) as BROJNAZIVA 
from 
    [TEST_Ugovori_Prod].dbo.VezaUgovorPartner as vup 
inner join 
    [TEST_MaticniPodaci2].[dbo].[Partner] as p on vup.PartnerId = p.PartnerID 
group by 
    vup.UgovorId

Results are like this (first row is vup.UgovorId, second is p.Naziv):

1264 1
1265 3

But I want to show all p.Naziv when that row has more then one for that vup.UgovorId like string so I would be like this:

1264 "Mark"
1265 "Jerry, Philip, Tom"

回答1:


I think this is the issue you are trying to solve?

I am not sure what technology you are using, but this may at least provide you some guidance.

So your code would look something like this:

select 
    vup.UgovorId, 
    substring(
    (
        Select ','+ P.Naziv AS [text()]
        From [TEST_MaticniPodaci2].[dbo].[Partner] as p
        Where vup.PartnerId = p.PartnerID
        ORDER BY P.Naziv
        For XML PATH ('')
    ), 2, 1000) [Names]
from 
    [TEST_Ugovori_Prod].dbo.VezaUgovorPartner as vup
group by vup.UgovorId


来源:https://stackoverflow.com/questions/48965582/how-to-show-all-names-for-identical-contract-in-t-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!