あなたはcount(*) over(
partition by a.Id)
を使用して、各id
のカウントを取得し、そのことで数を分割することができます。
テストのセットアップ:rextester:http://rextester.com/JQK48793
create table a (id int, name char(3), number decimal(9,2))
insert into a
values (1,'ABC',100.0)
create table b (id int, name char(3), school char(1))
insert into b values
(1,'ABC','A')
,(1,'ABC','B')
,(1,'ABC','C')
クエリ:
select
a.Id
, a.Name
, b.School
, Number = (a.Number+.0)/count(*) over (partition by a.Id)
from a
inner join b
on a.Id = b.Id
結果:
+----+------+--------+------------------+
| Id | Name | School | Number |
+----+------+--------+------------------+
| 1 | ABC | A | 33,3333333333333 |
| 1 | ABC | B | 33,3333333333333 |
| 1 | ABC | C | 33,3333333333333 |
+----+------+--------+------------------+
あなたは、単一の列に複数の値を保存している場合は、** STOP !**これは、追加のテーブルが必要なサインです。これらの各値は、元のキーの横に独自のレコードがあります。 –
ありがとうございます。追加のテーブルを作成せずにそれを実行できる方法はありますか? – Pranav