0
我有一張寫有交易數據的表。我需要在加入其他表格時查詢上述優惠,但是d.originalorderid
不是唯一條目,我有幾個重複條目。我想:在一個特定列中摺疊具有相同值的行
- 對於每一個獨特的
d.originalorderid
選擇一行 - 該行應該是最近的(最大ID)
我怎麼會去嗎?這是我現在的查詢。
SELECT d.id,
d.date,
d.ip, d.panmask,
d.merchantorderid,
d.amount,
d.cardholder,
d.bankhumanname,
d.cardtypeid,
d.bankcountrycode,
d.usercountrycode,
mc.paymentkey as merchantname,
dt.status,
d.merchantcontract,
dt.tag,
d.originalorderid,
ds.refnumber,
ds.dealauthcode,
mc.processingid,
pc.Name as processing,
d.customparams
FROM Deal as d
LEFT JOIN MerchantContract as mc ON mc.Id = d.MerchantContract
LEFT JOIN DealTrace as dt ON d.Id = dt.DealId
AND dt.id = (SELECT MAX(id)
FROM DealTrace WITH (nolock)
WHERE DealId = d.id)
LEFT JOIN DealSummary ds ON d.Id = ds.DealId
AND ds.id = (SELECT MAX(id)
FROM DealSummary WITH (nolock)
WHERE DealId = d.id)
LEFT JOIN Processing pc on mc.ProcessingId = pc.id
WHERE (d.MerchantContract IN ('12'))
ORDER BY ID desc OFFSET 0 ROWS FETCH NEXT 1000 ROWS ONLY
你會得到SQL Server和MySQL之間的不同答案。 – shawnt00
通用的方式是在交易之後立即拋出另一個連接:內部連接(通過originalorderid從交易組中選擇originalorderid,max(id)作爲maxorderid)md.originalorderid = d.originalorderid和md.maxid = d .id' – shawnt00
你的問題很混亂。你只提到一個表'交易',但在你的查詢中你使用了很多表。 – jonju