无法比较两个表中两列的值,如果它在第一个表中,则返回最佳行

时间:2014-08-08 17:05:14

标签: sql sql-server

我正在处理一个查询,该查询比较两个表并为每种类型的产品返回一行或零行。如果在比较两个表的值之后返回零,并且第二行至少有一个比第一个表更好的行,并且如果第一个表具有最佳行,则返回一行。

例如,我有两个表T1和T2,其中包含以下行

表T1

Product Price Tax
Pen     10    2.25
Pen      5    1.25
Pen     15    1.5
Board   25    5.26
Board    2    NULL
Water    5    10

表T2

 Product Price  Tax   
 Pen      8     2.5
 Pen      12    4.2
 Board    NULL  4.26
  Book     10    5

在上面两个表上运行SQL查询后,结果应该是

Product Price Tax
Pen     15    NULL
Board   25    5.26
Water    5    10

以上结果可以解释为 Price Pen T1 15tax,这是两个表中最高的pen T2 NULL最高tax因此我们为15返回Price,为price返回tax 同样,理事会在T1中同时包含255.26,因此返回WaterT2,而T1Book中没有条目1}}返回T1中的最高值,Book没有返回任何值,因为select t1.product, (case when t1.price >= t2.price then t1.price when t2.price > t1.price then NULL else coalesce(t1.price, t2.price) end) as price, (case when t1.tax >= t2.tax then t1.tax when t2.tax > t1.tax then NULL else coalesce(t1.tax, t2.tax) end) as tax from table1 t1 left join table2 t2 on t1.product = t2.product; 没有{{1}}的条目

我正在使用以下查询,但他们没有产生预期的结果

{{1}}

3 个答案:

答案 0 :(得分:1)

我相信你要找的是

SELECT t1.Product, 
    CASE 
        WHEN MAX(t1.Price) >= MAX(t2.Price) THEN MAX(t1.Price)
        WHEN MAX(t2.Price) > MAX(t1.Price)  THEN NULL
        ELSE COALESCE(MAX(t1.Price), MAX(t2.Price)) 
    END AS Price,
    CASE 
        WHEN MAX(t1.Price) >= MAX(t2.Price) AND MAX(t1.Tax) >= MAX(t2.Tax) THEN MAX(t1.Tax)
        WHEN MAX(t1.Price) >= MAX(t2.Price) AND MAX(t1.Tax) < MAX(t2.Tax)  THEN NULL
        ELSE COALESCE(MAX(t1.Tax), MAX(t2.Tax)) 
    END AS Tax
   FROM #Temp t1
   LEFT JOIN #Temp1 t2
   ON t1.Product = t2.Product 
   GROUP BY t1.Product, t2.Product

答案 1 :(得分:1)

您需要进行分组和聚合。为了安全起见,我会使用派生表:

select t1.product,
       (case when t1.price >= COALESCE(t2.price,0) then t1.price
             else NULL
        end) as price,
       (case when t1.tax >= COALESCE(t2.tax,0) then t1.tax
             else NULL
        end) as tax
from (SELECT product, MAX(price) as price, MAX(tax) as tax FROM table1 GROUP BY product) t1 left join
     (SELECT product, MAX(price) as price, MAX(tax) as tax FROM table2 GROUP BY product) t2
     on t1.product = t2.product;

答案 2 :(得分:0)

另一种方式,使用row_number()在两个派生表中枚举我们的价格:

declare @T1 table (Product varchar(10), Price int, Tax decimal(10,2))
declare @T2 table (Product varchar(10), Price int, Tax decimal(10,2))

insert into @T1
       select 'Pen',     10,    2.25 union all
       select 'Pen',      5,    1.25 union all
       select 'Pen',     15,    1.5 union all
       select 'Board',  25,    5.26 union all
       select 'Board',   2,    NULL union all
       select 'Water',   5,    10;

insert into @T2
       select 'Pen',      8,     2.5 union all
       select 'Pen',      12,    4.2 union all
       select 'Board',    NULL,  4.26


select one.Product,
       [Price] = case when one.Price > isnull(two.Price, 0) then one.Price else two.Price end,
       [Tax] = case when one.Tax > isnull(two.Tax, 0.0) then one.Tax else null end
from   (
              select Product, Price, Tax, [r]= row_number() over(partition by Product order by Price desc)
              from @t1
       ) one
left
join   (
              select Product, Price, Tax, [r]= row_number() over(partition by Product order by Price desc)
              from @t2
       ) two
       on one.Product = two.Product and two.r=1
where  one.r = 1