【问题标题】:Using MAX with multiple tables将 MAX 与多个表一起使用
【发布时间】:2011-07-19 20:17:37
【问题描述】:

我有四个表:产品、电脑、笔记本电脑和打印机。

    Products(maker, model, type)
    PC(code, model, speed, hd, cd, price)
    Laptop(code, model, speed, ram, hd, screen, price)
    Printer(code, model, color, type price)

我需要找到价格最高的产品型号(PC、笔记本电脑或打印机)。这不适用于 case 语句,因为如果两个型号的价格最高,则两者都需要显示,并且使用 case 将只选择一个然后退出 case 语句。我想使用 UNION 运算符来执行此操作,但我不知道该怎么做。这是我目前所拥有的:

SELECT model FROM 
(SELECT model, MAX(price) FROM 
(SELECT model, price FROM Pc UNION ALL SELECT model, price FROM Laptop UNION ALL 
 SELECT model, price FROM Printer) 
 GROUP BY model)

但这是不正确的语法,我不知道为什么。有什么想法吗?

【问题讨论】:

    标签: sql subquery union aggregate-functions multiple-tables


    【解决方案1】:
    Select datatable.model as price from (
        Select P.model,P.price from PC P where P.price=(select Max(Q.price) from PC Q)
        Union 
        Select P.model,P.price from Laptop P where P.price=(select Max(Q.price) from Laptop Q)
        Union 
        Select P.model,P.price from Printer P where P.price=(select Max(Q.price) from Printer Q)
    ) as datatable where datatable.price=(
        Select Max(newtable.price) as price from (
            Select P.model,P.price from PC P where P.price=(select Max(Q.price) from PC Q)
            Union 
            Select P.model,P.price from Laptop P where P.price=(select Max(Q.price) from Laptop Q)
            Union 
            Select P.model,P.price from Printer P where P.price=(select Max(Q.price) from Printer Q)) as newtable)
    

    【讨论】:

      【解决方案2】:

      您需要给派生表起别名:see this post


      编辑:这应该可以得到最高价格的模型。 (我不确定这是否是 sql server 的正确语法。)

      with max_price(price) as (
        SELECT MAX(price)
             FROM (
                SELECT price
                FROM Pc
                UNION ALL
                SELECT price
                FROM Laptop
                UNION ALL 
                SELECT price
                FROM Printer
             ) as sub1
      )
      
      SELECT model, price
      FROM (
         SELECT model, price
         FROM Pc
         UNION ALL
         SELECT model, price
         FROM Laptop
         UNION ALL 
         SELECT model, price
         FROM Printer
      ) as sub2
      JOIN max_price m ON m.price = sub2.price
      

      【讨论】:

      • 解决了语法问题,但现在我的查询返回每个型号,而不仅仅是价格最高的型号....
      • 几乎,但它说价格是无效的列名。我需要一张桌子吗?运营商让价格明确?
      • 我不太确定 with ... 是什么,正如你所看到的,我是初学者:/ 我想我明白了,但 sql server 仍然不喜欢它.它说没有为“max_price”的第 1 列指定列名
      • 现在它只是说 max_price 是一个无效的列名。使用 UNION 之外的其他东西会更容易吗?
      • 好的。我觉得这个应该做。我什至在 sql server 上测试过... :)
      【解决方案3】:

      这是我的解决方案,它是有效的。我已经试过了。

      WITH PRICE_MAX AS (select model, price
      from pc
      where price = (select max(price) 
                     from pc)
      UNION
      select model, price
      from laptop
      where price = (select max(price) 
                     from laptop)
      UNION
      select model, price
      from printer
      where price = (select max(price) 
                     from printer))
      
      select model from PRICE_MAX
      where price = (select Max(price) 
                     from PRICE_MAX)
      

      【讨论】:

        【解决方案4】:
        Select b.model from
        (Select a.model, Max(a.price) as price from
        (Select model, MAX(price) as price from PC group by model
        union
        Select model, MAX(price) as price from Laptop group by model
        union
        Select model, MAX(price) as price from Printer group by model)a
        Group by a.model)b
        where b.price=(Select Max(c.price) from(Select model, MAX(price) as price from PC group by model
        union
        Select model, MAX(price) as price from Laptop group by model
        union
        Select model, MAX(price) as price from Printer group by model)c)
        

        【讨论】:

          【解决方案5】:
          select model from (
          Select model, price from pc
          where price = (select max(price) from pc)
          union
          Select model, price from laptop
          where price = (select max(price) from laptop)
          
          union
          Select model, price from printer
          where price = (select max(price) from printer)
          ) as G
          where price = (
          select max(price) from (
          Select model, price from pc
          where price = (select max(price) from pc)
          union
          Select model, price from laptop
          where price = (select max(price) from laptop)
          
          union
          Select model, price from printer
          where price = (select max(price) from printer)
          ) as T
          )
          

          【讨论】:

            猜你喜欢
            • 2017-12-17
            • 1970-01-01
            • 1970-01-01
            • 2021-11-26
            • 2023-03-16
            • 2018-01-27
            • 2015-01-21
            • 2012-07-28
            • 1970-01-01
            相关资源
            最近更新 更多