[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

A count row like a autoincrement column

Posted on 2006-11-27
7
Medium Priority
?
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 18018226
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
ID: 18018261
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
ID: 18018299
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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


0
 
LVL 77

Expert Comment

by:peter57r
ID: 18018350
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
ID: 18018404
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 2000 total points
ID: 18018495
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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