473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Add sequential numbers to unique values

If I have a table that contains 2 columns

ID Serial
1 1234ABCD
2 1234ABCD
3 56789AAA
4 56789AAA
5 56789AAA
6 1011BBBB
7 4321XXXX
8 4321XXXX
9 4321XXXX
10 4321XXXX

How would I make a query that sequentially counts the unique serial numbers? Like this.

ID Serial Sequence_No
1 1234ABCD 1
2 1234ABCD 2
3 56789AAA 1
4 56789AAA 2
5 56789AAA 3
6 1011BBBB 1
7 4321XXXX 1
8 4321XXXX 2
9 4321XXXX 3
10 4321XXXX 4

Thanks
Dec 9 '18 #1

✓ answered by twinnyfo

NeoPa,

Yes, although using my method does, in fact, work and produce the results desired, it is probably not the best way. Here is the corrected version:
Expand|Select|Wrap|Line Numbers
  1. SELECT ID, 
  2.        Serial 
  3.        (SELECT Count(*) 
  4.         FROM tblYourTable 
  5.         WHERE Serial = T2.Serial 
  6.         AND ID <= T2.ID) AS Sequence_No 
  7. FROM tbYourTable as T2;
I would critique your method in the following way: I can see that your method would work only in situations in which the Serial Numbers are all sequential IDs (granted, this is the dataset provided by the OP). If those Serial Numbers were randomly scattered throughout the table, your method would only find how many records separated each occurrence.

7 3010
twinnyfo
3,653 Expert Mod 2GB
I think it is as simple as:

Expand|Select|Wrap|Line Numbers
  1. DCount("ID", "tblYourTable", "Serial = '" & [Serial] & "' AND ID <= " & [ID]) AS Sequence_No
Hope this hepps!
Dec 9 '18 #2
NeoPa
32,556 Expert Mod 16PB
Unfortunately the question doesn't match the example so using DCount() would not work - which is another good reason for not using Domain Aggregate functions in a query ;-)

To serialise the records is another matter - and not too straightforward. That's because databases and tables are built on Set Theory and aren't simply Excel spreadsheets.

To achieve this you'd need a sub-query to group the items you intend to serialise and provide the minimum value of the [ID]. This you'd link to the main table in your query using an INNER JOIN. For each resultant record you'd use the [ID] - Min([ID]) from the matching grouped query.

If you want the serialisation to start at one instead of zero then add an extra one.
Dec 10 '18 #3
twinnyfo
3,653 Expert Mod 2GB
I do have a question regarding these differing solutions, because I have always had questions about the real difference betwixt using a DCount() Domain Aggregate function and using the Count() function from within SQL. What is the "real" difference, and why does it matter? And, the Domain Aggregate functions are decried as the bane of humanity. I just want to understand a bit better--as I am sure you can probably assist.

Granted, I can see that using a Domain Aggregate function with very large datasets would be slow--but I also see that using Count() with large datasets would create similar delays.
Dec 10 '18 #4
twinnyfo
3,653 Expert Mod 2GB
NeoPa,

Yes, although using my method does, in fact, work and produce the results desired, it is probably not the best way. Here is the corrected version:
Expand|Select|Wrap|Line Numbers
  1. SELECT ID, 
  2.        Serial 
  3.        (SELECT Count(*) 
  4.         FROM tblYourTable 
  5.         WHERE Serial = T2.Serial 
  6.         AND ID <= T2.ID) AS Sequence_No 
  7. FROM tbYourTable as T2;
I would critique your method in the following way: I can see that your method would work only in situations in which the Serial Numbers are all sequential IDs (granted, this is the dataset provided by the OP). If those Serial Numbers were randomly scattered throughout the table, your method would only find how many records separated each occurrence.
Dec 10 '18 #5
NeoPa
32,556 Expert Mod 16PB
Hi Twinny.

You're correct in your critique. I did assume that the example given was a more reliable indicator of the requirement than the question as worded. I felt that had already been indicated. As for what may be an extra requirement of handling randomly sequenced records, I felt allowing the OP to realise that for themselves and being encouraged actually to express it would help them to understand the complexities of the situation a little better. There's no guarantee they would either have perceived the issue or expressed it mind.

NB. Your solution seems the perfect answer. If it hadn't already been selected as Best Answer then I'd have done it myself. I've taken the liberty of splitting the post though, as the extra question, while perfectly fine in itself, isn't what someone is looking for when they search for this particular question.

The evils of Domain Aggregate functions.

Very good question. Bear in mind that each call is a separately set up processing of a dataset. It also has procedural overhead of course, but I expect that to be really minimal. What you should be noticing here is that it's separate from the original SQL of your query, as well as being each separate from the other. One of the great things that an RDBMS engine provides you with is the optimisation of handling data. Particularly with large amounts.

Even if the original SQL has no overlapping data itself (with the Domain Aggregate function data), it's often the case that multiple DA functions within some SQL, process either the same data or just data from the same source. This is more efficiently handled by a single pass through the source data rather than multiple passes through that same data. The cost of setting up a recordset is included in every DA function call and that cost is (relatively) considerable.

Often the cost of such a design is negligible. The understanding of why one would do it the proper way however, is priceless. Good habits mean you fall over fewer such issues in the first place.

I hope that helps.
Dec 10 '18 #6
twinnyfo
3,653 Expert Mod 2GB
NeoPa,

Yes, this was very heppful. I think, because of bad practices and inclinations of the past (as well as not fully understanding things) I naturally gravitate toward the Domain Aggregate functions. However, I have begun to incorporate subqueries in my projects more regularly, mostly because, thanks to this site, I’ve gained a better understanding of them, as well as an appreciation for them.

Thanks again for clarifying something for me that helps me understand my craft!
Dec 11 '18 #7
NeoPa
32,556 Expert Mod 16PB
Always a pleasure my friend.

Bear in mind you ask more advanced questions than most. This gives an opportunity to share some of the more advanced practices and understanding so that all have access to it. Win-win!
Dec 11 '18 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tony Williams | last post by:
I recently posted a message asking for help with sequential numbers. I want to create an autonnumber reference number that reverts back to 1 at the start of each year. GlenAppleton gave me some...
14
by: amywolfie | last post by:
Hi All: I know this is simple, but I just can't seem to get there: I need to sort a table by a text field (txtDescription), then assign sequential numbers to the field SEQUENCE in table. ...
8
by: nescio | last post by:
hello, i have an array and i don't know the content of it, but i want only unique values. in php there is a function to do this, but how must i do this in javascript? i have tried a lot and...
11
by: sqlservernewbie | last post by:
Hi Everyone, Here is a theoretical, and definition question for you. In databases, we have: Relation a table with columns and rows
6
by: shira | last post by:
Hi, Looking to see if someone might have an explanation for this behavior. Is it a bug? Corruption? I have been able to reproduce the problem with only 2 rows and 1 field. Here is the table:...
3
by: Finomosec | last post by:
Hi, i have a table of number-objects with beginning and endnr: 10-15 16-20 25-30 32-32 35-35 36-36 37-40
3
by: Excel 009 | last post by:
Hi, Is there a way to populate sequential numbers in a field using SQL or VBA? If yes, how? Assume the following is my existing table: Fruit ID Apply Banana
18
by: Joel Miller | last post by:
I found an article that was somewhat like what I was trying to do. The article was titled: SQL Query - Find block of sequential numbers Here is the article...
21
by: neelsfer | last post by:
Ok i am back with a question about recordsets. I can add a record to two different recordsets at the sametime.(RacetimingT and RaceEntry5) My problem: how do i add a sequential number to both...
12
by: Pierkes | last post by:
Hi, I have A table with 2 columns - ID_tr - type The type column has 4 items in it. - new - extension - DIP
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.