Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

case when then or subquery? Expand / Collapse
Author
Message
Posted Monday, April 29, 2013 2:40 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, November 18, 2013 4:48 PM
Points: 46, Visits: 113
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!"
Post #1447775
Posted Monday, April 29, 2013 2:57 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 12:10 PM
Points: 23,237, Visits: 31,931
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 Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1447782
Posted Monday, April 29, 2013 3:08 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:42 AM
Points: 13,279, Visits: 12,112
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1447786
Posted Monday, April 29, 2013 3:25 PM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 2:20 PM
Points: 364, Visits: 384
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
select q.user_name,
q.event_id,
min(attendance) as attendance
from @query_results q
group by q.user_name, q.event_id;

Post #1447791
Posted Monday, April 29, 2013 3:26 PM


SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, November 18, 2013 4:48 PM
Points: 46, Visits: 113
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_ID JOIN_DATE AttendedWebinar
000000033064 4/17/2013 4:28:18 PM Did Not Attend
000000033064 4/18/2013 9:40:59 AM Attended

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!"
Post #1447792
Posted Monday, April 29, 2013 3:38 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 11:42 AM
Points: 13,279, Visits: 12,112
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_ID JOIN_DATE AttendedWebinar
000000033064 4/17/2013 4:28:18 PM Did Not Attend
000000033064 4/18/2013 9:40:59 AM Attended

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1447794
Posted Monday, April 29, 2013 4:42 PM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Monday, August 11, 2014 12:41 PM
Points: 541, Visits: 1,045
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

Post #1447815
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse