-3

客户表列:

客户 ID 公司名称 联系人姓名 联系人职位 地址 城市 地区 邮政编码 国家 电话 传真

供应商表列:

供应商 ID 公司名称 联系人姓名 联系人职位 地址 城市 地区 邮政编码 国家 电话 传真 主页

从这些表中我们如何找到“哪个国家的供应商和客户数量最多......?” 我需要查询上述问题....?请回答我任何人......!

4

3 回答 3

2

要获得单个国家/地区作为单个值包含在两个表中的最大次数,请将它们合并,然后将它们分组:

 select top 1 x.Country
 from (
   select Country from Customer
   union all
   select Country from Supplier) x
 group by x.Country
 order by count(1) desc

编辑:或者,您可以将两个表分别分组,然后将它们完全外部连接在一起,并添加匹配的术语(请记住处理null来自仅位于两个列表之一中的国家的 s ):

select top 1 ISNULL(x.Country, y.Country) as [Country]
from (
  select Country, COUNT(1) as [Count] from Customers
  group by Country) x
full outer join (
  select Country, COUNT(1) as [Count] from Suppliers
  group by Country) y
on x.Country = y.Country
order by ISNULL(x.[Count], 0) + ISNULL(y.[Count], 0) desc
于 2012-07-17T07:37:09.107 回答
0

SQL 服务器语法

最大客户数

select Country from (
(with cte as (select Country,COUNT(*) cnt
from Customer
group by Country)
select Country,Rank() over (order by cnt desc) row_num from cte)a
where a.row_num=1

最大供应商数量

select Country from (
(with cte as (select Country,COUNT(*) cnt
from suppliers
group by Country)
select Country,Rank() over (order by cnt desc) row_num from cte)a
where a.row_num=1
于 2012-07-17T07:24:36.713 回答
0
SELECT a.country AS MaxCustomers, b.country AS MaxSuppliers
FROM
(
    SELECT TOP 1 country
    FROM customers
    GROUP BY country
    ORDER BY COUNT(*) DESC
) a
CROSS JOIN
(
    SELECT TOP 1 country
    FROM suppliers
    GROUP BY country
    ORDER BY COUNT(*) DESC
) b

应该输出如下内容:

MaxCustomers    |    MaxSuppliers
---------------------------------
USA             |    Japan
于 2012-07-17T07:26:21.607 回答