Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-13 : 21:09:03
|
| how can get this code to return thisregardinobjectidname| owneridname | activitytypecodename | lastcontactbob dealan melvin charles phonecall 10\10\07forbes felix melvin charles email 15\10\07as apposed to thisregardinobjectidname| owneridname | activitytypecodename | lastcontactbob dealan melvin charles phonecall 10\10\07bob dealan melvin charles email 15\10\07I need it to only return the most recent contact date for eas regardingobjectidname. currently, it outputs the most recent email and the most recent phonecall for each. i only need the most recent not the most recent for eas activitytypeSELECT FilteredActivityPointer.regardingobjectidname, FilteredActivityPointer.owneridname, FilteredActivityPointer.activitytypecodenameFROM FilteredActivityPointer CROSS JOIN (SELECT MAX(actualend) AS [Last Contact Date], owneridname, regardingobjectidname, activitytypecodename FROM FilteredActivityPointer AS FilteredActivityPointer_1 GROUP BY owneridname, regardingobjectidname, activitytypecodename, actualend) AS maxresultsWHERE (FilteredActivityPointer.statecodename = 'completed') AND (FilteredActivityPointer.activitytypecodename IN ('phone call', 'e-mail')) AND (FilteredActivityPointer.owneridname IN ('stephanie frazier'))GROUP BY FilteredActivityPointer.regardingobjectidname, FilteredActivityPointer.activitytypecodename, FilteredActivityPointer.owneridnamePlease helpCompnetsyslc |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-13 : 21:35:31
|
Not sure without adequate test data..SELECT a.regardingobjectidname, a_1.owneridname, a.activitytypecodename,a.actualendFROM FilteredActivityPointer a INNER JOIN (SELECT MAX(actualend) AS [Last Contact Date],owneridname FROM FilteredActivityPointer GROUP BY owneridname ) AS a_1on a.owneridname = a_1.owneridnameWHERE (a.statecodename = 'completed') AND (a.activitytypecodename IN ('phone call', 'e-mail')) AND(a.owneridname IN ('stephanie frazier'))On the subquery, you just need the max date and ownerid, don't group by the actualend date field if you want the max.. this way you grab the max date, and that date's owner. Return the ownerid name from the subquery and everything else from the main table. You won't need the group by at the end if the subquery is the only aggregating subquery. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-13 : 21:52:18
|
| It still didnt work. this is the query that gives me the closet resultSELECT regardingobjectidname, MAX(actualend) AS Last_Contacted_On, activitytypecodename, owneridnameFROM FilteredActivityPointer AS AWHERE (statecodename = 'completed') AND (activitytypecodename IN ('phone call', 'e-mail')) AND (owneridname IN ('stephanie frazier'))GROUP BY regardingobjectidname, activitytypecodename, owneridnameORDER BY regardingobjectidnamethe one you send returns multiple records for each regardingowneridnameCompnetsyslc |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-13 : 22:04:28
|
Explain how you got this:regardinobjectidname| owneridname | activitytypecodename | lastcontactbob dealan melvin charles phonecall 10\10\07bob dealan melvin charles email 15\10\07 with this criteria:AND (owneridname IN ('stephanie frazier'))in your posted sample.In the meantime, give me a couple rows of data to test with, and your desired result. Showing a query that doesn't produce the desired results, without sample data or valid desired results makes it more difficult."regardingowneridname" wasn't even a field in any of the queries or your sample data...so how would I know that multiple records would be returned?quote: I need it to only return the most recent contact date for eas regardingobjectidname. currently, it outputs the most recent email and the most recent phonecall for each. i only need the most recent not the most recent for eas activitytype
That, I misread before..so..SELECT a.regardingobjectidname, a_1.owneridname, a.activitytypecodename,a_1.[Last Contact Date]FROM FilteredActivityPointer a INNER JOIN (SELECT MAX(actualend) AS [Last Contact Date],regardingobjectidname FROM FilteredActivityPointer GROUP BY owneridname ) AS a_1on a.regardingobjectidname = a_1.regardingobjectidname and a.actualend = a_1.[Last Contact Date]WHERE (a.statecodename = 'completed') AND (a.activitytypecodename IN ('phone call', 'e-mail')) AND(a.owneridname IN ('stephanie frazier'))Might work better.In english that says to "get the max date per regardingobjectidname" and return joining records where the regardingobjectidname match and the actual end date matches the max(actualend) date for that regardingobjectidname. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-13 : 22:46:41
|
| Abel Vanhusan 11/11/2007 2:09:06 PM E-mail Stephanie FrazierAbel Vanhusan 11/11/2007 1:52:23 PM Phone Call Stephanie FrazierAdewole Osinubi 16/10/2007 1:33:41 AM E-mail Stephanie FrazierAdewole Osinubi 13/11/2007 11:29:50 PM Phone Call Stephanie FrazierAdly Thebaud 11/11/2007 2:16:18 PM E-mail Stephanie FrazierAdly Thebaud 11/11/2007 2:17:08 PM Phone Call Stephanie FrazierAdnan Zaman 22/10/2007 7:34:29 PM E-mail Stephanie FrazierAdnan Zaman 08/11/2007 2:36:52 PM Phone Call Stephanie FrazierAkinlolu Ojo 02/11/2007 7:03:06 PM E-mail Stephanie FrazierAkinlolu Ojo 13/11/2007 11:30:45 PM Phone Call Stephanie FrazierAl Fosha 28/08/2007 4:28:12 PM E-mail Stephanie FrazierAlan Green 13/11/2007 8:32:49 PM E-mail Stephanie FrazierAlan Green 13/11/2007 8:23:59 PM Phone Call Stephanie FrazierAlan Moody 28/08/2007 4:28:12 PM E-mail Stephanie FrazierAlan Segaloff 21/08/2007 7:50:13 PM E-mail Stephanie FrazierAleks Pausak 22/10/2007 8:01:47 PM E-mail Stephanie FrazierAleks Pausak 22/10/2007 7:59:00 PM Phone Call Stephanie FrazierAlex Lubertozzi 03/07/2007 1:57:09 PM Phone Call Stephanie FraziAlex Trebeck 11/11/2007 2:23:20 PM E-mail Stephanie FraziAlex Trebeck 11/11/2007 2:19:12 PM Phone Call Stephanie FrazAlexander Abr 24/08/2007 1:52:01 PM E-mail Stephanie FraziAlexander Abr 08/10/2007 12:46:48 PM Phone CallStephanie FraziAlexander kierce 01/11/2007 7:48:01 PM E-mail Stephanie FrazierAlexander Bie 01/11/2007 7:42:46 PM Phone Call Stephanie Frazsorry the code i gave you was just an example output here is a real one base on the 'stephanie fraz'it returns two records when all i need is one. i will try out the code you just sent and see how it works. thank you for your help.Compnetsyslc |
 |
|
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-13 : 22:59:01
|
| The query you just sent timesout whiler tryig to retrieve the results.Compnetsyslc |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-13 : 23:08:17
|
[code]Adewole Osinubi Adewole Osinubi Phone Call 2007-11-13 23:29:50.000Adly Thebaud Adly Thebaud Phone Call 2007-11-11 14:17:08.000Adnan Zaman Adnan Zaman Phone Call 2007-11-08 14:36:52.000Akinlolu Ojo Akinlolu Ojo Phone Call 2007-11-13 23:30:45.000Alex Lubertozzi Alex Lubertozzi Phone Call 2007-07-03 13:57:09.000Alex Trebeck Alex Trebeck E-mail 2007-11-11 14:23:20.000Alexander Abr Alexander Abr Phone Call 2007-10-08 12:46:48.000Alexander Bie Alexander Bie Phone Call 2007-11-01 19:42:46.000[/code]results from:[code]Create Table #filteredactivitypointer(regardingobjectidname varchar(35), actualend datetime not null, activitytypecodename varchar(15) not null, owneridname varchar(25) not null,statecodename varchar(10) not null)Insert Into #filteredactivitypointer (regardingobjectidname, actualend, activitytypecodename, owneridname,statecodename)Select 'Abel Vanhusan', '11/11/2007 2:09:06 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Abel Vanhusan', '11/11/2007 1:52:23 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Adewole Osinubi', '10/16/2007 1:33:41 AM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Adewole Osinubi', '11/13/2007 11:29:50 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Adly Thebaud', '11/11/2007 2:16:18 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Adly Thebaud', '11/11/2007 2:17:08 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Adnan Zaman', '10/22/2007 7:34:29 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Adnan Zaman', '11/08/2007 2:36:52 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Akinlolu Ojo', '11/02/2007 7:03:06 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Akinlolu Ojo', '11/13/2007 11:30:45 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Al Fosha', '08/28/2007 4:28:12 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Alan Green', '11/13/2007 8:32:49 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Alan Green', '11/13/2007 8:23:59 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Alan Moody', '08/28/2007 4:28:12 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Alan Segaloff', '08/21/2007 7:50:13 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Aleks Pausak', '10/22/2007 8:01:47 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Aleks Pausak', '10/22/2007 7:59:00 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Alex Lubertozzi', '07/03/2007 1:57:09 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Alex Trebeck', '11/11/2007 2:23:20 PM', 'E-mail', 'Stephanie Frazier','completed' UNION ALLSelect 'Alex Trebeck', '11/11/2007 2:19:12 PM', 'Phone Call', 'Stephanie Frazier','completed' UNION ALLSelect 'Alexander Abr', '08/24/2007 1:52:01 PM', 'E-mail', 'Stephanie Frazier','completed' UNION ALLSelect 'Alexander Abr', '10/08/2007 12:46:48 PM', 'Phone Call','Stephanie Frazier','completed' UNION ALLSelect 'Alexander kierce', '11/01/2007 7:48:01 PM', 'Email', 'Stephanie Frazier','completed' UNION ALLSelect 'Alexander Bie', '11/01/2007 7:42:46 PM', 'Phone Call', 'Stephanie Frazier','completed' SELECT a.regardingobjectidname, a_1.regardingobjectidname, a.activitytypecodename,a_1.[Last Contact Date]FROM #FilteredActivityPointer a INNER JOIN (SELECT MAX(actualend) AS [Last Contact Date],regardingobjectidname FROM #FilteredActivityPointer GROUP BY regardingobjectidname ) AS a_1on a.regardingobjectidname = a_1.regardingobjectidname and a.actualend = a_1.[Last Contact Date]WHERE (a.statecodename = 'completed') AND (a.activitytypecodename IN ('phone call', 'e-mail')) AND(a.owneridname IN ('stephanie frazier'))order by a.regardingobjectidnamedrop table #filteredactivitypointer[/code]My last query had a few issues (too little coffee). see bolded query above for syntax. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-13 : 23:09:37
|
quote: Originally posted by thehandsomecode The query you just sent timesout whiler tryig to retrieve the results.Compnetsyslc
Yah..sorry 'bout that. My last post at same time has results you need I believe. Helped having data to use.. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-13 : 23:30:23
|
| I get an error invalid object name #filteredactivitypointerCompnetsyslc |
 |
|
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-13 : 23:36:32
|
| Sorry my Bad i took of the # But it sttill times out.Compnetsyslc |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-13 : 23:39:51
|
I can't help if it times out, based on what you have posted, were my results what you would have expected?How big is the table you are running this against? It shouldn't time out, that is not the query's fault. Check your timeout settings... Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
thehandsomecode
Yak Posting Veteran
51 Posts |
Posted - 2007-11-14 : 11:31:22
|
| Okat i got that to work. it was the e-mail attribute messing up the whole thing.how do i now join all of this with another table called filteredcontact to pull out data like new_ratingCompnetsyslc |
 |
|
|
dataguru1971
Master Smack Fu Yak Hacker
1464 Posts |
Posted - 2007-11-14 : 11:45:01
|
Do another inner join on the table using the linking field.. Poor planning on your part does not constitute an emergency on my part. |
 |
|
|
|
|
|
|
|