[英]How do I write my SQL Code in MS Access SQL?
表名:記錄
ID | 日期 | 時間 | 地位 | 箱型 | 材料 | 架子 | 員工編號 | 交易 |
---|---|---|---|---|---|---|---|---|
1 | 25-05-2020 | 13:11:12 | 70 | 36757 | 4 | 4 | 188 | 2 |
2 | 25-05-2020 | 13:12:40 | 71 | 31178 | 2 | 5 | 188 | 2 |
3 | 25-05-2020 | 13:13:31 | 71 | 31178 | 2 | 5 | 188 | 102 |
4 | 27-05-2020 | 13:14:14 | 71 | 38444 | 3 | 2 | 181 | 3 |
5 | 27-05-2020 | 13:14:15 | 71 | 38444 | 3 | 2 | 181 | 3 |
6 | 27-05-2020 | 13:14:41 | 71 | 38444 | 3 | 2 | 181 | 3 |
7 | 27-05-2020 | 13:15:10 | 71 | 39123 | 6 | 5 | 188 | 3 |
8 | 27-05-2020 | 13:15:51 | 71 | 38444 | 3 | 2 | 181 | 103 |
9 | 27-05-2020 | 13:16:51 | 71 | 38444 | 3 | 2 | 181 | 103 |
我想要的所需 OUTPUT 是第 1、6 和 7 行
ID | 日期 | 時間 | 地位 | 箱型 | 材料 | 架子 | 員工編號 | 交易 |
---|---|---|---|---|---|---|---|---|
1 | 25-05-2020 | 13:11:12 | 70 | 36757 | 4 | 4 | 188 | 2 |
6 | 27-05-2020 | 13:14:41 | 71 | 38444 | 3 | 2 | 181 | 3 |
7 | 27-05-2020 | 13:15:10 | 71 | 39123 | 6 | 5 | 188 | 3 |
您好親愛的開發者朋友們,
我有這張表“記錄”。 有兩種類型的交易,即。 “正向”和“反向”。 正向交易由一位到兩位數字表示(即從 1 到 30 ),反向交易由三位數字表示(即從 100 到 130) 。 現在,每一個獨特的正向交易都有一個獨特的反向交易。 (例如,事務 1 將具有 101,事務 2 將具有 102,事務 3 將具有 103,依此類推)。
我不想在我的報告中看到反向交易以及它們的遠期交易。 (例如 102 和 2、103 和 3 )但是,我必須找到匹配的正向交易,其其他列數據(日期、狀態、框類型、材料、機架、員工編號)與反向交易相同。 (如圖所示,記錄號 2 和 3 )此外,反向交易可以有多個正向交易匹配,我只想跳過與反向交易數量相等的那些正向交易。 (*如表所示,記錄號4,5和6匹配記錄號8和9,所以我想用8和9隱藏兩個記錄號4和5)
所以,我嘗試編寫一個 SQL 查詢,但它在 MS 訪問中不起作用。 感謝您的大力幫助。 謝謝! 此致。
我的代碼
with table_a as (
select a.*, row_number() over (partitition by Status, BoxType, Material, Rack, EmployeeNr , Transaction) as dup_n --all columns except ID and time.
---for multiple entries of same type
from Records a
where Transaction<100
)
,table_b as (
select b.*, (Transaction-100) as type_new,
row_number() over (partitition by Status, BoxType, Material, Rack, EmployeeNr , Transaction) as dup_n --all columns except time and ID
---for multiple entries of same type
from Records b
where Transaction>99
)
select*from(
select a.* from table_a
left join table_b
on <Status, BoxType, Material, Rack, EmployeeNr , Transaction> --all columns except time and ID
and a.type = b.type_new
and a.dup_n = b.dup_n
)
where a.Transaction is null
Stackoverflow 並非旨在成為一種代碼編寫服務,但由於您確實嘗試使用 SQL 而我是提出 VBA 的人,我對挑戰很感興趣,這里有一些東西可以讓您開始。 首先,構建一個“臨時”表 - 表是永久的,但記錄是臨時的 - 與 Records 表具有相同的字段,除了 ID 字段不是自動編號,只是數字類型。 將其稱為 RecordsSelected 並確保兩個表中的字段在設計中的順序相同。 然后,構建三個查詢對象。
轉速:
SELECT [Date], Status, BoxType, Material, Rack, EmployeeNr, Transaction,
Count(ID) AS CntRev, Val(Mid([Transaction],2)) AS Forward
FROM Records
WHERE (((Records.Transaction)>100))
GROUP BY [Date], Status, BoxType, Material, Rack, EmployeeNr, Transaction, Val(Mid([Transaction],2));
查詢1:
SELECT Records.*
FROM Records
LEFT JOIN (SELECT Records.* FROM Records
WHERE (((Records.Transaction)>100))) AS Reverses
ON (Records.Date = Reverses.Date)
AND (Records.Status = Reverses.Status)
AND (Records.BoxType = Reverses.BoxType)
AND (Records.Material = Reverses.Material)
AND (Records.Rack = Reverses.Rack)
AND (Records.EmployeeNr = Reverses.EmployeeNr)
WHERE (((Reverses.Transaction) Is Null));
查詢2:
SELECT Records.*, Forward
FROM CntRevs
INNER JOIN Records ON (CntRevs.EmployeeNr = Records.EmployeeNr)
AND (CntRevs.Rack = Records.Rack)
AND (CntRevs.Material = Records.Material)
AND (CntRevs.BoxType = Records.BoxType)
AND (CntRevs.Status = Records.Status)
AND (CntRevs.Date = Records.Date)
AND (CntRevs.Forward = Records.Transaction);
通用模塊中的代碼。 結果是將您所需的 output 中標識的 3 條記錄寫入臨時表。
Sub Test()
Dim rs1 As DAO.Recordset, rs2 As DAO.Recordset, rs3 As DAO.Recordset, db As DAO.Database
Dim x As Integer
Set db = CurrentDb
db.Execute "DELETE FROM RecordsSelected"
db.Execute "INSERT INTO RecordsSelected SELECT * FROM Query1"
Set rs1 = db.OpenRecordset("SELECT * FROM CntRevs")
Set rs3 = db.OpenRecordset("SELECT * FROM RecordsSelected")
Do While Not rs1.EOF
Set rs2 = db.OpenRecordset("SELECT * FROM Query2 WHERE Transaction = " & rs1!Forward & " ORDER BY ID DESC")
If Not rs2.EOF Then
rs2.MoveLast
rs2.MoveFirst
If rs2.recordCount > rs1!CntRev Then
For x = 1 To rs2.recordCount - rs1!CntRev
With rs3
.AddNew
!ID = rs2!ID
!Date = rs2!Date
!Time = rs2!Time
!Status = rs2!Status
!BoxType = rs2!BoxType
!Material = rs2!Material
!Rack = rs2!Rack
!EmployeeNr = rs2!EmployeeNr
!Transaction = rs2!Transaction
.Update
End With
rs2.MoveNext
Next
End If
End If
rs1.MoveNext
Loop
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.