Chris
Chris

Reputation: 1

Access Query with complex sorting requirements

I have to generate Q-Q plots from a continually expanding dataset. In order to do this I have to have two columns sorted independently of each other.

Like this:

Main table

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

The final table should sort like this:

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

I attempted to do this with two joined subqueries and a VBA function that added a row number to each subquery. It did not return any values, although each subquery works on its own.

This is the SQL query that I attempted to use:

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;

Upvotes: 0

Views: 181

Answers (1)

June7
June7

Reputation: 21370

Assuming both fields have data in every record.

If both fields have unique number values, consider:

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;

Another query approach requires two query objects:

Query1

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

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

Both solutions might perform slowly with large dataset.

Here is VBA approach. Build a table called MainSorted with 3 fields:

Seq - autonumber
F1 - number
F2 - number

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

Or this version:

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

Considering @CPerkins comments:

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

Upvotes: 2

Related Questions