【问题标题】:SQL server query to get this output获取此输出的 SQL 服务器查询
【发布时间】:2012-07-26 09:12:08
【问题描述】:

我有一个无法获得输出的查询。 下面是要执行的查询

问:2. 编写查询以查找具有最大关联车辆数的城市名称、ID 和计数。

现在,Given 是应该在其上执行查询的一张表。

Vehicle_Detail_ID   City_ID
56                    242
57                    242
58                    242
59                    243
60                    241
61                    242
62                    245

另一个具有城市名称的表

City_ID City_Name
242          Bangalore
243          ChamarajNager
241          Bellary
245          Chitradurga

预期输出:

City_ID No_Vehicles
242          4

请告诉我如何编写获取正确输出的查询。

下面给出的是给出城市 ID 及其车辆数量的查询。 尝试:

SELECT c.city_id, COUNT(c.City_ID) AS NO_vehicles
  FROM city c, vehicle_details v 
 WHERE c.City_ID = v.City_ID
 GROUP BY c.City_ID

实际输出

City_ID No_Vehicles
242             4
243             1
241             1
245             1

请帮助我获得最大的这些,如预期输出所示。

【问题讨论】:

  • 什么数据库?这是作业吗?
  • 哪个 RDBMS?您会查看 SELECT TOP 1 ... ORDER BY COUNT(...) DESCLIMIT 1 或其变体。
  • 你已经完成了一半。现在添加一个条件来将结果限制为最上面的结果。
  • 您还没有回答您使用哪个数据库。 Sql Server、MySql、Oracle、Informix、db2、...

标签: sql sql-server database sql-server-2008-r2


【解决方案1】:

// 解决方案一

Select top 1 v.cityID,count(*) as c 
from tblVehicle as v 
group by cityID
order by c desc

// 使用 cityName 的解决方案 2

Select top 1 a.cityID,a.cityName,Count(*) as c 
from tblCity as a
inner join tblVehicle as b
    on a.cityID=b.cityID
group by a.cityID,a.cityName
order by c desc

【讨论】:

    【解决方案2】:
    CREATE TABLE V_ddetails(Vehicle_Detail_ID int,City_ID int)
    INSERT INTO V_ddetails 
    VALUES(56,242),(57,242),(58,242),(59,243),(60, 241),(61,242),(62,245)
    
    CREATE TABLE city(City_ID int, City_Name varchar(50))
    
    INSERT INTO city
    VALUES(242,'Bangalore'),(243,'ChamarajNager'),(241,'Bellary'),(245,'Chitradurga')
    
    select top 1 v.City_ID,COUNT(*) as No_Vehicles from V_ddetails v inner join city c
    on v.City_ID =c.City_ID 
    group by v.City_ID 
    order by No_Vehicles desc
    

    【讨论】:

    • @missReclusive 请注意,这不包括 2 个城市各有 4 辆车的情况。编译器将在没有特定逻辑的情况下显示哪个先出现
    【解决方案3】:

    我认为使用分区函数和 CTE 你可以做到这一点ON FIDDLE

    CREATE TABLE V_details(Vehicle_Detail_ID int,City_ID int) 
    INSERT INTO V_details  
    VALUES
    (56,242),
    (57,242),
    (58,242),
    (59,243),
    (60, 241),
    (60, 241),
    (60, 241),
    (60, 241),
    (61,242),
    (62,245)  
    
    CREATE TABLE city(City_ID int, City_Name varchar(50))  
    INSERT INTO city 
    VALUES
    (242,'Bangalore'),
    (243,'ChamarajNager'),
    (241,'Bellary'),
    (245,'Chitradurga') 
    
    
    
    
       ;with initdata_cte as  
    (     
      SELECT              
      c.city_id             
      , COUNT(c.City_ID) AS NO_vehicles                
      , RANK() over (order by c.city_id) rnk     
      FROM city c, V_details v        
      WHERE c.City_ID = v.City_ID       
      GROUP BY c.City_ID  
    ) 
    , rnk_cte as 
    (     
      SELECT              
      city_id             
      , NO_vehicles                
      , RANK() over (order by NO_vehicles desc) rnk     
      FROM initdata_cte     
    ) 
    select * from rnk_cte where rnk =1 
    

    【讨论】:

      【解决方案4】:

      试试

      select TOP 1 c.city_id,COUNT(c.City_ID) as NO_vehicles 
      from city c,vehicle_details v 
      where c.City_ID=v.City_ID 
      group by c.City_ID.
      

      【讨论】:

      【解决方案5】:
      CREATE TABLE V_ddetails(Vehicle_Detail_ID int,City_ID int)
      INSERT INTO V_ddetails 
      VALUES(56,242),(57,242),(58,242),(59,243),(60, 241),(61,242),(62,245)
      
      select City_ID,COUNT(*) as No_Vehicles  from V_ddetails 
      where city_id=242
      group by City_ID 
      

      【讨论】:

      • 这个想法是找到车辆数量最多的 city_id。如果您知道该问题的答案,则无需首先编写查询。
      • 好的,试试这里发布的最新查询。
      • 你不能那样做阿南德——这有点作弊!
      • @whytheq -- 有什么问题,我做了什么?
      • @AnandPhadke 您已将其放入答案where city_id=242 ...提问者如何在一般脚本中使用它?
      猜你喜欢
      • 1970-01-01
      • 2021-07-24
      • 2011-11-28
      • 2020-08-21
      • 2019-03-04
      • 2015-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多