- Where Developers Learn, Share, & Build Careers


I have found a very complex query that connects eight (8) different tables.

For this question, however, I simplify it with this type of structure:

  table table 1 (packet id int, activity int, mpid int , Partno Worker (20)) Insert values ​​in Table 1 (1, 1, 132, 'ABC 123'), (1, 2, 132, 'ABCSERA'), (1, 3, 132, 'def 123'), ( 1, 4, 132, 'Diff 94'), (2, 5, 228, 'xyz123'), (3, 6, 239, 'xyz321'), (3, 7, 239, 'AAAA 000')   

This type of table creates the following output:

  | _P_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 a bela here:

I was told that they do not really need PacketID or RequestID < / Code>, but whatever they need is something that shows how many requests are in the packet and which number of that packet needs attention.

I still need PacketID and RequestID value to make changes in data.

Therefore, I want to add a column to the table above:

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

If you just want to feel my pain and want to see the full view set on your SQL Server here:

  SELECT P.ID AS PacketID, RIDID AS RequestID, AID AS Action AID, EI.Firsten + '' + EI. As a LASTM employee, p. Data stamp, rq. RequestType, L. Line, R. as a description. Run, r Worker, R. Qt, RZ Esse Region Type, R.M.T.F., A StatsID, S. DSKN AS Status, AAMID AS Stator, A Data Stamp AS DBAPA.T. P.A. as stated in Join the left OUTER Deborah. R.PP.Kitat on request. • PIID Inner Join Deobo.AirID = A as an Action. Attach the requested left oter DBO. = A2.RequestID and (A.DateStamp & lt; A2.DateStamp OR A.DateStamp = A2.DateStamp and A.RequestID & lt; A2.RequestID) INNER Join CPAPP A.EmpID = EI COUNT INNER DEBO on AIO_Test_Results.dbo.EmployeeInfo AI. Type Request Request on R. RuiTtypeid = RQID Inner Join Debo. Line ASL on RILIDID = LIID INER JINO DEBO Region Type AS RZ O.N.R. Reason TypeID = RZ INNIR Joint Debo Status ASSS ATATSID = SID where (A 2. RichestID is IS) Alt = "screenshot">   

Instead of just giving you the full answer, I will try to explain How to prepare answers

Look for continuous sequence numbers. You can provide an order by (PacketID) a partition and (RequestId). This requires you the first number you need.

If you were using SQL Server 2012, you can use COUNT with a code. On older versions of SQL Server, you must use a join and group bye or a substrate.

This query shows the principles that use the subtotal approach:

  SELECT PacketID, RequestID, EmpID, PartNo, ROW_NUMBER () Over (PacketID Order of Order ID) ), Table 1 AS T2 Ware T1. (Packetid = T 2. Packet ID) from Table 1 AS T1   

(Select COUNT from *) To get the exact format you requested, you need to convert the integer to the string and add it. I'll leave it for you, but here are some signs.



Comments

Popular posts from this blog

Python SQLAlchemy:AttributeError: Neither 'Column' object nor 'Comparator' object has an attribute 'schema' -

java - How not to audit a join table and related entities using Hibernate Envers? -

mongodb - CakePHP paginator ignoring order, but only for certain values -