具有复杂排序要求的访问查询

时间:2019-01-19 00:33:44

标签: sql sorting ms-access subquery

我必须从不断扩展的数据集中生成Q-Q图。为此,我必须将两列彼此独立地排序。

赞:

主表

F1 | F2
---+----
6  | 10
8  |  7
2  |  5

决赛桌应该像这样:

F1 | F2
---+----
2  |  5
6  |  7
8  | 10

我试图通过两个联接的子查询和一个VBA函数来做到这一点,该函数向每个子查询添加了行号。尽管每个子查询都可以单独工作,但它没有返回任何值。

这是我尝试使用的SQL查询:

SELECT 
    wautonumber([Au_org_gpt]) AS index_no, 
    ORG.Project, ORG.Au_org_gpt AS Au_org_gpt_QQ, 
    MTS.Au_mts_gpt AS Au_mts_gpt_QQ 
FROM
    (SELECT 
         wautonumber([Au_org_gpt]) AS index_no, 
         MCIPLD_geochem_mts.Project, MCIPLD_geochem_mts.Au_org_gpt
     FROM
         MCIPLD_geochem_mts
     ORDER BY 
         MCIPLD_geochem_mts.Au_org_gpt) AS ORG 
INNER JOIN 
    (SELECT 
         wautonumber([Au_mts_gpt]) AS index_no, 
         MCIPLD_geochem_mts.Project, MCIPLD_geochem_mts.Au_mts_gpt
     FROM
         MCIPLD_geochem_mts
     ORDER BY 
         MCIPLD_geochem_mts.Au_mts_gpt)  AS MTS ON ORG.index_no = MTS.index_no;

1 个答案:

答案 0 :(得分:2)

假设两个字段在每个记录中都有数据。

如果两个字段都具有唯一的数字值,请考虑:

SELECT Query1.Seq, Query1.F1, Query2.F2
FROM
(SELECT DCount("*","Main","F2<=" & [F2]) AS Seq, Main.F2
    FROM Main ORDER BY Main.F2) AS Query2 
INNER JOIN
(SELECT DCount("*","Main","F1<=" & [F1]) AS Seq, Main.F1
    FROM Main ORDER BY Main.F1) AS Query1 
ON Query2.Seq = Query1.Seq
ORDER BY Query1.Seq;

另一种查询方法需要两个查询对象:

Query1

SELECT "F1" AS Src, F1 AS Data FROM Main
UNION SELECT "F2", F2 FROM Main;

查询2

TRANSFORM First(Query1.Data) AS FirstOfData
SELECT DCount("*","Query1","Src='" & [Src] & "' AND Data<=" & [Data]) AS Seq
FROM Query1
GROUP BY DCount("*","Query1","Src='" & [Src] & "' AND Data<=" & [Data])
PIVOT Query1.Src;

这两个解决方案对于大型数据集的执行速度可能都很慢。

这是VBA方法。用3个字段建立一个名为MainSorted的表:

序列-自动编号
F1-数字
F2-数字

Sub MainSort()
Dim rsSrc As Recordset, db As Database
Dim lngRow As Long
Set db = CurrentDb
db.Execute "DELETE FROM MainSorted"
db.Execute "INSERT INTO MainSorted(F1) SELECT F1 FROM Main ORDER BY F1"
Set rsSrc = db.OpenRecordset("SELECT F2 FROM Main ORDER BY F2")
lngRow = DMin("Seq", "MainSorted")
While Not rsSrc.EOF
    db.Execute "UPDATE MainSorted SET F2=" & rsSrc!F2 & " WHERE Seq = " & lngRow
    rsSrc.MoveNext
    lngRow = lngRow + 1
Wend
End Sub

或此版本:

Sub MainSort()
Dim rsSrc1 As Recordset, rsSrc2 As Recordset, db As Database
Set db = CurrentDb
db.Execute "DELETE FROM MainSorted"
Set rsSrc1 = db.OpenRecordset("SELECT F1 FROM Main ORDER BY F1")
Set rsSrc2 = db.OpenRecordset("SELECT F2 FROM Main ORDER BY F2")
While Not rsSrc1.EOF
    db.Execute "INSERT INTO MainSorted(F1,F2) VALUES(" & rsSrc1!F1 & "," & rsSrc2!F2 & ")"
    rsSrc1.MoveNext
    rsSrc2.MoveNext
Wend
End Sub

考虑@CPerkins的评论:

Sub MainSort()
Dim rsSrc1 As Recordset, rsSrc2 As Recordset, rsDest As Recordset, db As Database
Set db = CurrentDb
db.Execute "DELETE FROM MainSorted"
Set rsSrc1 = db.OpenRecordset("SELECT F1 FROM Main ORDER BY F1")
Set rsSrc2 = db.OpenRecordset("SELECT F2 FROM Main ORDER BY F2")
Set rsDest = db.OpenRecordset("SELECT * FROM MainSorted")
While Not rsSrc1.EOF
    rsDest.AddNew
    rsDest!F1 = rsSrc1!F1
    rsDest!F2 = rsSrc2!F2
    rsDest.Update
    rsSrc1.MoveNext
    rsSrc2.MoveNext
Wend
End Sub