Unix Technical Forum

SQL question

This is a discussion on SQL question within the SQL Server forums, part of the Microsoft SQL Server category; --> I have a table 'cust' as follows : rec_id name dept ------- ------- ------- 1, 'john', 'a' 2, 'tom', ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 02:48 PM
osa
 
Posts: n/a
Default SQL question

I have a table 'cust' as follows :

rec_id name dept
------- ------- -------
1, 'john', 'a'
2, 'tom', 'b'
3, 'tom', 'a'
4, 'tom', 'a'
5, 'mary', 'b'
6, 'mary', 'a'
7, 'mary', 'a'


Looking for SQL to output rows with (dept = b - no other rows with
that name) OR (dept = a AND no other row for that name with dept = b)


IF dept = b
THEN
output only that row for that name
ELSE IF dept = a AND no other row for that name with dept = b
THEN
output that row


Required output :

rec_id name dept
------- ------- -------
1, 'john', 'a'
2, 'tom', 'b'
5, 'mary', 'b'



DDL

create table cust (rec_id int, name char(30), dept char(10))

insert into cust values (1, 'john', 'a')
insert into cust values (2, 'tom', 'b')
insert into cust values (3, 'tom', 'a')
insert into cust values (4, 'tom', 'a')
insert into cust values (5, 'mary', 'b')
insert into cust values (6, 'mary', 'a')
insert into cust values (7, 'mary', 'a')

Any ideas for SQL for this ?

Thanks
Lena

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 02:48 PM
M A Srinivas
 
Posts: n/a
Default Re: SQL question

On Apr 4, 5:00 am, "osa" <osal...@yahoo.com> wrote:
> I have a table 'cust' as follows :
>
> rec_id name dept
> ------- ------- -------
> 1, 'john', 'a'
> 2, 'tom', 'b'
> 3, 'tom', 'a'
> 4, 'tom', 'a'
> 5, 'mary', 'b'
> 6, 'mary', 'a'
> 7, 'mary', 'a'
>
> Looking for SQL to output rows with (dept = b - no other rows with
> that name) OR (dept = a AND no other row for that name with dept = b)
>
> IF dept = b
> THEN
> output only that row for that name
> ELSE IF dept = a AND no other row for that name with dept = b
> THEN
> output that row
>
> Required output :
>
> rec_id name dept
> ------- ------- -------
> 1, 'john', 'a'
> 2, 'tom', 'b'
> 5, 'mary', 'b'
>
> DDL
>
> create table cust (rec_id int, name char(30), dept char(10))
>
> insert into cust values (1, 'john', 'a')
> insert into cust values (2, 'tom', 'b')
> insert into cust values (3, 'tom', 'a')
> insert into cust values (4, 'tom', 'a')
> insert into cust values (5, 'mary', 'b')
> insert into cust values (6, 'mary', 'a')
> insert into cust values (7, 'mary', 'a')
>
> Any ideas for SQL for this ?
>
> Thanks
> Lena


select * from cust a
where ( dept = 'b' OR ( dept= 'a' and not exists( select 1 from cust
b where b.name=a.name and b.dept='b' ))
)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 02:48 PM
Ed Murphy
 
Posts: n/a
Default Re: SQL question

M A Srinivas wrote:

> On Apr 4, 5:00 am, "osa" <osal...@yahoo.com> wrote:


>> Looking for SQL to output rows with (dept = b - no other rows with
>> that name) OR (dept = a AND no other row for that name with dept = b)
>>
>> IF dept = b
>> THEN
>> output only that row for that name
>> ELSE IF dept = a AND no other row for that name with dept = b
>> THEN
>> output that row


If a name has multiple rows with dept = 'b', do you want to output
all of them? If not, then which one do you want to output?

This question does not affect dept = 'a', since you don't have "only"
attached to it.

>> create table cust (rec_id int, name char(30), dept char(10))


rec_id should be a primary key.

>> insert into cust values (1, 'john', 'a')
>> insert into cust values (2, 'tom', 'b')
>> insert into cust values (3, 'tom', 'a')
>> insert into cust values (4, 'tom', 'a')
>> insert into cust values (5, 'mary', 'b')
>> insert into cust values (6, 'mary', 'a')
>> insert into cust values (7, 'mary', 'a')


This indicates that (name, dept) is not a unique key. It would be
possible to append this:

insert into cust values (8, 'mary', 'b')

> select * from cust a
> where ( dept = 'b' OR ( dept= 'a' and not exists( select 1 from cust
> b where b.name=a.name and b.dept='b' ))
> )


Another way to do it:

select *
from cust
where dept = 'b'
or (dept = 'a' and name not in
(select name from cust where dept = 'b'))
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 03-01-2008, 02:49 PM
--CELKO--
 
Posts: n/a
Default Re: SQL question

>> I have a table 'cust' as follows : <<

Please stop using silly, non-relational record numbering!! I am going
to assume that the impropery named columns are the key and that you
meant to post:

CREATE TABLE DeptAssignments
(emp_name CHAR(15) NOT NULL
REFERENCES Personnel (emp_name),
dept_name CHAR(1) NOT NULL
REFERENCES Departments(dept_name),
PRIMARY KEY (emp_name, dept_name));

>> Looking for SQL to output rows with (dept = b - no other rows with that name) OR (dept = a AND no other row for that name with dept = b) <<


SELECT emp_name
FROM DeptAssignments
GROUP BY emp_name
HAVING MIN(dept_name) = 'b' -- HAS ONE OR MORE B'S
OR (MIN(dept_name = 'a') AND MAX(dept_name = 'a'); -- A'S ONLY


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-01-2008, 02:49 PM
osa
 
Posts: n/a
Default Re: SQL question

On Apr 4, 12:05 am, Ed Murphy <emurph...@socal.rr.com> wrote:
> M A Srinivas wrote:
> > On Apr 4, 5:00 am, "osa" <osal...@yahoo.com> wrote:
> >> Looking for SQL to output rows with (dept = b - no other rows with
> >> that name) OR (dept = a AND no other row for that name with dept = b)

>
> >> IF dept = b
> >> THEN
> >> output only that row for that name
> >> ELSE IF dept = a AND no other row for that name with dept = b
> >> THEN
> >> output that row

>
> If a name has multiple rows with dept = 'b', do you want to output
> all of them? If not, then which one do you want to output?
>
> This question does not affect dept = 'a', since you don't have "only"
> attached to it.
>
> >> create table cust (rec_id int, name char(30), dept char(10))

>
> rec_id should be a primary key.
>
> >> insert into cust values (1, 'john', 'a')
> >> insert into cust values (2, 'tom', 'b')
> >> insert into cust values (3, 'tom', 'a')
> >> insert into cust values (4, 'tom', 'a')
> >> insert into cust values (5, 'mary', 'b')
> >> insert into cust values (6, 'mary', 'a')
> >> insert into cust values (7, 'mary', 'a')

>
> This indicates that (name, dept) is not a unique key. It would be
> possible to append this:
>
> insert into cust values (8, 'mary', 'b')
>
> > select * from cust a
> > where ( dept = 'b' OR ( dept= 'a' and not exists( select 1 from cust
> > b where b.name=a.name and b.dept='b' ))
> > )

>
> Another way to do it:
>
> select *
> from cust
> where dept = 'b'
> or (dept = 'a' and name not in
> (select name from cust where dept = 'b'))


>> >> If a name has multiple rows with dept = 'b', do you want to output
>> >> all of them? If not, then which one do you want to output?


output all of them.

Thanks to all who replied. All the solutions work. 'rec_id' is the
primary key - I should have made that clear.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-01-2008, 02:49 PM
--CELKO--
 
Posts: n/a
Default Re: SQL question

>> 'rec_id' is the primary key - I should have made that clear. <<

Yes, which is why the **minimal** Netiquette is to post real DDL. I
hope to Ghod you are not just numbering physical rows in your table
with IDENTITY or other non-relational crap. Can you share with us
what it means in your data model? How is it validated and verified?

If you have merely mimicked a sequential file in SQL, your data
integrity is gone.





Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-01-2008, 02:49 PM
M A Srinivas
 
Posts: n/a
Default Re: SQL question

On Apr 5, 2:56 am, "--CELKO--" <jcelko...@earthlink.net> wrote:
> >> I have a table 'cust' as follows : <<

>
> Please stop using silly, non-relational record numbering!! I am going
> to assume that the impropery named columns are the key and that you
> meant to post:
>
> CREATE TABLE DeptAssignments
> (emp_name CHAR(15) NOT NULL
> REFERENCES Personnel (emp_name),
> dept_name CHAR(1) NOT NULL
> REFERENCES Departments(dept_name),
> PRIMARY KEY (emp_name, dept_name));
>
> >> Looking for SQL to output rows with (dept = b - no other rows with that name) OR (dept = a AND no other row for that name with dept = b) <<

>
> SELECT emp_name
> FROM DeptAssignments
> GROUP BY emp_name
> HAVING MIN(dept_name) = 'b' -- HAS ONE OR MORE B'S
> OR (MIN(dept_name = 'a') AND MAX(dept_name = 'a'); -- A'S ONLY


Your query does not provide the output (rec_id,name,dept) requested.
It is just listing name (emp_name)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:06 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com