选择最便宜的价格,然后按供应商分组?

时间:2019-07-01 09:16:52

标签: sql tsql sybase

我想为技术采购团队提供默认的供应商设置,以使他们知道在哪里订购零件。这基于一个名为price_book的表。为此,我需要一个带有零件号,最便宜的价格和供应商代码的新表,以通过系统对其进行处理。如何仅选择提供最便宜价格的供应商?

我这样做是为了简化技术采购团队的订购过程。到目前为止,我每个供应商的价格最低(关于最小订购量)。我所缺少的是,只有一个价格最便宜的供应商被选中。因此,每件零件与相应供应商的最便宜价格。

查询很短:

final = []
for i in samp:
    row = {}
    row['id'] = i[0]
    row['name'] = i[1]
    row['code'] = i[2]
    row['id_parent'] = i[3]
    final.append(row)
print(final)

WHERE子句并不重要,因为它与内部流程有关。

结果应为包含零件号,价格和相应供应商的列的列表。按照零件号,应该只有供应商最便宜的价格。

4 个答案:

答案 0 :(得分:0)

尝试TOP(1) WITH TIES

SELECT TOP(1) WITH TIES pb.partno, pb.purch_price * c.rate [cheapest_price], pb.supplier
FROM price_book pb
LEFT JOIN currency c ON c.currency = pb.currency
WHERE pb.contract_id <> 0
AND pb.expire_date > Datediff(Day, '31 Dec 1971', Getdate())
AND pb.order_type = 'P'
AND pb.condition = 'N'
ORDER BY row_number() over(partition by partno order by pb.purch_price * c.rate [cheapest_price] desc);

答案 1 :(得分:0)

您可以为此尝试Windows功能,代码类似于以下示例

注意-该代码未经测试。

SELECT * from
(SELECT pb.partno,pb.supplier,pb.purch_price * c.rate as price, 
row_number() over(partition by pb.partno,pb.supplier order by pb.purch_price * c.rate )rn
FROM price_book pb
LEFT JOIN currency c ON c.currency = pb.currency
WHERE pb.contract_id <> 0
AND pb.expire_date > Datediff(Day, '31 Dec 1971', Getdate())
AND pb.order_type = 'P'
AND pb.condition = 'N')a
where a.rn=1

答案 2 :(得分:0)

看看:

DECLARE @priceBook TABLE (PriceID       INT IDENTITY(1, 1)
,                         PartNo        NVARCHAR(10)
,                         Price         DECIMAL(18, 2)
,                         fk_SupplierID INT);

INSERT INTO @priceBook (PartNo, Price, fk_SupplierID)
VALUES (N'123', 8.99, 1)
,      (N'123', 9.99, 2)
,      (N'456', 10.99, 1)
,      (N'456', 3.99, 2)
,      (N'456', 12.99, 3);

WITH cte AS
    (SELECT PriceID
     ,      PartNo
     ,      Price
     ,      fk_SupplierID
     ,      ROW_NUMBER() OVER (PARTITION BY PartNo
ORDER BY Price ASC) RN
       FROM @priceBook)
SELECT  cte.PriceID
,       cte.PartNo
,       cte.Price
,       cte.fk_SupplierID
  FROM  cte
 WHERE  rn = 1;

这是上述窗口功能的基本示例。这确实可以在MS SQL Server上运行,但不确定mySQL。给它一个测试,如果它对您有用,那么您可以调整您的确切结构。

答案 3 :(得分:0)

我现在发现了一个对我有用的代码:

SELECT B.partno,
MAX(C.supplier) AS supplier
FROM

(SELECT A.partno,
MIN(A.price) AS price
FROM

(SELECT DISTINCT pb.partno AS partno, MIN(pb.purch_price * c.rate) AS price, pb.supplier AS supplier
FROM price_book pb
LEFT JOIN currency c ON c.currency = pb.currency
LEFT JOIN part p ON p.partno = pb.partno
WHERE pb.contract_id <> 0
AND pb.expire_date > Datediff(Day, '31 Dec 1971', Getdate())
AND pb.order_type = 'P'
AND pb.condition = 'N'
AND p.mat_class IN ('C', 'E')
GROUP BY pb.partno, supplier) A

GROUP BY A.partno) B

JOIN (SELECT DISTINCT pb.partno AS partno, MIN(pb.purch_price * c.rate) AS price, pb.supplier AS supplier
FROM price_book pb
LEFT JOIN currency c ON c.currency = pb.currency
LEFT JOIN part p ON p.partno = pb.partno
WHERE pb.contract_id <> 0
AND pb.expire_date > Datediff(Day, '31 Dec 1971', Getdate())
AND pb.order_type = 'P'
AND pb.condition = 'N'
AND p.mat_class IN ('E', 'C')
GROUP BY pb.partno, supplier) C ON B.partno=C.partno and B.price=C.price
GROUP BY B.partno

非常感谢您给我的支持!

干杯, 多米尼克