sql >> Database teknologi >  >> RDS >> Sqlserver

Pris sammenligne med flere butikker i sql

Jeg tog et stik på dette, selvom jeg ikke var sikker på, hvordan du ville beregne din DifferencePercentage-kolonne, så du kan eventuelt se nærmere på det.

CREATE TABLE #tempProduct
(
    ID INT,
    SellerName VARCHAR(100),
    Total MONEY,
    Availability VARCHAR(100),
    Offer VARCHAR(100),
    Competitors INT
)

INSERT INTO #tempProduct (ID, SellerName, Total, Availability, Offer)
SELECT DISTINCT p.id, pp.SellerName, pp.Price + ISNULL(pp.Shipping,0), pp.Available, pp.Offer
FROM Products p
JOIN Product_Price pp
    ON p.id = pp.ProductId

-- Get Sears competitors
UPDATE tp
SET Competitors = pp.CompetitorCount
FROM #tempProduct tp
JOIN (
        SELECT ProductId, COUNT(sellerName) [CompetitorCount] 
        FROM Product_Price 
        WHERE SellerName <> 'Sears' AND Price + ISNULL(Shipping,0) IS NOT NULL
        GROUP BY ProductId
    ) pp
    ON pp.ProductId = tp.ID
WHERE tp.SellerName = 'Sears'

 SELECT DISTINCT 
    p.id, 
    p.ProductName,
    p.ProductCategory, 
    p.ProductImage, 
    p.ProductUri, 
    stp.Total [SearsTotal], 
    stp.Availability [SearsAvailability], 
    stp.Offer [SearsOffer], 
    stp.Competitors [#Competitors],
    100 - (((ISNULL(etp.Total,0) + ISNULL(atp.Total, 0))/stp.Competitors)/stp.Total) * 100 [DifferencePercentage(Sears & others)], -- Not sure how you want to calculate price difference
    atp.Total, 
    atp.Availability [AmazonTotal], 
    atp.Offer [AmazonOffer], 
    etp.Total [eBayTotal], 
    etp.Availability [eBayAvailability], 
    etp.Offer [eBayOffer]
 FROM Products p
JOIN Product_Price pp
    ON pp.ProductId = p.ID
JOIN #tempProduct stp
    ON stp.ID = p.id
JOIN #tempProduct etp
    ON etp.ID = p.id
JOIN #tempProduct atp
    ON atp.ID = p.id
WHERE stp.SellerName = 'Sears'  
AND etp.SellerName = 'eBay'
AND atp.SellerName = 'Amazon'   


  1. hvordan man opretter trigger til at lave sammenkædning af felter

  2. ORA-00918:kolonne er tvetydigt defineret i SELECT *

  3. Mysql visualiseringsværktøjer

  4. Sequelize Sådan sammenlignes år af en dato i forespørgslen