case when then or subquery?

  • i'm stuck!! i'm trying to determine if a customer attended our webinar based on their log in date/time - that's the easy part. but there are many customers who logged in several times, and using a case statement is not working the way i need it to. below is a snippet of my code:

    case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended'

    else 'Did Not Attend' end

    and this works if a customer ONLY logged in during the specified times. however, if they logged in during the event AND logged in prior to or after the event, they will be listed multiple times. makes sense so far, except i need either/or. i've tried to re-write my query several times, but i'm not able to exclude those records that fall outside of the specified time.

    thanks in advance for your help!

    Dana

    "Drats! Foiled again!"
  • danawexler (4/29/2013)


    i'm stuck!! i'm trying to determine if a customer attended our webinar based on their log in date/time - that's the easy part. but there are many customers who logged in several times, and using a case statement is not working the way i need it to. below is a snippet of my code:

    case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended'

    else 'Did Not Attend' end

    and this works if a customer ONLY logged in during the specified times. however, if they logged in during the event AND logged in prior to or after the event, they will be listed multiple times. makes sense so far, except i need either/or. i've tried to re-write my query several times, but i'm not able to exclude those records that fall outside of the specified time.

    thanks in advance for your help!

    Hard to give you an answer based just on what you posted. Can you post the DDL for the table(s) involved, some sample data that mirrors your current data, the expected results, and what you have tried so far?

  • Lynn is absolutely correct that in order to give any solid advice we need a lot more details to work with. However, maybe I can give you a nudge that will help you. It seems that what you need to do is to put together a cte that has all the CustomerID values that were in attendance. Then you can query your customer table with a left join to your cte. If the cte value is NULL they did not attend, otherwise they did.

    Something like this maybe:

    with Attended as

    (

    select jh.SomeID

    from jh

    join Web on [some conditions]

    )

    select case when a.SomeID IS NOT NULL then 'Attended' else 'Did Not Attend' end

    from jh

    left join Attended a on a.SomeID = jh.SomeID

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Also maybe an aggregate will work:

    -- Set up sample data, this is a guess of what your current query results look like

    -- For the sake of this example webinar #45623 was from 10:00am to 10:30am

    -- and webinar #23178 was from 11:00am to 11:30am

    set language english;

    declare @query_results table (user_name nvarchar(10), event_id int, join_date datetime, attendance nvarchar(15));

    insert into @query_results values ('Johnny', 45623, '2013-04-30 09:20', 'Did not attend');

    insert into @query_results values ('Johnny', 45623, '2013-04-30 10:01', 'Attended');

    insert into @query_results values ('Johnny', 45623, '2013-04-30 10:34', 'Did not attend');

    insert into @query_results values ('Janey', 45623, '2013-04-30 10:09', 'Attended');

    insert into @query_results values ('Jimmy', 45623, '2013-04-30 10:02', 'Attended');

    insert into @query_results values ('Jimmy', 45623, '2013-04-30 10:15', 'Attended');

    insert into @query_results values ('Jimmy', 45623, '2013-04-30 10:31', 'Did not attend');

    insert into @query_results values ('Missy', 23178, '2013-04-30 11:32', 'Did not attend');

    insert into @query_results values ('Janey', 23178, '2013-04-30 11:02', 'Attended');

    insert into @query_results values ('Johnny', 23178, '2013-04-30 11:12', 'Attended');

    -- The existing query is returning more than one line per user,

    -- but we only want to know if the user has attended the event.

    -- Write an aggregate query, taking care of which column(s) you need to group by

    selectq.user_name,

    q.event_id,

    min(attendance) as attendance

    from @query_results q

    group by q.user_name, q.event_id;

  • thank you both Lynn and Sean - I appreciate your patience.

    below is a more detail - i wanted to try to keep it simple but apparently i over-simplified the first time!

    select reg.CUSTOMER_ID, jh.join_date,

    case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended Webinar'

    else 'Did Not Attend Webinar' end as AttendedWebinar

    from Webinar.Registration reg

    INNER JOIN Webinar.Webinars web ON web.PRODUCT_CODE = reg.PRODUCT_CODE

    inner join dbo.CUSTOMER cus on cus.MASTER_CUSTOMER_ID = reg.CUSTOMER_ID

    left outer join Webinar.JoinHistory jh on jh.WEBINAR_HOST_REGISTRATION_ID = reg.WEBINAR_HOST_REGISTRATION_ID

    where reg.REGISTRATION_TYPE = 'LIVE'

    and reg.PRODUCT_CODE = @WebinarID

    and (@ReportType = case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended Webinar' else 'Did Not Attend Webinar' end

    or

    @ReportType = 'All Webinar Registrants')

    the problem i have is reflected in the below result set:

    CUSTOMER_IDJOIN_DATEAttendedWebinar

    0000000330644/17/2013 4:28:18 PMDid Not Attend

    0000000330644/18/2013 9:40:59 AMAttended

    The result i need is either Attended or Did Not Attend, and i realize my query is working correctly as written - it needs to be tweaked to allow for one or the other.

    Dana

    "Drats! Foiled again!"
  • danawexler (4/29/2013)


    thank you both Lynn and Sean - I appreciate your patience.

    below is a more detail - i wanted to try to keep it simple but apparently i over-simplified the first time!

    select reg.CUSTOMER_ID, jh.join_date,

    case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended Webinar'

    else 'Did Not Attend Webinar' end as AttendedWebinar

    from Webinar.Registration reg

    INNER JOIN Webinar.Webinars web ON web.PRODUCT_CODE = reg.PRODUCT_CODE

    inner join dbo.CUSTOMER cus on cus.MASTER_CUSTOMER_ID = reg.CUSTOMER_ID

    left outer join Webinar.JoinHistory jh on jh.WEBINAR_HOST_REGISTRATION_ID = reg.WEBINAR_HOST_REGISTRATION_ID

    where reg.REGISTRATION_TYPE = 'LIVE'

    and reg.PRODUCT_CODE = @WebinarID

    and (@ReportType = case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended Webinar' else 'Did Not Attend Webinar' end

    or

    @ReportType = 'All Webinar Registrants')

    the problem i have is reflected in the below result set:

    CUSTOMER_IDJOIN_DATEAttendedWebinar

    0000000330644/17/2013 4:28:18 PMDid Not Attend

    0000000330644/18/2013 9:40:59 AMAttended

    The result i need is either Attended or Did Not Attend, and i realize my query is working correctly as written - it needs to be tweaked to allow for one or the other.

    As Lynn has already stated we need to see ddl and sample data. This means we need a script to create your table(s) and insert statement to populate them with data that represent your issue. Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I think you're making it more complex than it needs to be.. this will give you the customer_ID of everyone who attended.. once you have that list you can use it as you wish.

    select distinct CUSTOMER_ID

    FROM

    (

    select reg.CUSTOMER_ID, jh.join_date,

    case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended Webinar' else null end as AttendedWebinar

    from Webinar.Registration reg

    INNER JOIN Webinar.Webinars web ON web.PRODUCT_CODE = reg.PRODUCT_CODE

    inner join dbo.CUSTOMER cus on cus.MASTER_CUSTOMER_ID = reg.CUSTOMER_ID

    left outer join Webinar.JoinHistory jh on jh.WEBINAR_HOST_REGISTRATION_ID = reg.WEBINAR_HOST_REGISTRATION_ID

    where reg.REGISTRATION_TYPE = 'LIVE'

    and reg.PRODUCT_CODE = @WebinarID

    and (@ReportType = case when jh.join_date BETWEEN DATEADD(mi,-45,web.START_DATE_TIME) and web.END_DATE_TIME then 'Attended Webinar' else 'Did Not Attend Webinar' end

    or

    @ReportType = 'All Webinar Registrants')

    ) SOURCE

    WHERE AttendedWebinar is not null

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply