在 SQL 中赋值

Assign value in SQL

我想为我的日期行分配一个数字句点,但不确定如何使用 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

我的输出是:

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

谢谢!

MS Access 没有为此提供很多选项。一种是关联子查询:

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;

根据您的示例,我认为您是根据 ID 分配句点代码。

如果是这样,类似于

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

应该可以解决问题。

不确定这是否是您想要的?

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

如果您根据 ID 和日期创建自定义唯一键,则可以使用我的行计数器功能(如下):

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));

函数:

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