TylerNG
TylerNG

Reputation: 941

Assign value in SQL

I would like to assign a numerical period to my date rows but not sure how to do it using SQL:

ID Date
111 1/1/17
111 1/2/17
111 1/3/17
112 1/2/17
112 1/3/17
113 1/2/17
113 1/3/17
113 1/4/17

My output would be:

ID Date Period
111 1/1/17 1
111 1/2/17 2
111 1/3/17 3
112 1/2/17 1
112 1/3/17 2
113 1/2/17 1
113 1/3/17 2
113 1/4/17 3

Thank you!

Upvotes: 1

Views: 50

Answers (4)

Gustav
Gustav

Reputation: 55831

You can use my row counter function (below) if you create a custom unique key from ID and Date:

SELECT RowCounter(Format([ID], "0000") & Format([Date], "yyyymmdd"),False,CStr([ID])) AS Period, *
FROM tblSomeTable
WHERE (RowCounter(Format([ID], "0000") & Format([Date], "yyyymmdd"),False) <> RowCounter("",True));

The function:

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

Upvotes: 0

Kevin
Kevin

Reputation: 80

Not sure if this is what you are after?

SELECT ID, Date, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) AS Period
FROM TablePeriod

Upvotes: 0

Glendon Klassen
Glendon Klassen

Reputation: 11

From your example, I take it you're assigning a period code based on ID.

If so, something like

SELECT ID, Date, rank() OVER (PARTITION BY ID ORDER BY Date) AS Period FROM yourDB

should do the trick.

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269863

MS Access doesn't give you many options for this. One is a correlated subquery:

select t.id, t.date,
       (select count(*)
        from t as t2
        where t2.id = t.id and t2.date <= t.date
       ) as period
from t;

Upvotes: 2

Related Questions