0

这就是我的价格表的样子

Product_id prod_upc    str_nbr  Price
prod110010 4122067755  12       1.22
prod110010 4122067755  21       2.88
prod110010 4122063030  21       3.88
prod110010 4122063030  12       2.88
prod110010 4122063031  12       2.88
prod110010 4122063031  21       4.88

我有两个要求要解决

  1. 为所有商店的所有产品获得最低价格的 UPC?根据上述数据,输出应该是

    prod110010  4122067755  12      1.22
    
  2. 为每家商店的所有产品获得最低价格的 upc ?根据上述数据,输出应该是

    prod110010  4122067755  12      1.22
    prod110010  4122067755  21      2.88
    

我尝试了可能的内部查询,但没有任何效果,请在这里的任何 SQL 专家提供帮助。

我对特定商店的查询是:

SELECT DISTINCT t.product_id, t.prod_upc,t.str_nbr, t.MINVALUE 
  FROM ( SELECT dpc.product_id, hpd.prod_upc, str_nbr
              , MIN(hpd.curr_retl_prc) OVER 
                 (PARTITION BY dpc.product_id) MINVALUE 
           FROM prc_dta ) AS T

对于全球:

SELECT DISTINCT t.product_id, t.prod_upc, t.str_nbr, t.MINVALUE 
  FROM ( SELECT dpc.product_id, hpd.prod_upc, hpd.str_nbr
              , MIN(hpd.curr_retl_prc) OVER 
                    (PARTITION BY dpc.product_id,hpd.str_nbr) MINVALUE 
           FROM prc_dta) AS T
4

2 回答 2

0
This query to get min price upc row : 

    select top 1 Product_id,prod_upc,str_nbr,Price from tbl where prod_upc=(select top 1 min(prod_upc) from tb1 ) order by Price asc

This query get all row of min price of upc:

select Product_id,prod_upc,str_nbr,Price from tbl where prod_upc=(select top 1 min(prod_upc) from tb1 ) order by Price asc
于 2012-09-15T07:26:53.223 回答
0
WITH ranked (Product_id, prod_upc, str_nbr, Price, pricerank)
AS ( SELECT Product_id, prod_upc, str_nbr, Price,
    ROW_NUMBER() OVER ( PARTITION BY str_nbr ORDER BY Price ) AS pricerank
    FROM tbl
   )
SELECT Product_id, prod_upc, str_nbr, Price FROM ranked WHERE pricerank = 1
于 2012-09-15T07:29:59.600 回答