Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


case when then or subquery?


case when then or subquery?

Author
Message
danawexler
danawexler
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
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!"
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37929
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?

Cool
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)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16529 Visits: 16992
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)
Mansfield
Mansfield
Old Hand
Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)Old Hand (366 reputation)

Group: General Forum Members
Points: 366 Visits: 394
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;


danawexler
danawexler
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
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!"
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16529 Visits: 16992
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)
Erin Ramsay
Erin Ramsay
Mr or Mrs. 500
Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)Mr or Mrs. 500 (561 reputation)

Group: General Forum Members
Points: 561 Visits: 1099
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search