這是一個工作版本,適用於任何希望使用子查詢,where和group by來完成此操作的人。
該查詢查看用戶在1天的時間範圍內對項目成本所做的更改,其中包含「昨天」的所有結果。它列出了當天的所有變化一個asc和一個desc,並比較價格變化的方式。如果從最古老的變化到最新的變化都是一樣的話,那麼它是免除的。
SELECT
us.Name,
it.Name,
pal.CS_PA_Line_Supplier__c as supplier_name,
newest.NewValue as new_value,
oldest.OldValue as old_value,
((newest.NewValue - oldest.OldValue)/oldest.OldValue) * 100 as Percentage
FROM
(
SELECT Id, Name, KNDY4__Item__c, CS_PA_Line_Supplier__c
FROM KNDY4__Contract_Line__c
) pal
LEFT JOIN
(
SELECT *
FROM
(
SELECT
ParentId,
CreatedById,
CreatedDate,
Field,
NewValue
FROM KNDY4__Contract_Line__History
WHERE CreatedDate >= CURDATE() - INTERVAL 1 DAY
AND CreatedDate < CURDATE()
AND Field='KNDY4__Negotiated_Price__c'
ORDER BY CreatedDate DESC
) cd
GROUP BY ParentId
) newest
ON newest.ParentId=pal.Id
LEFT JOIN
(
SELECT * FROM (
SELECT
ParentId,
OldValue
FROM KNDY4__Contract_Line__History
WHERE CreatedDate >= CURDATE() - INTERVAL 1 DAY
AND CreatedDate < CURDATE()
AND Field='KNDY4__Negotiated_Price__c'
ORDER BY CreatedDate ASC
) cd
GROUP BY ParentId
) oldest
ON oldest.ParentId=pal.Id
LEFT JOIN
(
SELECT Id, Name
FROM User
) us
ON us.Id = newest.CreatedById
LEFT JOIN
(
SELECT Id,Name
FROM KNDY4__Item__c
) it
ON it.Id=pal.KNDY4__Item__c
WHERE newest.ParentId IS NOT NULL
AND oldest.OldValue IS NOT NULL
AND newest.NewValue != oldest.OldValue
GROUP BY pal.KNDY4__Item__c, pal.CS_PA_Line_Supplier__c
ORDER BY it.Name ASC
每行有沒有唯一的id?藍帽子之前的點只是一個錯誤?每天需要多少次更改才能找到開始和結束價格? –
對產品進行的每個更改都有唯一的Id,並且存在兩個產品共享的ParentID。 – mburgSolar
不知道每天有多少變化,我們只是開始跟蹤價格變化,但如果用戶犯了一個錯誤,然後將其恢復,每次記錄更改不應超過兩次,但如果他們將其恢復爲一次次數? – mburgSolar