2
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

4

2 回答 2

0

尽管我不确定您想如何计算您的 DifferencePercentage 列,但我对此进行了尝试,因此您可能想仔细查看一下。

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'   
于 2013-11-12T18:19:41.557 回答
0

好吧,这是“可能的”,但它变得非常疯狂。不要在家里尝试这个!你真的需要某种报告工具来做这种事情。

SQLFiddle

基于我们在另一篇文章中使用的逻辑,我们还需要一个变量。它将构建一个字符串来选择我们每个旋转列的最大值。所以我们将从:

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 个月后查看这个,我可能不知道这个查询到底发生了什么。

于 2013-11-12T18:29:06.830 回答