如何获取具有当前行ID的子查询?

时间:2018-10-26 08:03:58

标签: sql oracle

Hi StackOverflow社区,

我是SQL的新手,我有一个问题,就是我不知道如何解决(顺便说一句,我是SQL的新手)。

所以我有这些表:

TABLE 1 - Users
-ID     -NAME
1       User1       
2       User2       
3       User3       



TABLE 2 - JOBS
-ID     -JOB                    -USER       -ID_DIRECCTION
1       Football Match          User1       1                  
2       Bascketball Match       User3       1                   
3       Swimming Race           User2       2                   
4       Handball Game           User3       2                   
5       Tennis Match            User2       3                   



TABLE 3 - DIRECCTIONS
-ID     -DIRECCTION         -NUMBER
1       Fail Street         34
2       Swimming Street     45
3       Fake Street         01

我想做的是显示作业表,但添加一个带有SELECT的新列,该列显示已被定向的最后一个用户(如果没有人被定向,则结果将为null),如果最近有多个用户。

我已经接近用当前查询获取它了(我认为),但是主要问题是当我执行子查询时,有一种方法可以用执行主查询的当前行的ID来获取内部查询。 ??

SELECT j.ID, j.JOB, j.USER, j.ID_DIRECCTION,
(SELECT j.USER
FROM USERS u INNER JOIN
     JOBS j
     ON j.USER = u.ID INNER JOIN
     DIRECCTIONS d
     ON d.ID = j.ID_DIRECCTION
WHERE d.ID = (SELECT d.ID FROM DIRECCTIONS WHERE d.ID = j.ID_DIRECCTION --I want this to be current ID_DIRECCTION row)  
ORDER BY j.ID DESC FETCH FIRST 1 ROWS ONLY
) AS LAST_USER
FROM JOBS j;

如何选择当前行的ID来获取子查询中的其他内容?

结果应为:

-ID     -JOB                    -USER       -ID_DIRECCTION      -LAS_TUSER
1       Football Match          User1       1                   null*
2       Bascketball Match       User3       1                   User1*
3       Swimming Race           User2       2                   null*
4       Handball Game           User3       2                   User2*
5       Tennis Match            User2       3                   User3*

但是我总是在LAST_USER中得到相同的值

2 个答案:

答案 0 :(得分:1)

似乎您想要LAG,但不清楚为什么LAS_TUSER = User3参加网球比赛(工作ID 5)

SELECT j.*, 
       LAG(u.name) 
         over ( 
           PARTITION BY j.id_direcction 
           ORDER BY j.id ) 
FROM   users u 
       inner join jobs j 
               ON j.username = u.name 
ORDER  BY j.id 

Demo

答案 1 :(得分:0)

不清楚您的问题是在作业表中确实包含用户名,还是仅是ID。

如果确实有名字,那么您将在Jobs表和you can just use lag()中拥有所有需要的信息。

但是,如果您具有ID,那么就需要针对当前ID和滞后ID都加入到用户表。您仍然可以通过相同的方式获取先前的ID:

select id, job, id_user, id_direcction,
  lag(id_user) over (partition by id_direcction order by id) as last_id_user
from jobs

,然后将其用作CTE或内联视图,并从中加入:

select j.id, j.job, u.name, j.id_direcction, lu.name as last_name
from (
  select id, job, id_user, id_direcction,
    lag(id_user) over (partition by id_direcction order by id) as last_id_user
  from jobs
) j
join users u on u.id = j.id_user
left join users lu on lu.id = j.last_id_user

借助CTE提供样本数据,并对这些数据的真实含义进行假设:

-- sample data
with users (id, name) as (
            select 1, 'User1' from dual
  union all select 2, 'User2' from dual
  union all select 3, 'User3' from dual
),
jobs (id, job, id_user, id_direcction) as (
            select 1, 'Football Match', 1, 1 from dual
  union all select 2, 'Bascketball Match', 3, 1 from dual
  union all select 3, 'Swimming Race', 2, 2 from dual
  union all select 4, 'Handball Game', 3, 2 from dual
  union all select 5, 'Tennis Match', 2, 3 from dual
)
-- actual query
select j.id, j.job, u.name, j.id_direcction, lu.name as last_name
from (
  select id, job, id_user, id_direcction,
    lag(id_user) over (partition by id_direcction order by id) as last_id_user
  from jobs
) j
join users u on u.id = j.id_user
left join users lu on lu.id = j.last_id_user
order by j.id;

        ID JOB               NAME  ID_DIRECCTION LAST_
---------- ----------------- ----- ------------- -----
         1 Football Match    User1             1      
         2 Bascketball Match User3             1 User1
         3 Swimming Race     User2             2      
         4 Handball Game     User3             2 User2
         5 Tennis Match      User2             3      

除非您要获取其描述,否则无需引用direcctions表。我已经提供了一个in this db<>fiddle版本。

就像@KaushikNayak一样,根据您提供的数据,我看不出您希望如何或为什么在最后一行看到User3