-3

顧客表の列:

CustomerID CompanyName ContactName ContactTitle 住所 City Region PostalCode Country Phone Fax

仕入先表の列:

サプライヤーID 会社名 連絡先名 連絡先 住所 都市 地域 郵便番号 国 電話 ファックス ホームページ

これらの表から、「サプライヤーと顧客の数が最も多い国はどこですか...?」上記の問題のクエリが必要です....? Plsは私に答えてください....!

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

編集: または、2 つのテーブルを別々にグループ化し、それらを完全外部結合して、一致する用語を追加することもできます ( null2 つのリストの 1 つだけにある国の を処理することを忘れないでください)。

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 に答える