我有2张桌子
table1(水手):
id_sailor name
1 Barondeau
2 Vighetti
table2(voyages):
id_ voyage boat id_sailor
1 Juliette 1
2 Juliette 1
3 La belle 2
4 La Belle 1
如何制作这张新表:
n是特定船上水手的航程数量 -
boat name n
Juliette Barondeau 2
La Belle Barondeau 1
La Belle Vighetti 1
我尝试了什么:
select "table2"."boat", "table1"."name", count("table2"."boat" ) as "n"
from "table1", "table2"
where "table1"."id_sailor" = "table2"."id_sailor"
group by "table2"."name"
;
在hsqldb 1.8中,我有这个错误“不在聚合函数或group by子句中:1b6128f ...”
答案 0 :(得分:1)
您需要在group by "table2"."boat"
条款中添加GROUP BY
,看起来还不错。
group by "table2"."boat","table2"."name"
而不是
group by "table2"."name"
答案 1 :(得分:1)
似乎是简单的“分组依据”查询
select
v.boat, s.name, count(*) n
from voyages v
innner join sailors s on v.id_sailor = s.id_sailor
group by
v.boat, s.name
这里要注意的重点是,所有选定的列不使用聚合函数 {例如COUNT()} *应该在group by子句中列出。
答案 2 :(得分:1)
只需要按table2.boat添加分组。
select table2.boat, table1.name, count(table2.boat) as n
from table1, table2
where table1.id_sailor = table2.id_sailor
group by table1.name , table2.boat;