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


 
Thread Tools Search this Thread
Top Forums Programming I'm going crazy with SQL select. Please help!
# 1  
Old 08-07-2011
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:

Code:
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:
Code:
 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! Smilie

Code:
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! ^_^
# 2  
Old 08-10-2011
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():
Code:
...
HAVING COUNT(*) > CASE T_IM_MNC
 WHEN 1 THEN 5
 WHEN 2 THEN 3
 ELSE        7 END

# 3  
Old 08-10-2011
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?
# 4  
Old 08-10-2011
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? Smilie
# 5  
Old 08-10-2011
Sorry, still don't understand.
Ok, when T_IM_MNC is file, it return 5... and then?

Could you please make a readable example?
# 6  
Old 08-10-2011
You said
Code:
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:
Code:
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:
Code:
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:
Code:
HAVING count(*) > CASE T_IM_MNC
 WHEN 01
     THEN 5
 WHEN 02
     THEN 3
 ELSE     7
 END

# 7  
Old 08-10-2011
Well, it was very simple! Smilie
Thanks again for your feedback! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Sql select replace

Hi All, I had a query related to sql select replace command. i have a table named clusters and it looks like this Code: name characteristics sample 1.1 parent sample 1.2 ... (2 Replies)
Discussion started by: siya@
2 Replies

2. Programming

[Solved] SQL SELECT REPLACE

Hi All, I had a query related to sql select update replace command. i have a table named clusters and it looks like this name model characteristics sample1.1 +123 parent sample1.2 -456 clone sample1.3 +122 ... (5 Replies)
Discussion started by: sonia102
5 Replies

3. Shell Programming and Scripting

SQL select all but not if it is already in an other column

I know I know.. for sure one of the easier mysql statements. But somehow I can not figure out this. I expect to see all distinct items of 'data_12' where 'kwroot' has 'straxx' in, and in the same row 'data_12' ist (not = 'kwsearched' in any existing row) data_12 ... (6 Replies)
Discussion started by: lowmaster
6 Replies

4. Shell Programming and Scripting

using SELECT sql statement in shell script

Hi there I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this SN=123456 n=server1 m=x4140 sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';" echo $sql |/usr/sfw/bin/mysql -h... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

5. Shell Programming and Scripting

Select SQL Queries Option

count.sh#!/bin/ksh SQL1=`sqlplus -s usr/pwd @count.sql $1 $2 $3` SQL2=`sqlplus -s usr/pwd @selectall.sql $1 $2 $3` LIST="Count Select_All" select i in $LIST do if then echo $SQL1 elif then echo $SQL2 fi done (2 Replies)
Discussion started by: killboy
2 Replies

6. Shell Programming and Scripting

SQL Select and awk

Dear All , I have file1.txt contain values like the following: ---------- 23 24 25 and I have shell script which has the following : more file1.txt | awk '{print "select 'DUMP',CODE1||'|'||CODE2||'|'||CODE3 from CODE where CODE1='" $1 "';"}' > file2.sql all I need is to have the... (6 Replies)
Discussion started by: habuzahra
6 Replies

7. Shell Programming and Scripting

How to redirect value from sql select statment to unix variable

Hi, I need to get the value from the table using the sql command and store this value into the unix variable so that i can use this value for furthure use.. Please can any body help me in this regards Thanks & Regards Abdul Hafeez Shaik (17 Replies)
Discussion started by: abdulhafeez
17 Replies

8. Web Development

SQL Select inside Insert

I have following. . . . $userid = 2 . $query = "select username from users where userid = ".$userid.";"; . $username = $line; $data="Some Data Here"; . $query = "insert into logger (username, data) valuse ($username, $data);"; . I would like to not have 2 database calls. (3 Replies)
Discussion started by: Ikon
3 Replies

9. Shell Programming and Scripting

formatting the sql select result

Hi, I have about 12 columns and 15 rows to be retrived from sybase isql command through unix. But when i output the sql into a file and see it, the formatting is going for a toss. can someone please suggest how can i get the result correctly in the output file ? Thanks, Sateesh (2 Replies)
Discussion started by: kotasateesh
2 Replies

10. UNIX for Dummies Questions & Answers

Pipe SQL select statement results to script

Hello I would like to perform a select from a oracle table and return those values to my shell script For example: site=head -1 $infile | cut -c1-15 | awk '{printf "s%", $0} sqlplus -s /nolog |& #Open pipe to sql select col1, col2, col3, col4 from oracle_table where col5 =... (6 Replies)
Discussion started by: houtakker
6 Replies
Login or Register to Ask a Question