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 |
|
ninaa
Starting Member
1 Post |
Posted - 2011-10-18 : 12:53:11
|
| select top 25 is_issue_no as [Issue Number], cm_full_name as [Client City]from issues i left join customers cm on i.is_customer_id=cm.cm_customer_id left join callbacks cb on i.is_issue_no=cb.cb_issue_nowhere (is_assigned_group_id=3 or is_assigned_group_id=4 or is_assigned_group_id=7 or is_assigned_group_id=10 or is_assigned_group_id=8) RESULT:::: Issue Number Client City3 HARVEY29 LA MOURE15 HAYFIELD22 SALISBURY23 DE SMET select CB_COMPLETEDDATE as [Date Last Called] , cm.cm_full_name as [Client City] from [callbacks] cbinner join issues i on cb.cb_issue_no=i.is_issue_no left join customers cm on i.is_customer_id=cm.cm_customer_id where CB_COMPLETEDDATE != 'NULL' and CB_COMPLETEDDATE < DateAdd(d, -7, GETDATE()) --and cm_full_name= 'columbus' group by cm_full_name RESULT:::: Date Last Called Client City10/3/2011 BRYANT4/21/2011 BRYON10/3/2011 COKATO9/20/2011 DALTON19/27/2011 DE SMET9/15/2011 HARVEY210/11/2011 HAYFIELD10/7/2011 KIESTER9/14/2011 LA MOURE --The first query gives me a list of issue no and the client city associated to that issue. --The second query gives me when the Client in that city was last called. --I need a list of isssue no with the client city as given by first query, but only those clients that are listed in the result of -- query no 2. Anyway that I can achieve it? |
|
|
pduffin
Yak Posting Veteran
68 Posts |
Posted - 2011-10-18 : 13:51:52
|
| Only select the name from the second query an put the query within an IN in the where statement.select top 25is_issue_no as [Issue Number],cm_full_name as [Client City]fromissues ileft join customers cm on i.is_customer_id=cm.cm_customer_idleft join callbacks cb on i.is_issue_no=cb.cb_issue_nowhere(is_assigned_group_id=3 or is_assigned_group_id=4 or is_assigned_group_id=7 or is_assigned_group_id=10 or is_assigned_group_id=8) and (cm_full_name in (select cm.cm_full_namefrom [callbacks] cbinner join issues i on cb.cb_issue_no=i.is_issue_noleft join customers cm on i.is_customer_id=cm.cm_customer_idwhere CB_COMPLETEDDATE != 'NULL' and CB_COMPLETEDDATE < DateAdd(d, -7, GETDATE())--and cm_full_name= 'columbus'group by cm_full_name)Likes to run, hates the runs! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-19 : 05:12:29
|
another way is to use join which will perform better than IN for large datasetsselect top 25is_issue_no as [Issue Number],cm_full_name as [Client City],t.[Date Last Called]fromissues ileft join customers cm on i.is_customer_id=cm.cm_customer_idleft join callbacks cb on i.is_issue_no=cb.cb_issue_noinner join (select CB_COMPLETEDDATE as [Date Last Called] , cm.cm_full_name as [Client City]from [callbacks] cbinner join issues i on cb.cb_issue_no=i.is_issue_noleft join customers cm on i.is_customer_id=cm.cm_customer_idwhere CB_COMPLETEDDATE != 'NULL' and CB_COMPLETEDDATE < DateAdd(d, -7, GETDATE())--and cm_full_name= 'columbus'group by cm_full_name) ton t.[Client City]= cm_full_namewhere(is_assigned_group_id=3 or is_assigned_group_id=4 or is_assigned_group_id=7 or is_assigned_group_id=10 or is_assigned_group_id=8) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|