Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8302461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:17:50+00:00 2026-06-08T17:17:50+00:00

I’d like to create a SQL Server select statement where the cross referenced data

  • 0

I’d like to create a SQL Server select statement where the cross referenced data are in one column from different rows. I’d like to use stuff (if possible) and no ;with command.

Contract table

ID  Subject
1   a
2   b
3   c

Company table

ID  Name
1   a_ltd
2   b_ltd
3   c_ltd
4   d_ltd
5   e_ltd

ContractContractorCrossRef table (junction table)

ID  ContractID_ CompanyID_
1   1               1
2   1               2
3   2               3
4   3               1
5   3               4
6   3               5

I’d like this result:

Contract.ID company.Name
1           a_ltd, b_ltd
2           c_ltd
3           a_ltd, d_ltd, e_ltd

Populate data:

create table #Contract (ID INT, Subject VARCHAR(1))
INSERT  #Contract
SELECT  1, 'a' UNION ALL
SELECT  2, 'b' UNION ALL
SELECT  3, 'c'

create table #Company (ID INT, Name VARCHAR(5))
INSERT  #Company
SELECT  1, 'a_ltd' UNION ALL
SELECT  2, 'b_ltd' UNION ALL
SELECT  3, 'c_ltd' UNION ALL
SELECT  4, 'd_ltd' UNION ALL
SELECT  5, 'e_ltd'

create table #ccRef (ID INT, ContractID_ INT, CompanyID_ INT)
INSERT  #ccRef
SELECT  1, 1, 1 UNION ALL
SELECT  2, 1, 2 UNION ALL
SELECT  3, 2, 3 UNION ALL
SELECT  4, 3, 1 UNION ALL
SELECT  5, 3, 4 UNION ALL
SELECT  6, 3, 5 

Select:

select #Contract.ID, #Company.Name from #Contract
inner join #ccRef on #Contract.ID = #ccRef.ContractID_ inner join #Company
on #ccRef.CompanyID_ = #Company.ID

Select result: (not wanted)

ID  Name
1   a_ltd
1   b_ltd
2   c_ltd
3   a_ltd
3   d_ltd
3   e_ltd

Requested result:

Contract.ID company.Name
1           a_ltd, b_ltd
2           c_ltd
3           a_ltd, d_ltd, e_ltd
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T17:17:51+00:00Added an answer on June 8, 2026 at 5:17 pm

    Try this. I have not tested the code as i do not have ddl for these tables.But it should work..

           SELECT ct.id ,
     left(companyname.value('.','varchar(max)'), len(companyname.value('.','varchar(max)'))-1) as companyname 
    from 
        contract ct 
        cross apply 
        ( 
        select cm.name + ',' [text()]
        from 
        contractcompanycrossref cc 
    
        inner join company cm
        on cc.companyid = cm.ID
          where ct.id = cc.contractid 
        for xml path (''),type
        ) cmnames(companyname)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have some data like this: 1 2 3 4 5 9 2 6
I would like to count the length of a string with PHP. The string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.