使用来自select查询的数据更新表,包括第二个表

问题描述 投票:0回答:6

此查询的目标是使用Table2中的最大和最小价格更新表1中的MinPrice和MaxPrice列,其中CountryCode和ProductCode匹配。

当我运行此查询时,表1中的Max和Minprice的整列填充了select查询中出现的第一个值。

如果我自己运行select查询,它会显示每个产品的每个国家/地区的正确最小值和最大值。

   UPDATE Table1
        SET MinPrice = MinOfPrice, Maxprice = MaxOfPrice
        FROM (SELECT Min(lp.Price) AS MinOfPrice, Max(lp.Price) AS MaxOfPrice 
        FROM Table2 lp INNER JOIN Table1 d ON lp.CountryCode = d.CountryCode AND 
        lp.ProductCode = d.ProductCode
        GROUP BY lp.CountryCode, lp.ProductCode, lp.PriceOriginTypeCode) h ;
sql sql-server
6个回答
1
投票

我认为这将是您的解决方案或几乎与您的解决方案类似。

    UPDATE Table1 
            SET table1.MinPrice = lp.MinOfPrice, table1.Maxprice = lp.MaxOfPrice
            FROM Table1 INNER JOIN ( SELECT top 100 percent  CountryCode, ProductCode, MAX(Price) as MaxOfPrice, MIN(Price) as MinOfPrice FROM Table1 group by  CountryCode, ProductCode) as lp ON lp.CountryCode = Table1.CountryCode AND 
            lp.ProductCode = Table1.ProductCode 

1
投票

只是为了解释我的评论

UPDATE Table1
        SET MinPrice = MinOfPrice, Maxprice = MaxOfPrice
FROM (SELECT Min(lp.Price) AS MinOfPrice, Max(lp.Price) AS MaxOfPrice, lp.CountryCode,lp.ProductCode
        FROM Table2 lp INNER JOIN Table1 d ON lp.CountryCode = d.CountryCode AND 
        lp.ProductCode = d.ProductCode
        GROUP BY lp.CountryCode, lp.ProductCode, lp.PriceOriginTypeCode) h join Table1 on Table1.ProductCode=h.ProductCode and Table1.CountryCode=h.CountryCode

1
投票

我猜你当前的查询正在修改每个记录的table1的最小值和最大值,尝试使用下面的公共列进行更新

   UPDATE Table1
    SET MinPrice = MinOfPrice, Maxprice 
   = MaxOfPrice
      FROM (SELECT Min(lp.Price) AS 
      MinOfPrice, Max(lp.Price) AS 
     MaxOfPrice 
    FROM Table 2 lp INNER JOIN Table1 d 
   ON lp.CountryCode = d.CountryCode 
  AND 
      lp.ProductCode = d.ProductCode
    GROUP BY lp.CountryCode, 
    lp.ProductCode, 
    lp.PriceOriginTypeCode) h
      where h.some_common_column =
             Table1.common_column
        -- make this query as corelated via above where clause

0
投票

您应该在子查询中获取聚合值,然后执行连接。像这样的东西:

update t1
set t1.minPrice = t2.minPrice
    ,t1.maxPrice = t2.maxPrice
from table1 t1
         inner join (
                    select min(price) minPrice
                          ,max(price) maxPrice
                          ,productCode
                          ,countryCode
                    from table2
                    group by productCode
                             ,countryCode
                     ) t2 on t2.productCode = t.productCode
                           and t2.countryCode = t.countryCode

0
投票

试试这个:

UPDATE Table1
SET MinPrice = h.MinOfPrice
,Maxprice = h.MaxOfPrice
FROM (
      SELECT d.CountryCode, d.ProductCode,
             Min(lp.Price) AS MinOfPrice,
             Max(lp.Price) AS MaxOfPrice 
      FROM Table 2 lp 
      INNER JOIN Table1 d 
         ON lp.CountryCode = d.CountryCode
         AND lp.ProductCode = d.ProductCode
      GROUP BY d.CountryCode, d.ProductCode
) h ;

我已经摆脱了lp.PriceOriginTypeCode,因为我不知道这是否也是TABLE1的一部分


0
投票

你必须将Table1带出你的子查询。我不明白你为什么要分为三列 - 这样你可以为一个CountryCode-ProductCode组合有几行。

UPDATE t
SET MinPrice = h.MinOfPrice, Maxprice = h.MaxOfPrice
FROM Table1 t
    JOIN (
        SELECT  lp.CountryCode,
             lp.ProductCode,
             Min(lp.Price) AS MinOfPrice,
             Max(lp.Price) AS MaxOfPrice 
        FROM Table2 lp
        GROUP BY lp.CountryCode, lp.ProductCode
        , lp.PriceOriginTypeCode -- why do you also group by this column?
    ) h ON h.CountryCode = t.CountryCode AND h.ProductCode = t.ProductCode;
© www.soinside.com 2019 - 2024. All rights reserved.