每個產品都有一個LatLng. 隨著時間的推移,有時產品可以復制,但很難發現。價格或圖片可能略有不同。
我想Products通過他們ProductType和他們Price在每個位置進行比較,并將它們放在地圖上,以便更容易找到重復項。
對于每個產品,我計算其價格的 5%,然后我可以從其他產品價格中添加/減去它以找到粗略的匹配。
Products
ProductID ProductType Price Latitude Longitude
--------------------------------------------------------------------
ABC Red Widget 500 12.34 67.89
DEF Red Widget 505 12.34 67.89
MNO Red Widget 480 12.34 67.89
RST Red Widget 500 12.34 67.89
UVW Red Widget 300 12.34 67.89
JKL Blue Widget 800 76.54 32.10
XYZ Blue Widget 800 45.67 23.45
預期的結果是ABC, DEF, MNOANDRST應該被回傳,因為它們都是Red Widgets, 位于相同的位置并且具有完全相同的價格或彼此價格的 5% 以內的價格。
UVW 超出百分比,因此可能不是重復的,不應回傳。
JKL并且XYZ是Blue Widgets與同價位的,但不是在同一個位置,因此沒有回來。
SQL
到目前為止,這是我所擁有的,但它回傳的結果太多了。我認為它正在回傳在每個位置之外匹配的行。JOIN 中的 AND 對我來說是正確的。感覺 OR 可能允許更靈活的匹配,但括起來的括號看起來是正確的......
WITH cte AS
(
SELECT
p.ProductID,
p.ProductType,
p.Price,
(p.Price / 100) * 5 AS PricePercent,
ROUND(p.Latitude, 3) AS Latitude,
ROUND(p.Longitude, 3) AS Longitude
FROM
Products p
WHERE
p.Latitude IS NOT NULL AND p.Longitude IS NOT NULL
)
SELECT
DISTINCT a.ProductID,
a.Price,
a.Latitude, a.Longitude
FROM
cte a
INNER JOIN
/* ProductIDs don't match */
cte b ON a.ProductID <> b.ProductID
/* match only where location is the same*/
AND a.Latitude = b.Latitude
AND a.Longitude = b.Longitude
/* match only where Product Type is the same*/
AND a.ProductType = b.ProductType
/*match only if price is the same, or within 5% above or 5% below price*/
AND (/*same price*/
b.Price = a.Price
OR
/*b.price is within percentage over a.price*/
(b.Price > a.Price AND b.Price < (a.Price a.PricePercent))
OR
/*b.price is within percentage under a.price */
(b.Price < a.Price AND b.Price >(a.Price-a.PricePercent)))
uj5u.com熱心網友回復:
首先,這件事:
AND (/*same price*/
b.Price = a.Price
OR
/*b.price is within percentage over a.price*/
(b.Price > a.Price AND b.Price < (a.Price a.PricePercent))
OR
/*b.price is within percentage under a.price */
(b.Price < a.Price AND b.Price >(a.Price-a.PricePercent))
)
是否(或應該)與此相同:
AND (
-- price within range /- percent
b.Price < (a.Price a.PricePercent)
AND b.Price >(a.Price-a.PricePercent)
)
其次對我來說它有效......:
SQL小提琴
MS SQL Server 2017 架構設定:
查詢 1:
WITH cte AS
(
SELECT
p.ProductID,
p.ProductType,
p.Price,
(p.Price / 100) * 5 AS PricePercent,
ROUND(p.Latitude, 3) AS Latitude,
ROUND(p.Longitude, 3) AS Longitude
FROM
Products p
WHERE
p.Latitude IS NOT NULL AND p.Longitude IS NOT NULL
)
SELECT
DISTINCT a.ProductID,
a.Price,
a.Latitude, a.Longitude
FROM
cte a
INNER JOIN
/* ProductIDs don't match */
cte b ON a.ProductID <> b.ProductID
/* match only where location is the same*/
AND a.Latitude = b.Latitude
AND a.Longitude = b.Longitude
/* match only where Product Type is the same*/
AND a.ProductType = b.ProductType
/*match only if price is the same, or within 5% above or 5% below price*/
AND (
-- price within range /- percent
b.Price < (a.Price a.PricePercent)
AND b.Price >(a.Price-a.PricePercent)
)
結果:
| ProductID | Price | Latitude | Longitude |
|-----------|-------|----------|-----------|
| ABC | 500 | 12 | 68 |
| DEF | 505 | 12 | 68 |
| RST | 500 | 12 | 68 |
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329762.html
標籤:sql sql-server 内部联接
下一篇:主查詢列中的SQL查詢子表
