2014-09-26 4 views
1

Mysql 호출에 어려움을 겪고 있으며 전문 지식을 빌리 길 희망합니다.Mysql 다중 레코드가있는 고유 레코드

나는 내가 원하는 것은 두 개의 선택을 통해서만 가능할 수 있다고 믿는다. 아직 나는 이것들 중 하나를 수행하지 않았으며 이것에 대한 내 머리를 감싸기 위해 고심하고있다. 나는 단지 법안에 대한 하나 개의 레코드를 원하는 사용자 이름 열 즉 만의 고유 레코드를 선택하고자하는 내가 그들에게 을 제공, 가장 최근의 시작일과 하나가 필요

+------------------+----------------------+-------------------------------+ 
|  username |  acctstarttime |  acctstoptime   | 
+------------------+----------------------+-------------------------------+ 
|  bill  | 22.04.2014   |   23.04.2014   | 
+------------------+----------------------+-------------------------------+ 
|  steve  | 16.09.2014   |        | 
+------------------+----------------------+-------------------------------+ 
|  fred  | 12.08.2014   |        | 
+------------------+----------------------+-------------------------------+ 
|  bill  | 24.04.2014   |        | 
+------------------+----------------------+-------------------------------+ 

:

나는 테이블과 같이가 지난 3 개월 동안 없었던 (end_date는 여기 중요하지 않습니다.) 그렇지 않으면 데이터가 필요하지 않습니다. 요약하면 가장 최근 시작일이 3 개월 이상인 사람이 필요합니다. 그러나

SELECT DISTINCT(username), ra.acctstarttime AS 'Last IP', ra.acctstoptime 
FROM radacct AS ra 
WHERE ra.acctstarttime < DATE_SUB(now(), interval 3 month) 
GROUP BY ra.username 
ORDER BY ra.acctstarttime DESC 

이 단순히 3 개월 전을 통해 시작 날짜를 가지고 특정 고객에 대한 DATE_START에 대해 나에게 세부 사항을 제공합니다

내가 현재 사용하고있는 명령입니다. 나는 이것에 대한 몇 가지 다른 조합을 지치고 double select 명령을 시도했지만 현재 벽돌 벽을 때리고있다. 어떤 도움이나 옳은 방향으로의 추진은 많은 도움이 될 것입니다.

나는 다음과 같은 만든

업데이트 : 그것이 있어야로 쿼리가있을 때 http://sqlfiddle.com/#!2/f47b2/1

효과적으로 난 단지 1 개 행을 볼 수 있습니다. 이것은 법안의 행이 될 것입니다. 그는 지난 3 개월 이내에 시작 날짜가없는 유일한 사람입니다. 내가 볼 기대 결과는 다음과 같다 :이로

24 bill April, 11 2014 12:11:40+0000 (null) 

법안에 대한 최신 시작 날짜가 있지만이 시작 날짜는 최근 3 개월 이내에 없습니다. 바라기를 이것은 명확하게하는 것을 도울 것이다. 지금까지 많은 도움을 주셔서 감사합니다.

http://sqlfiddle.com/#!2/f47b2/14

은 또 다른 예이다. 계산서의 acctstartdate가 4 월 항목으로 표시되면 지난 3 개월 동안 where 절을 추가하면 원하는 결과를 얻을 수 있습니다.

+0

를 추가하는 나를 좋아하면 알려줘 : 당신의 좋은 설명을 바탕으로 편집

, 여기에 쿼리 SQLFiddle입니다 ie'WHERE ra.acctstarttime> DATE_SUB (now(), 3 개월 간격) ' – sqlab

+0

@sqlab 질문의 업데이트를 참조하십시오. 많은 감사합니다 –

답변

1

SQLFiddle http://sqlfiddle.com/#!2/444432/9 (MySQL의 5.5)

나는 현재 텍스트에 따라 2 가지 방법으로 질문을 찾고 있어요 :

I only want one record for bill and I need the one with most recent start_date, providing they were in the last three months (end_date is not important to me here) else I do not want any data

구조

create table test 
(
    username varchar(20), 
    date_start date 
); 

데이터

Username date_start 
--------- ----------- 
bill  2014-09-25 
bill  2014-09-22 
bill  2014-05-26 
andy  2014-05-26 
tim   2014-09-25 
tim   2014-05-26 

우리가

Username date_start 
--------- ----------- 
bill  2014-09-25 
tim   2014-09-25 

쿼리

select * 
from test a 
inner join 
(
    select username, max(date_start) as max_date_start 
    from test 
    where date_start > date_sub(now(), interval 3 month) 
    group by username 
) b 
on 
    a.username = b.username 
    and a.date_start = b.max_date_start 
where 
    date_start > date_sub(now(), interval 3 month) 

가장 최근 지난 3 개월에 대한 설명

,의 각 사용자에 대한 최대의 시작 날짜를 얻을 수 있도록 원하는 것은. 기록을 최신 3 개월로 제한하기 위해 where date_start > date_sub(now(), interval 3 month)을 사용하고 각 사용자의 최대 시작 날짜를 찾기 위해 group by username을 사용합니다.

우리는 사용자와 최대 날짜를 기준으로이 작은 하위 집합과 주 데이터를 결합하여 원하는 결과를 얻습니다.

우리는 최신 3개월보고 대신 각 사용자에 대한 가장 최근의 날짜를 찾을하고자하는 경우, 우리는 이런 종류의 데이터를보고 할 것이다 또 다른 각도 :

무엇 우리가 원하는

Username date_start 
--------- ----------- 
bill  2014-05-26 
tim   2014-05-26 
andy  2014-05-26 

쿼리

select * 
from test a 
inner join 
(
    select username, max(date_start) as max_date_start 
    from test 
    where date_start < date_sub(now(), interval 3 month) 
    group by username 
) b 
on 
    a.username = b.username 
    and a.date_start = b.max_date_start 
where 
    date_start < date_sub(now(), interval 3 month) 

이 쿼리를 원하는대로 변경할 수 있기를 바랍니다. http://sqlfiddle.com/#!2/f47b2/17

select * 
from activity a 

-- find max dates for users for records with dates after 3 months 
inner join 
(
    select username, max(acctstarttime) as max_date_start 
    from activity 
    where acctstarttime < date_sub(now(), interval 3 month) 
    group by username 
) b 
on 
    a.username = b.username 
    and a.acctstarttime = b.max_date_start 

-- find usernames who have data in the recent three months 
left join 
(
    select username, count(*) 
    from activity 
    where acctstarttime >= date_sub(now(), interval 3 month) 
    group by username 
) c 
on 
    a.username = c.username 

where 
    acctstarttime < date_sub(now(), interval 3 month) 
    -- choose users who DONT have data from recent 3 months 
    and c.username is null 

당신은 당신은 당신의 비교를 반대한다 설명을

+0

죄송합니다 유형 "ie"가 "was not"이도록 의도 되었기 때문에 나는 나의 질문을 바로 잡았습니다.내 질문에 업데이 트를 참조하십시오. –

+0

@ TheHumbleRat'select date_sub (현재 날짜, 간격 3 개월);''June, 26 2014'. 따라서 6 월 26 일 이후에 기록이있는 모든 사용자 이름이 표시됩니다. 청구서 (ID 24) 외에도 ID 13과 26도 표시되어야합니다. 그 맞습니까? – zedfoxus

+0

스티브와 프레드는 지난 3 개월 이내의 날짜를 가진 테이블에 다른 사용자로 빌만 표시하면 안됩니다. 프레드는 예를 들어이 날짜가 'July, 07 2014'이고 엔트리가 'July, 11 2014' 이후에 있습니다. 둘 다 지난 3 개월 동안 활동했습니다. 그러나 Bill은 지난 3 개월 이내에 시작 날짜가 없으므로 지난 3 개월 동안 활동하지 않은 사람들에게 꼭 알아야 할 것이 있습니다. –

0

이 시도 :

select t.* 
    from radacct t 
    join ( 
     select ra.username, max(ra.acctstarttime) as acctstarttime 
      from radacct as ra 
     WHERE ra.acctstarttime < DATE_SUB(now(), interval 3 month) 
     ) s on t.username = s.username and t.acctstarttime = s.acctstarttime 

SQLFiddle

+0

불행히도 이것은 내게 아무런 결과를주지 않습니다. 나는 지난 달 안에 시작 날짜가 없었던 기록이 하나 있는데, 나는 그 대신에 1 주일을 보여주고 기록이 보이지 않는다. –

+0

@ TheHumbleRat [이] (http://sqlfiddle.com/#!2/2e60d/3)에서 어떤 행이 필요합니까? – zaratustra

+0

내 업데이트 된 댓글을 참조하십시오. –

관련 문제