473,287 Members | 1,581 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

Conditional Having Statement

5
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 4294
Delerna
1,134 Expert 1GB
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 Expert 1GB
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
Great that makes sense.

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

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

You need somehow to say

MONTH(tickets_1.ticketStartDate)>@Month

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

MONTH(tickets_1.ticketStartDate)=@Month


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



By the way, which one makes sense?
Sep 4 '09 #5
Delerna
1,134 Expert 1GB
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 Expert 1GB
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
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
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...
5
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...
11
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...
62
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...
2
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...
3
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...
5
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
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,...
43
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. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.