问题
create table Products
(
id int,
ProductName varchar(200),
ProductCategory varchar(200),
ProductImage varchar(200),
ProductUri varchar(200),
)
Insert into Products values(135, 'Product X', 'Digital Camera', Null, Null)
Insert into Products values(136, 'Product Y', 'Mobile', Null, Null)
create table Product_Price
(
id int,
ProductId int,
dt date,
SellerName varchar(20),
Available varchar(20),
Offer varchar(20),
Price money,
Shipping money
)
insert into Product_Price values (1, 135,'2012-01-16','Sears','In Stock','30% discount',32.00,2.00)
insert into Product_Price values (2, 135,'2012-01-16','Amazon','In Stock',Null,30.00,NULL)
insert into Product_Price values (3, 135,'2012-01-16','eBay','Just 2 Left',Null,28.00,1.00)
insert into Product_Price values (4, 136,'2012-01-16','Sears','In Stock','30% discount',30.00,6.00)
insert into Product_Price values (5, 136,'2012-01-16','Amazon','In Stock',Null,28.00,4.00)
insert into Product_Price values (6, 136,'2012-01-16','eBay','Out Of stock',Null,Null,Null)
And i want result like this :
ID ProductName ProductCategory ProductImage ProductUri SearsTotal Price(Price+Shipping) SearsAvailablity SearsOffer #Competitors DifferencePercentage(Sears & others) AmazonTotal Price(Price+Shipping) AmazonAvailablity AmazonOffer eBayTotal Price(Price+Shipping) eBayAvailablity eBayOffer 135 Product X Digital Camera NULL NULL 34 In Stock 30% discount 2 15.25423729 30 In Stock NULL 29 Just 2 Left NULL 136 Product Y Mobile NULL NULL 36 In Stock 25% discount 1 12.5 32 In Stock NULL NULL Out Of stock NULL
Step 1 is here : Product price comparison in sql My test is here : http://sqlfiddle.com/#!3/ec1e7/6
回答1:
I took a stab at this although I wasn't sure how you wanted to calculate your DifferencePercentage column, so you may want to look at that closer.
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'
回答2:
Well, this is "possible", but it's getting pretty crazy. Don't try this at home! You really need some sort of reporting tool to do this kind of thing.
SQLFiddle
Building on the logic we used in the other post, we'll need one more variable. It's going to build a string that selects the max of each of our pivoted columns. So we'll start with:
DECLARE @cols AS VARCHAR(MAX)
DECLARE @cols2 AS VARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)
DECLARE @COL_ALIASES AS VARCHAR(MAX)
Select @COL_ALIASES = ISNULL(@COL_ALIASES + ', ', '') +
'Max(' + QUOTENAME(SellerName + '_TOTAL') + ') As ' + QUOTENAME(SellerName + '_TOTAL') + ', ' +
'Max(' + QUOTENAME(SellerName + '_AVAILABLE') + ') As ' + QUOTENAME(SellerName + '_AVAILABLE')
from #Product_Price
select @cols = STUFF((SELECT distinct ',' +
QUOTENAME(SellerName + '_TOTAL')
FROM #Product_Price
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '')
select @cols2 = STUFF((SELECT distinct ',' +
QUOTENAME(SellerName + '_AVAILABLE')
FROM #Product_Price
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '')
Then we assemble the query string from all of that fun stuff:
select @query =
' select p.id ,
p.productname,' + + @COL_ALIASES + CHAR(10) +
' from Products p
inner join (
select
productId,
' + @cols + ',' + @cols2 + '
from
(
select
p1.id as ProductID,
p2.sellername + ''' + '_TOTAL' + ''' As TotalSeller,
p2.sellername + ''' + '_AVAILABLE' + ''' as AvailableSeller,
p2.price,
p2.available
from
products p1
inner join product_price p2
on p1.id = p2.productid ) t1
PIVOT (max(price) for TotalSeller in (' + @cols + ')) t
PIVOT (max(available) for AvailableSeller in (' + @cols2 + ') ) u )
pvt
ON p.id = pvt.productid
GROUP BY p.id,
p.productname
'
And finally, we run it:
Exec sp_executesql @Query
You would just keep expanding on this logic to add your additional pivots. You can see why I said this is getting crazy. I did this much just because I kind of enjoyed the challenge, but I certainly wouldn't recommend trying to actually use this. As folks have pointed out in other posts, you could expose yourself to SQL injection, and it would be a beast to maintain. If I looked at this 6 months from now, I'd probably have no idea what the heck was going on in this query.
来源:https://stackoverflow.com/questions/19933235/price-compare-with-multiple-store-in-sql