包含两个SELECTS的UNION的查询

时间:2013-11-13 19:02:49

标签: oracle11g

包含两个包含列的SELION的UNION的查询 LOCATION_ID,STREET_ADDRESS,CITY,COUNTRY_NAME和“No of Depts”。结果必须是ALL列表 具有该位置的部门数量的位置。列表必须从最高到最低排序 部门数量。

SELECT locations.location_id, 
       locations.street_address, 
       locations.city, 
       locations.country_id 
FROM locations,departments
WHERE (locations.location_id = departments.location_id)
GROUP BY locations.location_id, 
         locations.street_address, 
         locations.city, 
         locations.country_id
UNION ALL
SELECT Count(departments.department_name) 
FROM departments
WHERE (locations.location_id = departments.location_id)
GROUP BY departments.department_id, departments.location_id
ORDER BY (departments.department_name) DESC;`

1 个答案:

答案 0 :(得分:1)

为什么这不起作用?

  SELECT count() as c, 
         departments.department_name, 
         locations.location_id, 
         locations.street_address, 
         locations.city, 
         locations.country_id 
  FROM locations
  join departments ON locations.location_id = departments.location_id
  GROUP BY departments.department_name, locations.location_id, locations.street_address, locations.city, locations.country_id