从两个表中选择最大值,最小值

时间:2022-02-28 07:48:05

I have two tables. Differ in that an archive is a table and the other holds the current record. These are the tables recording sales in the company. In both we have among other fields: id, name, price of sale. I need to select from both tables, the highest and lowest price for a given name. I tried to do with the query:

我有两张桌子。不同之处在于存档是一个表而另一个存放当前记录。这些是记录公司销售额的表格。在这两个领域我们都有其他领域:id,名称,销售价格。我需要从两个表中选择给定名称的最高价和最低价。我试着用这个查询:

select name, max (price_of_sale), min (price_of_sale)
from wapzby
union
select name, max (price_of_sale), min (price_of_sale)
from wpzby
order by name

but such an inquiry draws me two records - one of the current table, one table archival. I want to chose a name for the smallest and the largest price immediately from both tables. How do I get this query?

但这样的询问给我带来了两条记录 - 一张当前的表,一张表档案。我想立即从两个表中选择最小和最大价格的名称。我如何获得此查询?

4 个解决方案

#1


22  

Here's two options (MSSql compliant)

这里有两个选项(符合MSSql)

Note: UNION ALL will combine the sets without eliminating duplicates. That's a much simpler behavior than UNION.

注意:UNION ALL将组合集合而不消除重复。这比UNION简单得多。

SELECT Name, MAX(Price_Of_Sale) as MaxPrice, MIN(Price_Of_Sale) as MinPrice
FROM
(
    SELECT Name, Price_Of_Sale
    FROM wapzby
    UNION ALL
    SELECT Name, Price_Of_Sale
    FROM wpzby
) as subQuery
GROUP BY Name
ORDER BY Name

This one figures out the max and min from each table before combining the set - it may be more performant to do it this way.

这个在合并集合之前计算出每个表的最大值和最小值 - 以这种方式执行它可能更高效。

SELECT Name, MAX(MaxPrice) as MaxPrice, MIN(MinPrice) as MinPrice
FROM
(
    SELECT Name, MAX(Price_Of_Sale) as MaxPrice, MIN(Price_Of_Sale) as MinPrice
    FROM wapzby
    GROUP BY Name
    UNION ALL
    SELECT Name, MAX(Price_Of_Sale) as MaxPrice, MIN(Price_Of_Sale) as MinPrice
    FROM wpzby
    GROUP BY Name
) as subQuery
GROUP BY Name
ORDER BY Name

#2


6  

In SQL Server you could use a subquery:

在SQL Server中,您可以使用子查询:

SELECT  [name],
        MAX([price_of_sale]) AS [MAX price_of_sale],
        MIN([price_of_sale]) AS [MIN price_of_sale]
FROM (
    SELECT  [name],
            [price_of_sale]
    FROM    [dbo].[wapzby]
    UNION
    SELECT  [name],
            [price_of_sale]
    FROM    [dbo].[wpzby]
) u
GROUP BY [name]
ORDER BY [name]

#3


1  

Is this more like what you want?

这更像你想要的吗?

SELECT 
    a.name,
    MAX (a.price_of_sale), 
    MIN (a.price_of_sale) ,  
    b.name, 
    MAX (b.price_of_sale), 
    MIN (b.price_of_sale) 
FROM 
    wapzby a, 
    wpzby b
ORDER BY 
    a.name 

It's untested but should return all your records on one row without the need for a union

它未经测试但应该在一行中返回所有记录,而不需要联合

#4


1  

SELECT MAX(value) FROM tabl1 UNION SELECT MAX(value) FROM tabl2;
SELECT MIN(value) FROM tabl1 UNION SELECT MIN(value) FROM tabl2;

#1


22  

Here's two options (MSSql compliant)

这里有两个选项(符合MSSql)

Note: UNION ALL will combine the sets without eliminating duplicates. That's a much simpler behavior than UNION.

注意:UNION ALL将组合集合而不消除重复。这比UNION简单得多。

SELECT Name, MAX(Price_Of_Sale) as MaxPrice, MIN(Price_Of_Sale) as MinPrice
FROM
(
    SELECT Name, Price_Of_Sale
    FROM wapzby
    UNION ALL
    SELECT Name, Price_Of_Sale
    FROM wpzby
) as subQuery
GROUP BY Name
ORDER BY Name

This one figures out the max and min from each table before combining the set - it may be more performant to do it this way.

这个在合并集合之前计算出每个表的最大值和最小值 - 以这种方式执行它可能更高效。

SELECT Name, MAX(MaxPrice) as MaxPrice, MIN(MinPrice) as MinPrice
FROM
(
    SELECT Name, MAX(Price_Of_Sale) as MaxPrice, MIN(Price_Of_Sale) as MinPrice
    FROM wapzby
    GROUP BY Name
    UNION ALL
    SELECT Name, MAX(Price_Of_Sale) as MaxPrice, MIN(Price_Of_Sale) as MinPrice
    FROM wpzby
    GROUP BY Name
) as subQuery
GROUP BY Name
ORDER BY Name

#2


6  

In SQL Server you could use a subquery:

在SQL Server中,您可以使用子查询:

SELECT  [name],
        MAX([price_of_sale]) AS [MAX price_of_sale],
        MIN([price_of_sale]) AS [MIN price_of_sale]
FROM (
    SELECT  [name],
            [price_of_sale]
    FROM    [dbo].[wapzby]
    UNION
    SELECT  [name],
            [price_of_sale]
    FROM    [dbo].[wpzby]
) u
GROUP BY [name]
ORDER BY [name]

#3


1  

Is this more like what you want?

这更像你想要的吗?

SELECT 
    a.name,
    MAX (a.price_of_sale), 
    MIN (a.price_of_sale) ,  
    b.name, 
    MAX (b.price_of_sale), 
    MIN (b.price_of_sale) 
FROM 
    wapzby a, 
    wpzby b
ORDER BY 
    a.name 

It's untested but should return all your records on one row without the need for a union

它未经测试但应该在一行中返回所有记录,而不需要联合

#4


1  

SELECT MAX(value) FROM tabl1 UNION SELECT MAX(value) FROM tabl2;
SELECT MIN(value) FROM tabl1 UNION SELECT MIN(value) FROM tabl2;