客户表列:
客户ID公司名称联系人姓名联系人姓名地址城市地区邮政编码国家电话传真
供应商表格列:
SupplierID公司名称联系人姓名联系人姓名地址城市地区邮政编码国家电话传真主页
从这些表中我们如何找到"哪个国家/地区拥有最多的供应商和客户......?" 我需要查询上述问题....? 请回答我任何一个......!
答案 0 :(得分: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
):
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
答案 1 :(得分: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
答案 2 :(得分: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