【问题标题】:select max value from a table looking for description in another table从表中选择最大值在另一个表中查找描述
【发布时间】:2013-06-05 03:32:37
【问题描述】:


我有 3 张桌子

买家

buyer_id | name
50       |Joe
60       |Astor
70       |Cloe

物品

item_id | description
1       | iphone
2       | ipod
3       | imac

Item_Sold

buyer_id | item_id
50       | 1
50       | 2
60       | 1
60       | 3
70       | 1
70       | 2
70       | 3

我想找出畅销商品的描述,在这种情况下:

Best-Selling
iphone

【问题讨论】:

  • 查看我编辑的答案...之前忘记了 FROM 子句

标签: sql sqlite count nested max


【解决方案1】:
SELECT description AS Best_Selling
FROM item
WHERE item_id = (SELECT item_id FROM( SELECT item_id ,COUNT(*) as num_items
                                      FROM Item_Sold
                                      GROUP BY item_id
                                      ORDER BY num_items DESC
                                      LIMIT 1
                                     )z
                 )

SQL FIDDLE

这个答案并不完全正确。如果两件商品的销售额相同,则只会退回其中一件。

【讨论】:

  • 您可以将select item_id 替换为select item_id, count(*) ... 的连接:select i.description from item i join (select ...
  • 如果两件或多件商品的销售量相等会发生什么...我的意思是如果商品 ID 1 和 2 都有 3 个买家...它只会给出第一个商品描述....
  • @mhasan ,感谢您帮助编辑和回答我的问题。
【解决方案2】:

此查询将给出销售额最大的所有商品 ID 描述,即当两个或多个商品 ID 的销售额相等时......

;WITH CTE1(Id,Counts) as
(
SelectItem_Id,COUNT(buyer_id ) AS C  FROM T GROUP BY ID
) 

Select Item.Description from CTE1 A inner join 
(Select MAX(Counts) AS MaxCount FROM CTE1 ) b on a.Counts=b.MaxCount 
inner join
Item on Item.Item_Id=a.Item_Id

如果公用表表达式不起作用,您可以尝试这样......

Select Item.Description from (Select Item_Id,COUNT(buyer_id ) AS Counts  FROM item_sold GROUP BY Item_Id) A inner join 
(Select MAX(Counts) AS MaxCount FROM 
(
Select Item_Id,COUNT(buyer_id) AS Counts  
FROM item_sold GROUP BY Item_Id) v 
) b 
on a.Counts=b.MaxCount 
inner join
Item on Item.Item_Id=a.Item_Id

SQL Fiddle Demo 这是我正在谈论的 Fiddle 的链接......它给出了所有销售量最大的描述...... Case Sql Fiddle Demo

【讨论】:

  • 这就是我要说的!非常感谢
【解决方案3】:
select description as "Best-Selling" 
from (select a.item_id, b.description, count(*) count 
      from Item_Sold a,Items b
      where a.item_id = b.item_id
      group by a.item_id ) temp
where count = (select max(count) 
               from (select a.item_id, count(*) count 
                      from Item_Sold a,Items b
                      where a.item_id = b.item_id
                      group by a.item_id ) temp1)

【讨论】:

    【解决方案4】:

    pl-sql:

    select description as "Best-Selling"
    from item
    where item_id in (
        select item_id from (
            select item_id, count(item_id) as item_count 
            from item_sold 
            group by item_id) 
        where item_count = (
            select max(item_count) from (
                select item_id, count(item_id) as item_count 
                from item_sold 
                group by item_id)
            )
    )
    

    【讨论】:

      猜你喜欢
      • 2017-11-13
      • 1970-01-01
      • 2016-05-16
      • 2023-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      相关资源
      最近更新 更多