How to convert this complex query which uses CASE WHEN STATEMENT

Posts   
 
    
atif
User
Posts: 1
Joined: 03-Jun-2008
# Posted on: 03-Jun-2008 18:01:48   

Can anyone plz help me for converting this query in llblgen pro commands. i will be really very thankfull.

SELECT dbo.MemberSavedSearch.MemberSavedSearchId, dbo.Member.MemberId, dbo.Member.Email, dbo.MemberSavedSearch.PropertyNotificationFrequencyId FROM dbo.Member INNER JOIN dbo.MemberSavedSearch ON dbo.Member.MemberId = dbo.MemberSavedSearch.MemberId WHERE (dbo.MemberSavedSearch.EmailStop = 0) And '1'= CASE WHEN (dbo.MemberSavedSearch.PropertyNotificationFrequencyId = 1 OR dbo.MemberSavedSearch.PropertyNotificationFrequencyId = 4) AND ((DATEPART(dy,GetDate())) != DATEPART(dy, dbo.MemberSavedSearch.LastNotificationSent)) THEN '1'

WHEN (dbo.MemberSavedSearch.PropertyNotificationFrequencyId = 2 OR dbo.MemberSavedSearch.PropertyNotificationFrequencyId = 5) AND ((DATEPART(dw,GetDate()) = 6) AND (DATEPART(dy,GetDate()) != DATEPART(dy,dbo.MemberSavedSearch.LastNotificationSent))) THEN '1'

WHEN (dbo.MemberSavedSearch.PropertyNotificationFrequencyId = 3 OR dbo.MemberSavedSearch.PropertyNotificationFrequencyId = 6) AND (((DATEPART(mm,GetDate()) != DATEPART(mm,dbo.MemberSavedSearch.LastNotificationSent))) AND ((DATEPART(yyyy,GetDate()) != DATEPART(yyyy,dbo.MemberSavedSearch.LastNotificationSent))))THEN '1'

END

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 03-Jun-2008 18:34:39   
SELECT  MemberSavedSearch.MemberSavedSearchId, 
        Member.MemberId, 
        Member.Email, 
        MemberSavedSearch.PropertyNotificationFrequencyId
FROM    Member 
        INNER JOIN MemberSavedSearch ON Member.MemberId = MemberSavedSearch.MemberId
WHERE   (MemberSavedSearch.EmailStop = 0)
 And    (
            (
                (MemberSavedSearch.PropertyNotificationFrequencyId = 1 OR MemberSavedSearch.PropertyNotificationFrequencyId = 4) 
                AND
                DATEPART(dy, MemberSavedSearch.LastNotificationSent) != DATEPART(dy,GetDate())
            )
            or
            (
                (MemberSavedSearch.PropertyNotificationFrequencyId = 2 OR MemberSavedSearch.PropertyNotificationFrequencyId = 5) 
                AND 
                DATEPART(dw,GetDate()) = 6 AND DATEPART(dy,MemberSavedSearch.LastNotificationSent) != DATEPART(dy,GetDate())
            )
            or
            (
                (MemberSavedSearch.PropertyNotificationFrequencyId = 3 OR MemberSavedSearch.PropertyNotificationFrequencyId = 6) 
                AND 
                DATEPART(mm,MemberSavedSearch.LastNotificationSent) != DATEPART(mm,GetDate())
                AND 
                DATEPART(yyyy,MemberSavedSearch.LastNotificationSent) != DATEPART(yyyy,GetDate())
            )           
        )

now it's just a matter of nesting predicates. not knowing exactly how your using this you may want to remove the datepart functions and use between @start and @stop date variables.

because this logic is so complex I would also consider quering the database for unsent emails and then filtering the results in code using specifications.