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 8772233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:59:19+00:00 2026-06-13T17:59:19+00:00

I’ve got a very complex query that joins eight (8) different tables. For the

  • 0

I’ve got a very complex query that joins eight (8) different tables.

For the sake of this question, though, I’ll simplify it with this type of structure:

create table table1(PacketID int, RequestID int, EmpID int, PartNo varchar(20))

insert into table1 values
 (1 , 1, 132, 'abc123'),
 (1 , 2, 132, 'abc456'),
 (1 , 3, 132, 'def123'),
 (1 , 4, 132, 'def456'),
 (2 , 5, 228, 'xyz123'),
 (3 , 6, 239, 'xyz321'),
 (3 , 7, 239, 'aaa000')

This type of table creates the following output:

|_P_|_R_|_Emp_|_PartNo_|
|_1_|_1_|_132_|_abc123_|
|_1_|_2_|_132_|_abc465_|
|_1_|_3_|_132_|_def123_|
|_1_|_4_|_132_|_def456_|
|_2_|_5_|_228_|_xyz123_|
|_3_|_6_|_239_|_xyz321_|
|_3_|_7_|_239_|_aaa000_|

I put together a fiddle of it here: http://www.sqlfiddle.com/#!3/a3ce4/1

I was told they don’t really need this PacketID or RequestID, but what they do need is something that shows how many requests are in a packet and what request number of that packet needs attention.

I will still need the PacketID and RequestID values in order to make changes to the data.

So, I want to add a column to the table above to be something like this:

|_P_|_R_|_ReadAs_|_Emp_|_PartNo_|
|_1_|_1_|_1 of 4_|_132_|_abc123_|
|_1_|_2_|_2 of 4_|_132_|_abc465_|
|_1_|_3_|_3 of 4_|_132_|_def123_|
|_1_|_4_|_4 of 4_|_132_|_def456_|
|_2_|_5_|_1 of 1_|_228_|_xyz123_|
|_3_|_6_|_1 of 2_|_239_|_xyz321_|
|_3_|_7_|_2 of 2_|_239_|_aaa000_|

How exactly would I go about doing that?

If you just want to feel my pain and see the complete view defined on my SQL Server, here it is:

SELECT P.ID AS PacketID, R.ID AS RequestID, A.ID AS ActionID,
  EI.FIRSTNAME + ' ' + EI.LASTNAME AS Employee, P.DateStamp, 
  RQ.Description AS RequestType, L.Description AS Line, R.PartNo,
  R.Workorder, R.Qty, RZ.Description AS ReasonType, R.MTF,
  A.StatusID, S.Description AS Status, A.EmpID AS Stator, 
  A.DateStamp AS Stated
FROM dbo.Packet AS P
  LEFT OUTER JOIN dbo.Request AS R ON R.PacketID = P.ID
  INNER JOIN dbo.Action AS A ON R.ID = A.RequestID
  LEFT OUTER JOIN dbo.Action AS A2 ON A.RequestID = A2.RequestID
         AND (A.DateStamp < A2.DateStamp OR
         A.DateStamp = A2.DateStamp AND A.RequestID < A2.RequestID)
  INNER JOIN CPAPP.AIO_Test_Results.dbo.EmployeeInfo AS EI ON A.EmpID = EI.COUNT
  INNER JOIN dbo.RequestType AS RQ ON R.RequestTypeID = RQ.ID
  INNER JOIN dbo.Line AS L ON R.LineID = L.ID
  INNER JOIN dbo.ReasonType AS RZ ON R.ReasonTypeID = RZ.ID
  INNER JOIN dbo.Status AS S ON A.StatusID = S.ID
WHERE (A2.RequestID IS NULL)

screenshot

  • 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-13T17:59:20+00:00Added an answer on June 13, 2026 at 5:59 pm

    Rather than just giving you the entire answer, I’ll try to explain how to create the answer yourself.

    To get the consecutive sequence numbers have a look at ROW_NUMBER. You can provide a partition by (PacketID) and an order by (RequestId). This gets you the first number you need.

    To get the counts, if you were using SQL Server 2012 you could use COUNT with a partition. On older versions of SQL Server you will have to use a JOIN and GROUP BY or a subselect.

    This query demonstrates the principles using the subselect approach:

    SELECT
        PacketID,
        RequestID,
        EmpID,
        PartNo,
        ROW_NUMBER() OVER (PARTITION BY PacketID ORDER BY RequestID),
        (SELECT COUNT(*) FROM table1 AS T2 WHERE T1.PacketId = T2.PacketId)
    FROM table1 AS T1
    

    http://www.sqlfiddle.com/#!3/a3ce4/8

    To get the exact format you requested you just need to convert the integers to strings and concatenate them. I’ll leave this for you to do, but here’s a few hints.

    • CAST and CONVERT
    • + (String Concatenation)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
this is what i have right now Drawing an RSS feed into the php,

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.