[英]how can I join two queries in which one is derived from other?
Could somebody explain me whats wrong in this? 有人可以解释一下这是怎么回事吗? My query is like this...
我的查询是这样的...
select t1.year, t1.empid, t2.tcost
from (select year,empid,sum(cost) as total from orders group by year,empid) t1
inner join (select year, max(total) from t1 group by year) t2
on t1.year= t2.year
I am getting error message as below 我收到如下错误消息
ERROR at line 3:
ORA-00942: table or view does not exist
I know I can achieve this using WITH clause but I want to know how to use join in this case. 我知道我可以使用WITH子句来实现,但是我想知道在这种情况下如何使用join。
Thanks in advance 提前致谢
If in your query you use self-join, there is usually an equivalent query based on analytical functions. 如果在查询中使用自联接,通常会存在一个基于分析函数的等效查询。 The advantage is, in most cases, shorter execution time.
在大多数情况下,优点是执行时间更短。 Here you could use
max ... keep dense rank ...
: 在这里您可以使用
max ... keep dense rank ...
:
select year, empid, sum_cost
from (
select year, empid, sum(cost) sum_cost,
max(sum(cost)) keep (dense_rank last order by sum(cost))
over (partition by year) max_cost
from orders group by year, empid )
where sum_cost = max_cost
Sample data and output: 样本数据和输出:
create table orders (year number(4), empid number(5), cost number(10,2));
insert into orders values (2010, 1, 100);
insert into orders values (2010, 1, 115);
insert into orders values (2010, 1, 207);
insert into orders values (2010, 2, 104);
insert into orders values (2011, 1, 90);
insert into orders values (2011, 2, 15);
insert into orders values (2011, 2, 107);
insert into orders values (2011, 3, 100);
Output: 输出:
YEAR EMPID SUM_COST
---- ----- --------
2010 1 422
2011 2 122
Edit: I doubt you could eliminate with
clause if you want to do self-join here. 编辑:如果您要在此处进行自我联接
with
我怀疑您可以消除with
子句。 with
is used especially when complex sub-queries are used twice or more. with
当复杂的子查询用于两次或更多次是特别使用。 And if you insist on join
instead of where (year,tcost) in ...
, as you suggested in of the comments, please use: 如果您坚持
join
而不是where (year,tcost) in ...
的where (year,tcost) in ...
,如您在注释中的建议,请使用:
with vvn as (select year, empid, sum(cost) as sc from orders group by year, empid)
select v1.year, v1.empid, v1.sc from vvn v1
join (select year, max(sc) msc from vvn group by year) v2
on v1.year = v2.year and v1.sc = v2.msc;
BTW, shortened version of my first answer does not really need keep dense rank
part, simpler is: 顺便说一句,我的第一个答案的缩短版本并不需要
keep dense rank
,更简单的是:
select year, empid, sum_cost
from (select year, empid, sum(cost) sum_cost,
max(sum(cost)) over (partition by year) max_cost
from orders group by year, empid )
where sum_cost = max_cost
Version with somehwat modified keep...
is still valid and interesting, but you probably noticed this. 进行了某些修改的
keep...
版本仍然有效且有趣,但是您可能已经注意到了这一点。
SELECT t1.year, t1.empid, t2.tcost
FROM (SELECT year, empid, sum(cost) AS total
FROM orders
GROUP BY year, empid) t1
INNER JOIN (SELECT year, max(total) **AS tcost**
FROM t1 **<-- ?? No, you need to specify a table**
GROUP BY year) t2
ON t1.year = t2.year
You have a comma between t1 and INNER, and syntax is wrong in FROM T1, you cannot JOIN an inner table to another inner. 您在t1和INNER之间有一个逗号,并且FROM FROM中的语法是错误的,您不能将一个内部表联接到另一个内部表。 Also max(total) needs to be aliased.
同样,max(total)需要别名。 All shown above.
全部显示在上方。
Try this 尝试这个
select t.year, t.empid, t.total
from (select t1.year,t1.empid,sum(t1.cost) as total from orders as t1
inner join orders as t2 on t1.year= t2.year group by t1.year,t1.empid) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.