【问题标题】:i want query for MS access but i have SQL query, can any one help me for convert SQl query to MS Access我想查询 MS Access 但我有 SQL 查询,谁能帮我将 SQl 查询转换为 MS Access
【发布时间】:2018-03-12 06:20:34
【问题描述】:

我想查询 MS Access 但我有 SQL 查询,谁能帮我将 SQL 查询转换为 MS Access。

数据:

StateCode   BuyerCode   Sales
GJ           B01        20
GJ           B02        5
GJ           B03        50
GJ           B02        45
GJ           B01        7
GJ           B11        20
GJ           B01        45
GJ           B01        12
GJ           B08        50
GJ           B01        8
GJ           B03        30
MP           B03        50
MP           B03        45
MP           B03        7
MP           B03        20
MP           B01        45
MP           B01        12
MP           B02        50
MP           B01        8
MP           B03        30

SQL 查询:

WITH TOPTEN AS (
    SELECT *, ROW_NUMBER() 
    over (
        PARTITION BY [statecode] 
        order by [TOTALsales] desc
    ) AS RowNo 
    FROM TBL_sales_testRushang
)
SELECT TOPTEN.statecode,TOPTEN.buyercode,TOPTEN.Totalsales FROM TOPTEN WHERE RowNo <= 3

【问题讨论】:

  • 这将很难在 Access 中摆动。但无论如何,请向我们展示您表格中的一些示例数据。也许有人可以给你一个解决方法。
  • 请删除上述评论并在您的问题中包含数据,格式为文本,每行缩进四个或更多空格。

标签: sql ms-access sql-server-2014 sql-server-2016 ms-access-2016


【解决方案1】:

你可以使用我的快速RowCounter功能:

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

然后是这样的查询:

SELECT 
    statecode, buyercode, Totalsales, RowNo
FROM
    (SELECT RowCounter([StateCode],False) AS RowNo, *
    FROM TBL_sales_testRushang
    WHERE (RowCounter([StateCode],False) <> RowCounter("",True))
    ORDER BY TotalSales Desc) AS TOPTEN
WHERE
    RowNo <= 3

【讨论】:

    【解决方案2】:
    select statecode, buyercode, totalsales
    from
    (
    SELECT 
           statecode, buyercode, totalsales,
           (select count(*)
            from tbl_sales_testRushang AS t1  
            where t1.statecode = t.statecode
            and   t1.totalsales >= t.totalsales 
            ) as rowno
    FROM tbl_sales_testRushang t ) tbl
    where rowno <= 3
    order by statecode, rowno  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      相关资源
      最近更新 更多