分组后消除某些重复的行
Eliminate certain duplicated rows after group by
有了这个数据库:
Chef(cid,cname,age),
Recipe(rid,rname),
Cooked(orderid,cid,rid,price)
Customers(cuid,orderid,time,daytime,age)
[cid表示chef id,依此类推]
根据客户的订单,我需要为每位厨师找出他的年龄与订购 his/her 餐点的人的平均年龄之间的差异。
我写了以下查询:
select cid, Ch.age - AVG(Cu.age) as Diff
from Chef Ch NATURAL JOIN Cooked Co,Customers Cu
where Co.orderid = Cu.orderid
group by cid
这解决了问题,但是如果你假设顾客有他们的唯一id,它可能不起作用,因为这样一个人可以点两顿来自同一个厨师的饭菜,影响计算。
现在我知道它可以用 NOT EXISTS 来回答,但我正在寻找一个包含按功能分组的灵魂(类似于我写的东西)。到目前为止我找不到(我搜索并尝试了很多方法,从 select distinct 到 where 子句中的操作,再到 "having count(distinct..)" )
编辑:人们要求举个例子。我正在使用 SQLFiddle 进行编码,但它经常崩溃,所以我会尽力而为:
cid | cuid | orderid | Cu.age
-----------------------------
1 333 1 20
1 200 2 41
1 200 5 41
2 4 3 36
假设厨师 1 的年龄是 50 岁。我的查询将为您提供 50 - (20+40+40/3) = 16 和 2/3。虽然它实际上应该是 50 - (20+40/2) = 20。(因为 ID 为 200 的人订购了我们心爱的厨师 1 的两份食谱)。
假设厨师 2 的年龄是 47。我的查询结果将是:
cid | Diff
----------
1 16.667
2 11
另一个编辑:我没有被教导任何特定的 sql-查询 form.So 我真的不知道 Oracle 到 MySql 到 Microsoft Server 之间有什么区别,所以我基本上是 "freestyle" 查询。(我希望它在我的考试中也会很好 :O )
首先,您应该将查询写成:
select cid, Ch.age - AVG(Cu.age) as Diff
from Chef Ch join
Cooked Co
on ch.cid = co.cid join
Customers Cu
on Co.orderid = Cu.orderid
group by cid;
两个不同的原因:
NATURAL JOIN
只是一个等待发生的错误。列出要用于连接的列,以免意外字段或拼写差异影响结果。
- 切勿在
FROM
子句中使用逗号。始终使用明确的 JOIN
语法。
接下来,你的问题的答案比较复杂。对于每个厨师,我们可以通过以下方式获得顾客的平均年龄:
select cid, avg(age)
from (select distinct co.cid, cu.cuid, cu.age
from Cooked Co join
Customers Cu
on Co.orderid = Cu.orderid
) c
group by cid;
然后,为了区别,您还需要将这些信息也带进来。一种方法在子查询中:
select cid, ( age - avg(cuage) ) as diff
from (select distinct co.cid, cu.cuid, cu.age as cuage, c.age as cage
from Chef c join
Cooked Co
on ch.cid = co.cid join
Customers Cu
on Co.orderid = Cu.orderid
) c
group by cid, cage;
有了这个数据库:
Chef(cid,cname,age),
Recipe(rid,rname),
Cooked(orderid,cid,rid,price)
Customers(cuid,orderid,time,daytime,age)
[cid表示chef id,依此类推]
根据客户的订单,我需要为每位厨师找出他的年龄与订购 his/her 餐点的人的平均年龄之间的差异。 我写了以下查询:
select cid, Ch.age - AVG(Cu.age) as Diff
from Chef Ch NATURAL JOIN Cooked Co,Customers Cu
where Co.orderid = Cu.orderid
group by cid
这解决了问题,但是如果你假设顾客有他们的唯一id,它可能不起作用,因为这样一个人可以点两顿来自同一个厨师的饭菜,影响计算。
现在我知道它可以用 NOT EXISTS 来回答,但我正在寻找一个包含按功能分组的灵魂(类似于我写的东西)。到目前为止我找不到(我搜索并尝试了很多方法,从 select distinct 到 where 子句中的操作,再到 "having count(distinct..)" )
编辑:人们要求举个例子。我正在使用 SQLFiddle 进行编码,但它经常崩溃,所以我会尽力而为:
cid | cuid | orderid | Cu.age
-----------------------------
1 333 1 20
1 200 2 41
1 200 5 41
2 4 3 36
假设厨师 1 的年龄是 50 岁。我的查询将为您提供 50 - (20+40+40/3) = 16 和 2/3。虽然它实际上应该是 50 - (20+40/2) = 20。(因为 ID 为 200 的人订购了我们心爱的厨师 1 的两份食谱)。 假设厨师 2 的年龄是 47。我的查询结果将是:
cid | Diff
----------
1 16.667
2 11
另一个编辑:我没有被教导任何特定的 sql-查询 form.So 我真的不知道 Oracle 到 MySql 到 Microsoft Server 之间有什么区别,所以我基本上是 "freestyle" 查询。(我希望它在我的考试中也会很好 :O )
首先,您应该将查询写成:
select cid, Ch.age - AVG(Cu.age) as Diff
from Chef Ch join
Cooked Co
on ch.cid = co.cid join
Customers Cu
on Co.orderid = Cu.orderid
group by cid;
两个不同的原因:
NATURAL JOIN
只是一个等待发生的错误。列出要用于连接的列,以免意外字段或拼写差异影响结果。- 切勿在
FROM
子句中使用逗号。始终使用明确的JOIN
语法。
接下来,你的问题的答案比较复杂。对于每个厨师,我们可以通过以下方式获得顾客的平均年龄:
select cid, avg(age)
from (select distinct co.cid, cu.cuid, cu.age
from Cooked Co join
Customers Cu
on Co.orderid = Cu.orderid
) c
group by cid;
然后,为了区别,您还需要将这些信息也带进来。一种方法在子查询中:
select cid, ( age - avg(cuage) ) as diff
from (select distinct co.cid, cu.cuid, cu.age as cuage, c.age as cage
from Chef c join
Cooked Co
on ch.cid = co.cid join
Customers Cu
on Co.orderid = Cu.orderid
) c
group by cid, cage;