Solved

A count row like a autoincrement column

Posted on 2006-11-27
7
256 Views
Last Modified: 2010-05-18
Hello guys,

I need to create in my query a column a sequence, as I have 7000 rows and I filter by inicial letter, like
a , so I want all register that starts with A, I need to create a sequence column that starts from 1.

like this:

For A

1
2
3
4

For B
1
2
3
4
5


How?
0
Comment
Question by:hidrau
  • 4
  • 3
7 Comments
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
Hi hidrau,

You can use the function below to add a sequence number for each group.
You need to pass the Initial letter as the argument.

Myseq:GetNextrNo(left(myfield),1)

You must ensure that your data is correctly sorted in your query.

Pete

Function getnextno(groupval)

Static mygroup
Static LastNo
If Nz(mygroup, "") <> groupval Then
LastNo = 0
mygroup = groupval
End If
LastNo = LastNo + 1
getnextno = LastNo

End Function
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
Ins't there any way to do this directly in SQL, because I will assemble this query in Delphi, and custom-made function doesn't word out of access :((

I don't know but only access functions works, when I tried to create some and called it from my delphi application, my application didn't work.

You see!

0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
To do it in sql, you must have a field which has a  unique value within each group (letter) and the data must be sorted on this field within the group.
Do you have such a field?

Pete
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Author Comment

by:hidrau
Comment Utility
ok, I can create a field with value 0, it is possible so, to create the sequence?


0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
I think we have a language pronblem.
I'll try to re-do my response.

In your table, do you have a field which  contains a unique value for each record? (a Primary key, for example).
If so, you need to sort your query on these fields:
1.Initial Letter
2.Unique Key

Are you able to do this?

Pete
0
 
LVL 1

Author Comment

by:hidrau
Comment Utility
Ok, Peter,

I have a primary key, but let me explain better.

When I filter my query this way:

SELECT * FROM TabWord
WHERE LEFT(WordField,1)='b'
ORDER BY WordField

all the rows that start with 'B' letter goes from 3950 until 8670

when I tell you this number, it is because it is a primary key.

I need to create this query order by WordField, but I need to create a sequence that
goes from 1 to ... the end of my rows filtered.



0
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
Comment Utility
You must use your own primary key field name in the sql below:

SELECT Left([wordfield],1) AS Init, C1*,  (select count(*) from Tabword where left(wordfield,1) = left(c1.wordfield,1) and primarykeyfield<=c1.primarykeyfield) AS Rank FROM Tabword AS C1
ORDER BY Left([companyname],1), C1.primarykeyfield;


Pete
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now