1

我有以下

user_id job_id job_offer_date 
------- ------ --------------
1          123     2013-05-10 
1          124     2013-07-19
2          127     2013-05-10
3          128     2013-06-15 

我想在这里编写两个单独的查询以在报告中使用:

QUERY #1(我已经有这个工作了)

此查询要返回 FIRST 工作邀请日期为 2013 年 5 月 10 日的所有用户。这是一个简单的查询,因为如果用户在该日期之前有任何工作,它将返回他。在这种情况下,我将看到用户 #1、#2。

此查询如下所示:

SELECT DISTINCT j.* FROM job WHERE j.job_offer_date <= '2013-05-10'

QUERY #2(这是我真正的问题)

如何返回第一个工作邀请日期在 2013 年 5 月 10 日之后和 2013 年 7 月 19 日之前的用户。在这种情况下,因为用户 #1 在 2013 年 5 月 10 日之前收到了他的第一个报价,所以他不应该被包括在结果中。此结果集应仅包括用户 #3。

这里最重要的关键是因为用户 #1 在 2013 年 5 月 10 日之前收到了他的第一个报价,他应该被排除在查询 #2 的结果集中。

4

5 回答 5

1

一组相关的第一份工作机会。

select user_id, min(job_offer_date) as first_offer
from job
group by user_id
having min(job_offer_date) > '2013-05-10'
   and min(job_offer_date) < '2013-07-19'

加入该集合以获取用户。我猜想用户数据存储在用户表中。

select u.*
from users u
inner join (select user_id, min(job_offer_date) as first_offer
            from job
            group by user_id
            having min(job_offer_date) > '2013-05-10'
               and min(job_offer_date) < '2013-07-19') o
        on o.user_id = u.user_id;
于 2013-06-29T20:00:19.013 回答
0

If I remember my SQL date stuff correctly, wouldn't it look something like this:

SELECT DISTINCT j.* FROM job WHERE j.job_offer_date > '2013-05-10' AND j.job_offer_data < '2013-7-19'

EDIT: I misunderstood your question. The above would look for any user with a job between (but not on) the two dates listed. Oleh Nechytailo has a better answer.

于 2013-06-29T19:36:40.250 回答
0

它肯定会奏效

WITH TEMP 
AS
(
SELECT *
FROM Job AS J
)

SELECT *
FROM TEMP AS A
WHERE OFFERdATE > '2013-05-10' AND oFFERdATE < '2013-7-19' AND
1 = CASE WHEN (SELECT COUNT(*) FROM TEMP WHERE OFFERdATE <= '2013-05-10' AND USERID = A.USERID) > 0 THEN      0 ELSE 1 END
于 2013-06-29T19:57:46.137 回答
0
--------------
-- sample data
--------------
declare @data table
(
    user_id int,
    job_id int,
    job_offer_date datetime
)

insert into @data (user_id, job_id, job_offer_date) values 
    (1, 123, '2013-05-10'),
    (1, 124, '2013-07-19'),
    (2, 127, '2013-05-10'),
    (3, 128, '2013-06-15')

-----------
-- solution
-----------
select user_id from @data
group by user_id
having min(job_offer_date) > '2013-05-10' and min(job_offer_date) < '2013-07-19'
于 2013-06-29T20:00:54.923 回答
0
select user_id, job_id, job_offer_date
from 
    (select user_id
        , job_id
        , job_offer_date
        , row_number() over 
                (partition by user_id order by job_offer_date) rown 
     from job) sub
where sub.rown = 1 and sub.job_offer_date > '2013-05-10' and sub.job_offer_date < '2013-07-19'
于 2013-06-29T19:41:23.027 回答