473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conditional Having Statement

5 New Member
Hey Guys,

I have a huge statement loads of if statements in... and its getting bigger.

On closer inspection there is only 3 difference in the select statement. so I thought I could cut the whole thing down to just 1 select statement if I have a conditional Having.

I've simplified the IF statement down a bit to give you an idea of what im trying to achieve

Expand|Select|Wrap|Line Numbers
  1. IF @month <> 0 & @diffFuture = 0 & @showDate <> 0
  2.  
  3.     HAVING (events.eventID = @eventID) 
  4.     AND     (events.enabled = 1)
  5.     AND     (MONTH(tickets_1.ticketStartDate) = MONTH(GETDATE())) 
  6.     AND    (YEAR(tickets_1.ticketStartDate) = YEAR(GETDATE()))
  7.  
  8. IF @month <> 0 & @diffFuture <> 0 & @showDate <> 0
  9.  
  10.     HAVING (events.eventID = @eventID) 
  11.     AND     (events.enabled = 1)
  12.     AND     (DATEPART(MONTH,tickets_1.ticketStartDate) = MONTH(@start)) 
  13.     AND     (DATEPART(YEAR,tickets_1.ticketStartDate) = YEAR(@start))
  14.  
  15. ELSE                            
  16.  
  17.     HAVING (events.eventID = @eventID) 
  18.     AND     (events.enabled = 1)        
  19.     AND     (DATEPART(MONTH,tickets_1.ticketStartDate) = @month) 
  20.     AND    (DATEPART(YEAR,tickets_1.ticketStartDate) = @year)
  21.  
But how would i turn that into a conditional HAVING.... I thought the below would work

Expand|Select|Wrap|Line Numbers
  1.         HAVING (events.eventID = @eventID) 
  2.     AND     (events.enabled = 1)    
  3.         CASE
  4.              WHEN @month <> 0 & @diffFuture = 0 & @showDate <> 0 THEN
  5.                           (MONTH(tickets_1.ticketStartDate) = MONTH(GETDATE())) 
  6.                         AND    (YEAR(tickets_1.ticketStartDate) = YEAR(GETDATE()))
  7.                 WHEN...
  8.         END
Any ideas?
Sep 2 '09 #1
7 4302
Delerna
1,134 Recognized Expert Top Contributor
Perhaps something like
Expand|Select|Wrap|Line Numbers
  1. AND MONTH(tickets_1.ticketStartDate) = 
  2.     case when @month <> 0 & @showDate<>0 then
  3.         case when @diffFuture = 0 then
  4.             MONTH(GETDATE()) 
  5.         else
  6.              MONTH(@start)
  7.         end
  8.     else
  9.         @month
  10.     end
  11.  
ditto for the year
Sep 3 '09 #2
Delerna
1,134 Recognized Expert Top Contributor
alternatively
@month, @diffFuture and @showDate look as though they will be either 0 or 1

so perhaps something like this will work
Expand|Select|Wrap|Line Numbers
  1. AND MONTH(tickets_1.ticketStartDate)
  2. =MONTH(GETDATE())*(1-@diffFuture)+MONTH(@start)*@diffFuture
  3.  
in other words, if f is either 1 or 0 then

(value1 * (1- f)) + (value2 * f ) = value1 when f=0 or value2 when f=1




It's just an idea, I will let you work out the rest of it if my assumption about the values of the variables is correct
Sep 3 '09 #3
tiptap
5 New Member
Great that makes sense.

Ok so what now if @month = 0. In that case I dont want MONTH(tickets_1 .ticketsStartDa te) in the Having statement.

Would i need to wrap that statement in an IF?
Sep 3 '09 #4
Delerna
1,134 Recognized Expert Top Contributor
in other words if @Month=0 you want to see every month?

You need somehow to say

MONTH(tickets_1 .ticketStartDat e)>@Month

when @Month=0 and when @Month<> 0 you need it to say

MONTH(tickets_1 .ticketStartDat e)=@Month


hmmmm ............... ...... thinking!



By the way, which one makes sense?
Sep 4 '09 #5
Delerna
1,134 Recognized Expert Top Contributor
Im thinkin OR to handle that case

Expand|Select|Wrap|Line Numbers
  1. AND MONTH(tickets_1.ticketStartDate) 
  2. =MONTH(GETDATE())*(1-@diffFuture)+MONTH(@start)*@diffFuture
  3.  
  4. OR MONTH(tickets_1.ticketStartDate) 
  5. >case when @Month=0 then 0 else 12 end
  6.  
There is no month > 12 so the OR will only take effect when @Month=0


Again, just ideas
Sep 4 '09 #6
Delerna
1,134 Recognized Expert Top Contributor
Also, not sure if you are aware but HAVING and WHERE work in the same way.

Generally you use HAVING when you need to filter by an AGGREGATE function

Expand|Select|Wrap|Line Numbers
  1. HAVING sum(Qty)>100 and Avg(Qty)>50
  2.  
You are not using agregates as filters so the more usual approach is
Expand|Select|Wrap|Line Numbers
  1. WHERE month(Dte)=2 and year(Dte)=2009
  2.  
Sep 4 '09 #7
tiptap
5 New Member
ill give those a go now and report back. cheers for the help so far!
Sep 4 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
2264
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far from being a "language internist" (on Python or anything else) so go easy on me if this is stupid - it just seemed quite elegant to me as a relative...
5
21916
by: Thomas Baxter | last post by:
Is it possible to have a conditional union statement in a stored proc? Here's an example on the northwind database. If says there's a syntax error near the UNION statement. Looks like it doesn't like having the BEGIN directly in front of it. Is the only solution to create a dynamic sql string then call exec on it? Any help...
11
2446
by: Steven T. Hatton | last post by:
I've made no secret of the fact that I really dislike the C preprocessor in C++. No aspect of the language has caused me more trouble. No aspect of the language has cause more code I've read to be difficult to understand. I've described it as GOTO's on steroids, and that's what it is!. One argument against abolishing it it that it is useful...
62
3244
by: Reinhold Birkenfeld | last post by:
Hi, after Guido's pronouncement yesterday, in one of the next versions of Python there will be a conditional expression with the following syntax: X if C else Y which is the same as today's (Y, X)
2
1608
by: estafford | last post by:
I am having trouble writing a conditional block using ASP.NET and C#. I am trying to do something like this: 1. if page is PostBack - transfer to another page 2. if not postback - connect to database and get information using a querystring id and create a DataReader (as DR). - generate a Form to display using values from the database...
3
4072
by: Lyners | last post by:
I am having a hard time with this one, and I thought it would be easy. I have a datagrid in which I have textboxs for users to enter data. One of the fields in the database behind the datagrid tells me if the textbox is readonly or not. I am having a hard time setting the readonly property of the textbox with a conditional if statement...
5
2892
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
5
2649
by: pwiegers | last post by:
Hi, I'm trying to use the result of a conditional statement in a where clause, but i'm getting 1)nowhere 2) desperate :-) The query is simple: -------- SELECT idUser, (@ageraw:=YEAR(CURRENT_DATE()) - YEAR(dateofbirth) -
43
7542
by: dev_cool | last post by:
Hello friends, I'm a beginner in C programming. One of my friends asked me to write a program in C.The purpose of the program is print 1 to n without any conditional statement, loop or jump. How is it possible? Please help me. Thanks in advance.
0
7478
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5343
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.