Solved

A count row like a autoincrement column

Posted on 2006-11-27
7
294 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
Industry Leaders: 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 500 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
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…

762 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