2016-06-16 68 views
1

我正在創建一個查詢來查找特定產品的最便宜供應商。SQL顯示最便宜的供應商

用戶將可以選擇最多4個供應商進行比較。

我的全部代碼

declare @Sup as varchar(20) set @Sup = 'WESTF' 
declare @Sup2 as varchar(20) set @Sup2 = 'NAVIG' 
declare @Sup3 as varchar(20) set @Sup3 = 'PRIMF' 
declare @Sup4 as varchar(20) set @Sup4 = '' 

select 'Product' as ProductCode, 'Description' as Description, 
@Sup as Cost, @Sup as SupplierDate, 
@Sup2 as Cost2, @Sup2 as SupplierDate2, 
@Sup3 as Cost3, @Sup3 as SupplierDate3, 
@Sup4 as Cost4, @Sup4 as SupplierDate4, 0 as Cheapest 

union all 

select p.ProductCode,p.Description, 
'£' + cast(psp1.Cost as varchar) + ' Per ' + Cast(psp1.Per as varchar)as Cost1,psp1.SupplierDate, 
'£' + cast(psp2.Cost as varchar) + ' Per ' + Cast(psp2.Per as varchar)as Cost2,psp2.SupplierDate, 
'£' + cast(psp3.Cost as varchar) + ' Per ' + Cast(psp3.Per as varchar)as Cost3,psp3.SupplierDate, 
'£' + cast(psp4.Cost as varchar) + ' Per ' + Cast(psp4.Per as varchar)as Cost4,psp4.SupplierDate, 


MinValue as Cheapest 
from 
     product as p 
     left join 
     (
     select psp.productid,psp.StandardBuy as Cost ,Per.Name as Per, 
     per.ConversionToBase,s.SupplierCode, 
     cast(convert(Date, psp.StandardBuyDateTime, 103) as varchar) as SupplierDate 
     from ProductSupplierPrice as psp 
     left join Supplier as s on s.SupplierID = psp.SupplierID 
     left join Per on Per.PerID = psp.BuyPerID 
     where s.SupplierCode = @Sup 
     ) as psp1 on psp1.ProductID = p.ProductId 

     left join 
     (
     select psp.productid,psp.StandardBuy as Cost ,Per.Name as Per, 
     per.ConversionToBase,s.SupplierCode, 
     cast(convert(Date, psp.StandardBuyDateTime, 103) as varchar) as SupplierDate 
     from ProductSupplierPrice as psp 
     left join Supplier as s on s.SupplierID = psp.SupplierID 
     left join Per on Per.PerID = psp.BuyPerID 
     where s.SupplierCode = @Sup2 
     ) as psp2 on psp2.ProductID = p.ProductId 

     left join 
     (
     select psp.productid,psp.StandardBuy as Cost ,Per.Name as Per, 
     per.ConversionToBase,s.SupplierCode, 
     cast(convert(Date, psp.StandardBuyDateTime, 103) as varchar) as SupplierDate 
     from ProductSupplierPrice as psp 
     left join Supplier as s on s.SupplierID = psp.SupplierID 
     left join Per on Per.PerID = psp.BuyPerID 
     where s.SupplierCode = @Sup3 
     ) as psp3 on psp3.ProductID = p.ProductId 

     left join 
     (
     select psp.productid,psp.StandardBuy as Cost ,Per.Name as Per, 
     per.ConversionToBase,s.SupplierCode, 
     cast(convert(Date, psp.StandardBuyDateTime, 103) as varchar) as SupplierDate 
     from ProductSupplierPrice as psp 
     left join Supplier as s on s.SupplierID = psp.SupplierID 
     left join Per on Per.PerID = psp.BuyPerID 
     where s.SupplierCode = @Sup4 
     ) as psp4 on psp4.ProductID = p.ProductId 

CROSS APPLY (SELECT MIN(d) MinValue FROM (VALUES 
(psp1.Cost/psp1.ConversionToBase), 
(psp2.Cost/psp2.ConversionToBase), 
(psp3.Cost/psp3.ConversionToBase), 
(psp4.Cost/psp4.ConversionToBase)) AS a(d)) A 

where p.Deleted = 0 and p.Description like '%endfeed%' 

但是最重要的一點是:

CROSS APPLY (SELECT MIN(d) MinValue FROM (VALUES 
(psp1.Cost/psp1.ConversionToBase), 
(psp2.Cost/psp2.ConversionToBase), 
(psp3.Cost/psp3.ConversionToBase), 
(psp4.Cost/psp4.ConversionToBase)) AS a(d)) A 

此找到最便宜的價格,但我不想顯示我想知道供應商代碼最便宜的價格與最便宜的價格有關。

我不確定最好的方法來做到這一點,任何幫助,將不勝感激。

+1

查找ROW_NUMBER()在 – Paparazzi

回答

1

CROSS APPLY (
SELECT top(1) d,supplierId 
FROM (VALUES 
    (psp1.Cost/psp1.ConversionToBase, psp1.<supplierID here>), 
    (psp2.Cost/psp2.ConversionToBase, psp2.<supplierID here>), 
    (psp3.Cost/psp3.ConversionToBase, psp3.<supplierID here>), 
    (psp4.Cost/psp4.ConversionToBase, psp4.<supplierID here>) 
    ) AS a(d,supplierId) 
ORDER BY d ASC) A 
+0

以爲這樣做是再更換它,但它似乎顯示了錯誤的方式全面展示最昂貴的。 – Richard

+0

啊,我誤讀了它,ORDER BY .. ASC。更正 – Serg

+0

是的,我試過了,然後他們都只是說空,不知道爲什麼。 – Richard