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)
我想要这样的结果:
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
第1步在此处:Product price comparison in sql 我的测试在这里:http://sqlfiddle.com/#!3/ec1e7/6
答案 0 :(得分:0)
虽然我不确定你想要如何计算你的差异百分比列,但我可能想要更接近这一点。
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 :(得分:0)
嗯,这是“可能的”,但它变得非常疯狂。不要在家尝试这个!你真的需要某种报告工具来做这种事情。
基于我们在另一篇文章中使用的逻辑,我们还需要一个变量。它将构建一个字符串,用于选择每个旋转列的最大值。所以我们将从:
开始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, '')
然后我们从所有有趣的东西中组装查询字符串:
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
'
最后,我们运行它:
Exec sp_executesql @Query
您将继续扩展此逻辑以添加其他支点。你可以看出为什么我说这变得疯狂。我做了这么多只是因为我很喜欢这个挑战,但我当然不会建议尝试实际使用它。正如大家在其他帖子中指出的那样,你可以将自己暴露给SQL注入,这将是一个维护的野兽。如果我从现在开始看了6个月,我可能不知道这个查询到底发生了什么。