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)

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
COUNTwith 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:
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.