I'm going crazy with SQL select. Please help!

Hello people,
as wrote in title I'm going crazy with a "complex" SQL select.

This is the seelct:

select T_ADDRESS, T_MCC,T_MNC,T_MSIN,T_IM_MNC, COUNT(*) FROM TABLETEST 
WHERE T_MCC=123 AND (T_MNC=11 OR T_MNC=01)
GROUP BY T_ADDRESS,T_MCC,T_MNC,T_MSIN,T_IM_MNC HAVING count(*) > 5;

This select works fine, and the output is like:

 T_ADDRESS         T_MCC   T_MNC   T_MSIN   T_IM_MNC                 
 32011111111             123          11           1234567890    01                         7   
 32711111111             123          11           1234567890    02                         7   
 32911111111             123          11          1234567890     03                       13  

What I'm try to do now, is differentiate the HAVING count clause.
This mean I want "HAVING count(*) > X" based on T_IM_MNC table.

For example:
T_IM_MNC=01 => HAVING count() > 5
T_IM_MNC=02 => HAVING count(
) > 3
T_IM_MNC=03 => HAVING count(*) > 7

Is it possible in one query?
Could you please help me?

---------- Post updated at 04:54 AM ---------- Previous update was at 04:30 AM ----------

AHEM! After a cup of coffee I find myself the solution... It was so simple! That's why I'm going crazy! :stuck_out_tongue:

select T_ADDRESS, T_MCC,T_MNC,T_MSIN,T_IM_MNC, COUNT(*) 
FROM TABLETEST  WHERE T_MCC=123 AND (T_MNC=11 OR T_MNC=01) 
GROUP BY T_ADDRESS,T_MCC,T_MNC,T_MSIN,T_IM_MNC 
HAVING (count(*) > 5 and T_IM_MNC=01) or count(*) > 5 and T_IM_MNC=02) );

Well, hope my post will help someone in the future! :slight_smile:

I prefer case for situations like this, and generally -- you can hang nice comments. CASE can either key off logical predicates including expression IS NULL (CASE WHEN boolean_expression THEN result ....) or off values (CASE expression WHEN value THEN result ....). CASE is especailly grand for avoiding OR on indexed columns, which generally prevents using the index! It seems to be more portable and versatile than DECODE() and ISNULL():

...
HAVING COUNT(*) > CASE T_IM_MNC
 WHEN 1 THEN 5
 WHEN 2 THEN 3
 ELSE        7 END

Thanks for reply,
could you please make an example with commands?

What does it mean "WHEN 1 THEN 5" ?
Means values from 1 to 5?

It decodes cases of T_IM_MNC, so when T_IM_MNC is this value then return that value. When T_IM_MNC is 1 return 5 from CASE.

The logical form of case (more wordy but more versatile, is "CASE WHEN ( T_IM_MNC = 1 ) THEN 5 . . . ."

Case makes it both less wordy, prettier to format, and run faster. What's not to like? :smiley:

Sorry, still don't understand.
Ok, when T_IM_MNC is file, it return 5... and then?

Could you please make a readable example?

You said

For example:
T_IM_MNC=01 => HAVING count(*) > 5
T_IM_MNC=02 => HAVING count(*) > 3
T_IM_MNC=03 => HAVING count(*) > 7

so the CASE WHEN logical form is:

HAVING count(*) > CASE
 WHEN T_IM_MNC=01 THEN 5
 WHEN T_IM_MNC=02 THEN 3
 WHEN T_IM_MNC=03 THEN 7
 END

and assuming these are the only T_IM_MNC values, use ELSE:

HAVING count(*) > CASE
WHEN T_IM_MNC=01
THEN 5
WHEN T_IM_MNC=02
THEN 3
ELSE 7
END

but since only one expression is being tested, we can use the even simpler if more restricted decode or CASE expression WHEN test_value form:

HAVING count(*) > CASE T_IM_MNC
 WHEN 01
     THEN 5
 WHEN 02
     THEN 3
 ELSE     7
 END

Well, it was very simple! :rolleyes:
Thanks again for your feedback! :b:

It is also great for merging "if .... query1 else query2" into one query. Generally, less words is better ! It's a great structure for hanging comments, too. For that reason, I like it in C/C++/C#/JAVA "switch case" and shell "case (a) ;; (b) ;; (*) ;; esac" (using both parens so vi % still works), too.