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

  • SEARCH
  • Home
  • 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 8220331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:29:13+00:00 2026-06-07T13:29:13+00:00

I am trying to combine rows from one table with my query in MS

  • 0

I am trying to combine rows from one table with my query in MS SQL on server 2008. I pulled this code off here I believe. This almost fits my needs but it is grouping the serial numbers off of SOPartsUsed but I need it to group based off tblServiceOrders.ProjectKeyID. Any help would be greatly appreciated I don’t know much about SQL. I will leave a more detailed explanation of what I am trying to accomplish below.

SELECT        
p1.ItemID, SerialNumbers AS SerialNumber 
FROM            
tblSOPartsUsed p1 INNER JOIN
tblServiceOrders p2 ON p1.SONumber = p2.SONumber
    CROSS APPLY
       (SELECT        
        stuff
           ((SELECT ',' + p3.SerialNumber
             FROM tblSerialNumbers p3
             WHERE p3.FKSOPartsUsed = p1.SOPartsUsedKeyID
             ORDER BY SerialNumber FOR XML PATH(''), TYPE ).value('.', 'varchar(max)'), 1, 1, '')
    ) D (SerialNumbers)
 WHERE        (p1.QuantityFilled > 0) AND (p2.ProjectKeyID = 385)
 GROUP BY p1.ItemID, SerialNumbers, p2.ProjectKeyID
 ORDER BY p1.ItemID

I have a table with serial numbers a table with parts used on a service order and a service order table.

tblSerialNumbers -> tblSOPartsUsed -> tblServiceOrders

tblSerialNumbers
itemID  SerialNumber  FKSOPartsUsed
1          1444                233
1          1555                234
1          1666                236

1          1999                237
1          1888                238

1          2222                239
1          2121                240

tblSOPartsUsed
itemID  SOPartsUsed  SONumber  QuantityFilled
1          233                  SO544         5
1          234                  SO544         7
1          236                  SO544         7
1          237                  SO577         7
1          238                  SO577         7

1          239                  SO581         7
1          240                  SO580         7

tblServiceOrders
SOnumber  ProjectKeyID
SO544        PJ366
SO577        PJ366
SO580        PJ111
SO581        PJ111

What I would like
itemID   ProjectKeyID   SerialNumber
1           PJ366             1444,1555,1666,1999,1888

What I get
itemID   ProjectKeyID   SerialNumber
1           PJ366             1444,1555,1666
1           PJ366             1999,1888

I am trying to group serial numbers and item id’s by ProjectKeyID found in tblServiceOrders. Right now the query above works but it is grouping ItemID’s on the tblSOPartsUsed and want to group on ProjectKeyID.

Thanks for any help.

  • 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-07T13:29:14+00:00Added an answer on June 7, 2026 at 1:29 pm
    SELECT  itemID ,
        ProjectKeyID ,
        ( SELECT    SerialNumber + ', ' AS 'data()'
          FROM      dbo.tblSOPartsUsed u
                    JOIN tblSerialNumbers nbr ON u.SOPartsUsed = nbr.FKSOPartsUsed
          WHERE     SONumber IN ( SELECT    SOnumber
                                  FROM      dbo.tblServiceOrders
                                  WHERE     ProjectKeyID = tso.ProjectKeyId )
                    AND itemID = tso.itemID
        FOR
          XML PATH('')
        )
    FROM    ( SELECT DISTINCT
                    c.itemID ,
                    ProjectKeyId
          FROM      dbo.tblServiceOrders a
                    JOIN dbo.tblSOPartsUsed b ON a.SOnumber = b.SONumber
                    JOIN dbo.tblSerialNumbers c ON b.SOPartsUsed = c.FKSOPartsUsed
        ) tso
    

    I don’t think I totally understand what you’re trying to accomplish with itemID – having only one distinct value in the sample makes it hard to verify results – but this might get you closer

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am basically trying to combine code from two examples given in the DataTables
I'm trying to select rows one by one in a while loop in SQL
I'm trying to write an SQL query that will gather certain data in one
im trying to combine two images into a single image. unfortunately this has to
I have a table of login events coming from Active Directory. One type of
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
While implementing a tree structure over a SQL 2005 server database, the query response
I'm trying to build a safe user authentication system. The code is from http://net.tutsplus.com/tutorials/php/simple-techniques-to-lock-down-your-website/
I have a table with itemid|fieldid|value and i'm trying to setup a query that
I'm trying to combine the codes from these two questions: using jQuery to slideToggle

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.