MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

SQL for Unique Business Owners

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
pvrajesh31
Beginner


Joined: 10 Aug 2004
Posts: 31
Topics: 11

PostPosted: Tue Jul 11, 2006 1:48 am    Post subject: SQL for Unique Business Owners Reply with quote

Hi ,
I need a SQL for the following requirement

i have a database with two columns
column1 - Owner
column2 - business

both columns are part of primary key.

A owner can have more than one business like cloth,retail,supermarket,cinema,etc. But i want to find the owner who has only one business i.e. cinema. Owners who has only cinema business and no other business.

Please help me out ....

Thanks in advance.
_________________
Rajesh
Back to top
View user's profile Send private message
shekar123
Advanced


Joined: 22 Jul 2005
Posts: 528
Topics: 90
Location: Bangalore India

PostPosted: Tue Jul 11, 2006 3:13 am    Post subject: Reply with quote

Rajesh,

I had created a table in the following way and inserted values.Please check if this is the kind of situation you are looking for ?
Code:

CREATE TABLE NEW                                                       
(OWNER    CHAR(10) NOT NULL,                                             
 BUSINESS CHAR(10) NOT NULL,                                           
 PRIMARY KEY(OWNER,BUSINESS));                                         
CREATE UNIQUE INDEX NEW1 ON NEW(OWNER,BUSINESS);                       
INSERT INTO NEW VALUES('SHEKAR','CINEMA');                             
INSERT INTO NEW VALUES('SHEKAR','CLOTH');                               
INSERT INTO NEW VALUES('SHEKAR','RETAIL');                             
INSERT INTO NEW VALUES('SHEKAR','MARKET');                             
INSERT INTO NEW VALUES('KOLUSU','CINEMA');                             
INSERT INTO NEW VALUES('KOLUSU','CLOTH');                               
INSERT INTO NEW VALUES('KOLUSU','RETAIL');                             
INSERT INTO NEW VALUES('KOLUSU','MARKET');                             
SELECT * FROM NEW;

OUPUT
Code:

SELECT * FROM NEW; 
---------+---------+
OWNER       BUSINESS
---------+---------+
KOLUSU      CINEMA 
KOLUSU      CLOTH   
KOLUSU      MARKET 
KOLUSU      RETAIL 
SHEKAR      CINEMA 
SHEKAR      CLOTH   
SHEKAR      MARKET 
SHEKAR      RETAIL

Now use the query to execute your output.
Code:

SELECT OWNER FROM NEW WHERE BUSINESS = 'CINEMA';
---------+---------+---------+---------+--------
OWNER                                           
---------+---------+---------+---------+--------
KOLUSU                                         
SHEKAR                                         

_________________
Shekar
Grow Technically
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12384
Topics: 75
Location: San Jose

PostPosted: Tue Jul 11, 2006 4:36 am    Post subject: Reply with quote

Quote:

Owners who has only cinema business and no other business.


Shekhar,

The poster wanted to find out owners with Just cinema business. According to your data , none of the rows would qualify.

Pvrajesh,

Try this


Code:

SELECT *                                       
  FROM TABLE                                     
 WHERE OWNER NOT IN (SELECT OWNER               
                       FROM TABLE                 
                      WHERE BUSINESS = 'CINEMA')
                      ;         


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vivek1983
Intermediate


Joined: 20 Apr 2006
Posts: 222
Topics: 24

PostPosted: Tue Jul 11, 2006 4:43 am    Post subject: Reply with quote

Rajesh,

Quote:

Owners who has only cinema business and no other business



Please try this query:



Code:


 SELECT
   owner
   business
FROM Table1
WHERE owner IN (SELECT owner FROM 
Table1  GROUP BY owner
HAVING COUNT(busniess) <= 1)     
AND business = 'cinema'               
 FOR FETCH ONLY WITH UR               

 


Hope this helps.

Regards,
Vivek.G
Back to top
View user's profile Send private message
pvrajesh31
Beginner


Joined: 10 Aug 2004
Posts: 31
Topics: 11

PostPosted: Tue Jul 11, 2006 5:55 am    Post subject: Reply with quote

Hi Vivek,
Thanks for the SQL. Your SQL is giving the results i wanted.

Kolusu,
Actually your SQL is giving all owners who does not have 'Cinema' business,but i need to find owners having only 'cinema' business and no other business.

Thanks to all for helping me in finding the way.

Rajesh
_________________
Rajesh
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12384
Topics: 75
Location: San Jose

PostPosted: Tue Jul 11, 2006 6:42 am    Post subject: Reply with quote

pvrajesh,

You just need to add an AND clause to the outer select.

Code:

SELECT *                                       
  FROM TABLE                                     
 WHERE OWNER NOT IN (SELECT OWNER               
                       FROM TABLE                 
                      WHERE BUSINESS = 'CINEMA')
    AND BUSINESS = 'CINEMA'
;


Hope this helps...

Cheers

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
vivek1983
Intermediate


Joined: 20 Apr 2006
Posts: 222
Topics: 24

PostPosted: Tue Jul 11, 2006 6:53 am    Post subject: Reply with quote

kolusu,

The query will return no rows since

the predeciates are contradictory.

Meaning .. NOT IN (SELECT OWNER
FROM TABLE
HERE BUSINESS = 'CINEMA') will select rows that corresponds to non-cinema whereas AND BUSINESS = 'CINEMA' corresponds to cinema-rows.

Please let me know if i have missed something.

Thanks,
Vivek.G
_________________
Vivek G
--------------------------------------
A dream is just a dream. A goal is a dream with a plan and a deadline. (Harvey Mackay)
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12384
Topics: 75
Location: San Jose

PostPosted: Tue Jul 11, 2006 8:15 am    Post subject: Reply with quote

vivek1983,

Yes you are right. The sql I gave Does not yield the desired results. Sorry.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
pvrajesh31
Beginner


Joined: 10 Aug 2004
Posts: 31
Topics: 11

PostPosted: Tue Jul 11, 2006 8:16 am    Post subject: Reply with quote

Hi Vivek,
You are right.
That SQL will always give SQL code 100. Because outer select will always get owners who does not have cinema business.

Kolusu,
I made a small correction in the SQL then it is working. Pls see below
Code:

SELECT *                                       
  FROM TABLE                                     
 WHERE OWNER NOT IN (SELECT OWNER               
                       FROM TABLE                 
                      WHERE BUSINESS <> 'CINEMA')
    AND BUSINESS = 'CINEMA'
;

I hope this is correct. Please advice.

Thanks
Rajesh
_________________
Rajesh
Back to top
View user's profile Send private message
coolman
Intermediate


Joined: 03 Jan 2003
Posts: 283
Topics: 27
Location: US

PostPosted: Fri Jul 14, 2006 11:14 am    Post subject: Reply with quote

would this not help?
Code:

SELECT COL1 FROM TABLE1
GROUP BY COL1
HAVING COUNT(*) = 1
AND COL2 = 'CINEMA'

________
marijuana vaporizer


Last edited by coolman on Sat Feb 05, 2011 1:44 am; edited 1 time in total
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12384
Topics: 75
Location: San Jose

PostPosted: Fri Jul 14, 2006 12:06 pm    Post subject: Reply with quote

coolman,

Did you test your sql ? The query will not work.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
coolman
Intermediate


Joined: 03 Jan 2003
Posts: 283
Topics: 27
Location: US

PostPosted: Mon Jul 17, 2006 4:25 pm    Post subject: Reply with quote

No, I didn't test it. I don't have access to creating tables. Btw, is it because of the composite key that the group by would fail?
________
Carrozzeria Ghia


Last edited by coolman on Sat Feb 05, 2011 1:44 am; edited 1 time in total
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12384
Topics: 75
Location: San Jose

PostPosted: Tue Jul 18, 2006 8:00 am    Post subject: Reply with quote

coolman wrote:
Btw, is it because of the composite key that the group by would fail?


You are using a column function Count(*) and you are checking for COl2 which is not a part of the Select query.

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group