Solved

Complax Case and Select question

Posted on 2013-01-03
7
455 Views
Last Modified: 2013-01-03
In the following select statement I am now passing in a paramater @vType

In the two Case statements when @vType = 'P' I want to use what I have below....referencing ContactV

When @vType <> 'N' I want the CASE statements to use ContactVN

Select 			vw.Pending,
			(	
				CASE WHEN ISNULL(ContactV.id, 0)= 0 AND ISNULL(vw.[Contact ID],0) > 0 THEN 'Add'
				WHEN ISNULL(ContactV.id, 0)> 0 AND ISNULL(vw.[Contact ID],0) > 0 
						AND NOT Exists (Select 1 from dbo.ClientVisitTrackingContacts WHERE vw.[Contact ID] = ContactV.[Contact ID] AND visitID = @VisitID ) THEN 'Add'
				WHEN ISNULL(vw.[Contact ID],0) = 0 THEN ''
				ELSE 'Remove' END
			) AS AddRemove,
			(
				CASE WHEN ISNULL(ContactV.id, 0) = 0 
				THEN 0 
				ELSE 1 END
			) AS ContactVisitSort,
			ContactV.VisitID vid,
			@VisitID id 
	FROM	dbo.vw_MarketingVisitationContacts AS vw 
	LEFT OUTER JOIN
			 dbo.ClientVisitTrackingContacts AS ContactV ON (vw.[Client ID] = ContactV.[Client ID] AND vw.[Contact ID] = ContactV.[Contact ID])
	LEFT OUTER JOIN
			dbo.ClientVisitTrackingNearBy ContactVN ON (vw.[Client ID] = COntactVN.nearbyClientID AND VW.[Contact ID] = ContactVN.nearbyContactID)
	WHERE		(LEN(vw.fullName) > 0) OR (vw.ur = 1)

Open in new window

0
Comment
Question by:lrbrister
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Jared_S
ID: 38740074
You could do this with nested cases, but an IF statement would make it much easier to read.

Either way, you're going to need to modify your criteria since

@vType = 'P'
and
@vType <> 'N'

are not mutually exclusive.

I would use:

IF @vType = 'P'
query1

IF @vType not in ('N','P')
query2
0
 

Author Comment

by:lrbrister
ID: 38740113
Jared_S
I have it working with nested case statements.

How would I do the "IF" in my select and where?
0
 
LVL 12

Accepted Solution

by:
Jared_S earned 350 total points
ID: 38740151
The IF just precedes the query block, so:

IF @vType = 'P'
Select                   vw.Pending,
                  (      
                        CASE WHEN ISNULL(ContactV.id, 0)= 0 AND ISNULL(vw.[Contact ID],0) > 0 THEN 'Add'
                        WHEN ISNULL(ContactV.id, 0)> 0 AND ISNULL(vw.[Contact ID],0) > 0
                                    AND NOT Exists (Select 1 from dbo.ClientVisitTrackingContacts WHERE vw.[Contact ID] = ContactV.[Contact ID] AND visitID = @VisitID ) THEN 'Add'
                        WHEN ISNULL(vw.[Contact ID],0) = 0 THEN ''
                        ELSE 'Remove' END
                  ) AS AddRemove,
                  (
                        CASE WHEN ISNULL(ContactV.id, 0) = 0
                        THEN 0
                        ELSE 1 END
                  ) AS ContactVisitSort,
                  ContactV.VisitID vid,
                  @VisitID id
      FROM      dbo.vw_MarketingVisitationContacts AS vw
      LEFT OUTER JOIN
                   dbo.ClientVisitTrackingContacts AS ContactV ON (vw.[Client ID] = ContactV.[Client ID] AND vw.[Contact ID] = ContactV.[Contact ID])
      LEFT OUTER JOIN
                  dbo.ClientVisitTrackingNearBy ContactVN ON (vw.[Client ID] = COntactVN.nearbyClientID AND VW.[Contact ID] = ContactVN.nearbyContactID)
      WHERE            (LEN(vw.fullName) > 0) OR (vw.ur = 1)
IF @vType NOT IN ('N','P')
... the second version of the query

If you had multiple statements in the same query, you would want to use Begin and End:
IF...
Begin
<query>
<query>
End

It MIGHT give you a slight performance boost, I'm not sure. It would be easy enough to try it and see. Otherwise, if it ain't broke...
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 150 total points
ID: 38740430
I would just change the LEFT JOIN conditions so that you don't have to maintain two different versions of the query, something like below.  (I really don't performance should be hurt here -- if it is dramatically worse, you might be forced to use two different queries):



Select                   vw.Pending,
                  (      
                        CASE WHEN ISNULL(ContactV.id, ContactVN.id, 0)= 0 AND ISNULL(vw.[Contact ID],0) > 0 THEN 'Add'
                        WHEN ISNULL(ContactV.id, ContactVN.id, 0)> 0 AND ISNULL(vw.[Contact ID],0) > 0
                                    AND NOT Exists (Select 1 from dbo.ClientVisitTrackingContacts WHERE vw.[Contact ID] = ISNULL(ContactV.[Contact ID], ContactVN.[ContactID]) AND visitID = @VisitID ) THEN 'Add'
                        WHEN ISNULL(vw.[Contact ID],0) = 0 THEN ''
                        ELSE 'Remove' END
                  ) AS AddRemove,
                  (
                        CASE WHEN ISNULL(ContactV.id, ContactVN.id, 0) = 0
                        THEN 0
                        ELSE 1 END
                  ) AS ContactVisitSort,
                  ISNULL(ContactV.VisitID, ContactVN.VisitID) vid,
                  @VisitID id
      FROM      dbo.vw_MarketingVisitationContacts AS vw
      LEFT OUTER JOIN
                   dbo.ClientVisitTrackingContacts AS ContactV ON @vType = 'P' AND (vw.[Client ID] = ContactV.[Client ID] AND vw.[Contact ID] = ContactV.[Contact ID])
      LEFT OUTER JOIN
                  dbo.ClientVisitTrackingNearBy ContactVN ON @vType NOT IN ('N', 'P') AND (vw.[Client ID] = COntactVN.nearbyClientID AND VW.[Contact ID] = ContactVN.nearbyContactID)
      WHERE            (LEN(vw.fullName) > 0) OR (vw.ur = 1)
0
 

Author Closing Comment

by:lrbrister
ID: 38741281
Thanks guys...
Nod to Jared for being first with a solution
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 38741355
So speed not quality.  I got it, will remember that next time and avoid wasting my time posting if you already have any kind of answer to your q.
0
 

Author Comment

by:lrbrister
ID: 38741638
Scott,
  I in no way meant to offend you.
I've used your answers for years and they are always of the highest quality.

This one time I gave a nod on a complex question to another person because quite frankly, running a shop alone I had to move on.

Again...sorry if I offended.  If I get the time one evening I already have a reminder to further investigate your answer.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question