Author |
Topic |
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-03 : 06:52:42
|
Hi There,I have an Sql Table that has a callId and a CallCode so I could have:CallId: 6 CallCode: (1) AssignedCallId: 6 CallCode: (2) AcceptedCallId: 6 CallCode: (3) CompletedCallId: 6 CallCode: (4) CompleteAcceptedI need to query the table and get a list of all the CallId that have a CallCode of (1) Assigned but not a CallCode of (2) AcceptedAlso the same for,I need to query the table and get a list of all the CallId that have a CallCode of (3) Completed but not a CallCode of (4) CompleteAcceptedI am trying to give a list of records that are waiting to be acceptedThanks for your help.Best Regards, Steve |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-03 : 07:26:08
|
you mean this?SELECT CalliDFROM TableGROUP BY CallIDHAVING (SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=2 THEN 1 ELSE 0 END) =0)OR (SUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=4 THEN 1 ELSE 0 END) =0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-04 : 14:55:41
|
Does anyone know how I can do thisHi,Thanks for taking the time to help.Your code works great but I need to be able to show what they relate to. on the left is what I have returned from your code on the right is what I need to be shown:CallId Callid Action 2 2 Needs Accepting3 3 Needs Accepting10 10 Needs Close-Out15 15 Needs Accepting16 16 Needs Close-Out22 22 Needs Close-Out25 25 Needs Close-Out26 26 Needs Accepting28 28 Needs Accepting29 29 Needs Close-Out30 30 Needs Close-Out31 31 Needs Close-OutYour code is exactly what I need but I must show what it relates to.Thanks again for your help, I appreciate it.Best Regards,Steve |
 |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-10 : 06:43:47
|
Hi There,I need your help further on this.The code that visakh16 supplied below works great but I need it to be more user friendly. This is visakh16 select query.SELECT CalliDFROM TableGROUP BY CallIDHAVING (SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=2 THEN 1 ELSE 0 END) =0)OR (SUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=4 THEN 1 ELSE 0 END) =0)I need to add something that when the sum above is 1, add some text that says 'Needs Accepting'IE the select query above returns something like:CallID237What I need to return is something like:CallID2 Needs Accepting3 Needs Accepting7 Needs AcceptingHow can I do that.Thanks again for your help. I do appreciate it.Best Regards,Steve |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-10 : 07:19:44
|
you mean this?SELECT CalliD,CASE WHEN SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >1 ORSUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >1THEN 'Needs Accepting'ELSE 'Some default value'ENDFROM TableGROUP BY CallIDHAVING (SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=2 THEN 1 ELSE 0 END) =0)OR (SUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=4 THEN 1 ELSE 0 END) =0) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-10 : 08:39:17
|
Hi There,Thanks for your reply, I appreciate you taking time to help.I am sorry to ask but I need it to work on both the criteria. I.E. If callcode = 1 then 'Needs Accepting' If Callcode = 3 then 'Needs CallBack'Also do I need to keep the code at the end:HAVING (SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=2 THEN 1 ELSE 0 END) =0)OR (SUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=4 THEN 1 ELSE 0 END) =0)Thanks again,Steve |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-10 : 09:16:30
|
[code]SELECT CalliD,CASE WHEN SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >1 THEN 'Needs Accepting'WHEN SUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >1 THEN 'Needs CallBack'ENDFROM TableGROUP BY CallIDHAVING (SUM(CASE WHEN CallCode=1 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=2 THEN 1 ELSE 0 END) =0)OR (SUM(CASE WHEN CallCode=3 THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN CallCode=4 THEN 1 ELSE 0 END) =0)[/code]you need to retain the code at end if you want to filter based on callcode value------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-10 : 10:48:46
|
Hi,Thanks again for your reply.I tried the code but it returns:CallCode | NoColumnName2 | NULL3 | NULL10| NULL15| NULLAny ideas why.Best Regards,Steve |
 |
|
mmarovic
Aged Yak Warrior
518 Posts |
Posted - 2011-08-10 : 14:27:39
|
quote: I need to query the table and get a list of all the CallId that have a CallCode of (1) Assigned but not a CallCode of (2) Accepted
select asgn.callId from table asgn where asgn.callCode = 1 and not exists(select * from table acc where acc.callId = asgn.callId and acc.callCode = 2 ) quote: I need to query the table and get a list of all the CallId that have a CallCode of (3) Completed but not a CallCode of (4) CompleteAccepted
Use the same logic as for the first query.MirkoMy blog: http://mirko-marovic-eng.blogspot.com/ |
 |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-10 : 18:46:50
|
Hi There,Thanks for the reply.I'll give that code a try but how do I add another column showing a prompt where not exists CallCode2.I would like to show something like:CallId | Message3 | Need to Accept 7 | Need to Accept9 | Need to Call */ This is if it's a Callcode of 3Thanks for your help.Best Regards,Steve |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-11 : 11:26:53
|
quote: Originally posted by Steve2106 Hi,Thanks again for your reply.I tried the code but it returns:CallCode | NoColumnName2 | NULL3 | NULL10| NULL15| NULLAny ideas why.Best Regards,Steve
how come SUM return NULL. i think you're using some modified query rather than my suggestion.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
Steve2106
Posting Yak Master
183 Posts |
Posted - 2011-08-12 : 04:22:08
|
Hi visakh16,Thanks for the reply and your continued help.Your right I have changed it a bit to work with a different table.This is the code I am using. It returns the correct ActionId but not the text. As mentioned the column name is (no column name) and it contains NULL.SELECT ActionID,CASE WHEN SUM(CASE WHEN EmailType = '(1) Assign/Re-Assign' THEN 1 ELSE 0 END) >1 THEN 'Needs Accepting'WHEN SUM(CASE WHEN EmailType = '(5) Complete Action' THEN 1 ELSE 0 END) >1 THEN 'Needs CallBack'ENDFROM tbemailhistoryGROUP BY ActionIdHAVING (SUM(CASE WHEN EmailType = '(1) Assign/Re-Assign' THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN EmailType = '(2) Accept' THEN 1 ELSE 0 END) =0)OR (SUM(CASE WHEN EmailType = '(5) Complete Action' THEN 1 ELSE 0 END) >0AND SUM(CASE WHEN EmailType = '(6) Accept Close-Out' THEN 1 ELSE 0 END) =0)I do not think I have changed your code much.Best Regards,Steve |
 |
|
|