Solved

Complax Case and Select question

Posted on 2013-01-03
7
449 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher 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:ScottPletcher
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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now