0

I am having some trouble with a query to select the right data from my table. I am trying to find a row of data among dates where the enddate is not null, proving harder than it sounds.

My query is this:

SELECT ms1.* FROM STATUS MS1
INNER JOIN
(
    SELECT MS.EndDate, MS.MemberID, MS.MemberStatusID, 
        MS.StartDate , M.IdentificationNumber 
    FROM MEMBER M
        INNER JOIN STATUS MS ON MS.MemberID = M.MemberID 
        AND MS.StartDate =(SELECT MAX(StartDate) FROM STATUS WHERE STATUS.MemberID = M.MemberID)
        AND MS.StartDate < MS.EndDate
    WHERE MS.EndDate IS NOT NULL
    GROUP BY MS.MemberID, MS.MemberStatusID, 
        MS.EndDate, MS.StartDate, M.IdentificationNumber
) MS2
ON MS1.MemberID = ms2.MemberID 
AND MS1.MemberStatusID = MS2.MemberStatusID
ORDER BY MS1.memberid

With this query I have successfully eliminated a couple of scenarios but I still can't get the last one to be excluded. The problem can be shown in the following data. In this small sub-set of my table, MemberID = 3 is correct (notice the start dates and end dates in relation to the StatusID). MemberID 452311 is not correct as the start and end dates are in the wrong order as compared to the StatusID. Ultimately I am trying to create a query that will only find me the type of record as memberid = 453291. I can't find a way to eliminate the 452311 record

MemberID    MemberStatusID  StartDate                   EndDate
3               2           2011-07-18 15:49:30.000     2013-04-09 10:08:51.877
3               226157      2013-04-09 10:08:51.877     NULL
453291          165079      2012-10-22 11:02:20.843     2012-12-18 09:25:26.150
453291          175401      2012-12-18 09:25:21.150     2013-01-28 12:30:49.460
453291          194582      2013-01-28 12:30:44.460     2013-03-12 09:02:53.723
453291          210649      2013-03-12 09:02:49.723     2013-04-01 15:41:10.783
453291          253471      2013-04-01 15:41:08.783     2013-05-01 15:25:30.330
453291          261919      2013-04-01 11:03:51.733     2013-04-01 11:03:52.733
452311          165101      2012-10-22 11:02:20.843     NULL
452311          194643      2013-01-29 08:36:15.560     2013-01-29 11:45:47.333
452311          194813      2013-01-29 11:45:46.333     2013-05-01 15:25:30.330
4

2 回答 2

0

是否要添加(在 ORDER BY 之前):

WHERE NOT (MS1.EndDate IS NULL AND EXISTS 
            (SELECT 1 
             FROM Status MS3
             WHERE MS3.MemberID = MS1.MemeberID 
             AND MS3.MemberStatusID = MS1.MemberStatusID
             AND MS3.StartDate > MS1.StartDate
            )
          )
于 2013-10-08T22:49:28.753 回答
0

尝试

SELECT ms1.* FROM STATUS MS1
INNER JOIN
(
    SELECT MS.EndDate, MS.MemberID, MS.MemberStatusID, 
        MS.StartDate , M.IdentificationNumber 
    FROM MEMBER M
        INNER JOIN STATUS MS ON MS.MemberID = M.MemberID 
        AND MS.StartDate =(SELECT MAX(StartDate) FROM STATUS WHERE STATUS.MemberID = M.MemberID)
        AND MS.StartDate < MS.EndDate
    WHERE MS.EndDate IS NOT NULL
    GROUP BY MS.MemberID, MS.MemberStatusID, 
        MS.EndDate, MS.StartDate, M.IdentificationNumber
) MS2
ON MS1.MemberID = ms2.MemberID 
AND MS1.MemberStatusID = MS2.MemberStatusID
AND MS2.EndDate IS NOT NULL
ORDER BY MS1.memberid
于 2013-10-08T22:40:00.103 回答