0

Table Fruits

FName
-----
orange
apple
apple
3mango
orange
orange
apple
mango

i want a query that result with counting.

orange-1
apple-1
apple-2
mango-1
orange-2
orange-3
apple-3
mango-2
Andre
  • 26,751
  • 7
  • 36
  • 80
  • 2
    How is the order defined? Is there an ID column? – JohnHC Oct 24 '17 at 10:37
  • Table Fruits FName ----- orange apple apple mango orange orange apple mango ----- i want a query that result with couting. orange-1 apple-1 apple-2 mango-1 orange-2 orange-3 apple-3 mango-2 – gurchani Oct 24 '17 at 10:41
  • Hi, please mark "is answer" if your question is answered for you. – Stefan Oct 30 '17 at 08:21

2 Answers2

3

In MS SQL Server would be a simple ROW_NUMBER() window function, so you just need the equivalent in MS Access. But for that you need an id column in your table, because you need to do a self-join.

Solution also here: Achieving ROW_NUMBER / PARTITION BY in MS Access

declare @tbl as table (
    id int
    ,fname varchar(15)
)

insert into @tbl values (1,'orange')
insert into @tbl values (2,'apple')
insert into @tbl values (3,'apple')
insert into @tbl values (4,'mango')
insert into @tbl values (5,'orange')
insert into @tbl values (6,'orange')
insert into @tbl values (7,'apple')
insert into @tbl values (8,'mango')

SELECT 
    t1.id
    ,t1.fname
    ,COUNT(*) AS [Ino Seq]
FROM 
    @tbl AS t1
    INNER JOIN
    @tbl AS t2
        ON t2.fname = t1.fname
        and t1.id >= t2.id
GROUP BY t1.id, t1.fname
Valerica
  • 1,618
  • 1
  • 13
  • 20
1

If you add an ID (Autonumber), you can use this function with the fruit name as the group key:

Public Function RowCounter( _
  ByVal strKey As String, _
  ByVal booReset As Boolean, _
  Optional ByVal strGroupKey As String) _
  As Long

' Builds consecutive RowIDs in select, append or create query
' with the possibility of automatic reset.
' Optionally a grouping key can be passed to reset the row count
' for every group key.
'
' Usage (typical select query):
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' Usage (with group key):
'   SELECT RowCounter(CStr([ID]),False,CStr[GroupID])) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter(CStr([ID]),False) <> RowCounter("",True));
'
' The Where statement resets the counter when the query is run
' and is needed for browsing a select query.
'
' Usage (typical append query, manual reset):
' 1. Reset counter manually:
'   Call RowCounter(vbNullString, False)
' 2. Run query:
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable;
'
' Usage (typical append query, automatic reset):
'   INSERT INTO tblTemp ( RowID )
'   SELECT RowCounter(CStr([ID]),False) AS RowID, *
'   FROM tblSomeTable
'   WHERE (RowCounter("",True)=0);
'
' 2002-04-13. Cactus Data ApS. CPH
' 2002-09-09. Str() sometimes fails. Replaced with CStr().
' 2005-10-21. Str(col.Count + 1) reduced to col.Count + 1.
' 2008-02-27. Optional group parameter added.
' 2010-08-04. Corrected that group key missed first row in group.

  Static col      As New Collection
  Static strGroup As String

  On Error GoTo Err_RowCounter

  If booReset = True Then
    Set col = Nothing
  ElseIf strGroup <> strGroupKey Then
    Set col = Nothing
    strGroup = strGroupKey
    col.Add 1, strKey
  Else
    col.Add col.Count + 1, strKey
  End If

  RowCounter = col(strKey)

Exit_RowCounter:
  Exit Function

Err_RowCounter:
  Select Case Err
    Case 457
      ' Key is present.
      Resume Next
    Case Else
      ' Some other error.
      Resume Exit_RowCounter
  End Select

End Function

See the in-line comments for typical usage(s).

Gustav
  • 53,498
  • 7
  • 29
  • 55