Unix Technical Forum

Limit query output to a single record based on combination of fields

This is a discussion on Limit query output to a single record based on combination of fields within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Hi all, I have a query as follows: Select building, floor from T1 I would like to limit the ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-13-2008, 07:14 PM
Sashi
 
Posts: n/a
Default Limit query output to a single record based on combination of fields

Hi all,

I have a query as follows:

Select building, floor from T1

I would like to limit the output where the building, floor combination
is present only once in the output.

For example, if the output is

blgdA, 2
bldgA, 3
bldgA, 3
bldgB, 1
bldgB, 2
bldgB, 2

I would like to filter it so that the output is only
blgdA, 2
bldgB, 1

I'm not sure about how to do this.

Can someone help out?
Thanks,
Sashi
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-13-2008, 07:14 PM
Urs Metzger
 
Posts: n/a
Default Re: Limit query output to a single record based on combination offields

Sashi schrieb:
> Hi all,
>
> I have a query as follows:
>
> Select building, floor from T1
>
> I would like to limit the output where the building, floor combination
> is present only once in the output.
>
> For example, if the output is
>
> blgdA, 2
> bldgA, 3
> bldgA, 3
> bldgB, 1
> bldgB, 2
> bldgB, 2
>
> I would like to filter it so that the output is only
> blgdA, 2
> bldgB, 1
>
> I'm not sure about how to do this.
>
> Can someone help out?
> Thanks,
> Sashi

Search the docs for GROUP BY and HAVING.

htht,
Urs Metzger
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-13-2008, 07:14 PM
Sashi
 
Posts: n/a
Default Re: Limit query output to a single record based on combination offields

On May 12, 10:55 am, Urs Metzger <u...@ursmetzger.de> wrote:
> Sashi schrieb:
>
> > Hi all,

>
> > I have a query as follows:

>
> > Select building, floor from T1

>
> > I would like to limit the output where the building, floor combination
> > is present only once in the output.

>
> > For example, if the output is

>
> > blgdA, 2
> > bldgA, 3
> > bldgA, 3
> > bldgB, 1
> > bldgB, 2
> > bldgB, 2

>
> > I would like to filter it so that the output is only
> > blgdA, 2
> > bldgB, 1

>
> > I'm not sure about how to do this.

>
> > Can someone help out?
> > Thanks,
> > Sashi

>
> Search the docs for GROUP BY and HAVING.
>
> htht,
> Urs Metzger


Thank you.
So I modified the query thus:
select bldg, count(Floor)
from T
group by bldg
having count(Floor) =1

However, when I try to include the floor number
select lbcode_key, count(floor ), floor
it fails: ORA-00979: not a GROUP BY expression 
I want to be able to see the floor number.
Also there is another field "type" and I need to filter it by this
field as well, to include the regexp '%Switch'.

So my original query should have been:
Select building, floor from T1 where type like '%Switch'

However, the where clause is disallowed when using group by and the
having clause doesn't like this either.

How do I do this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-13-2008, 07:14 PM
Urs Metzger
 
Posts: n/a
Default Re: Limit query output to a single record based on combination offields

Sashi schrieb:
> On May 12, 10:55 am, Urs Metzger <u...@ursmetzger.de> wrote:
>> Sashi schrieb:
>>
>>> Hi all,
>>> I have a query as follows:
>>> Select building, floor from T1
>>> I would like to limit the output where the building, floor combination
>>> is present only once in the output.
>>> For example, if the output is
>>> blgdA, 2
>>> bldgA, 3
>>> bldgA, 3
>>> bldgB, 1
>>> bldgB, 2
>>> bldgB, 2
>>> I would like to filter it so that the output is only
>>> blgdA, 2
>>> bldgB, 1
>>> I'm not sure about how to do this.
>>> Can someone help out?
>>> Thanks,
>>> Sashi

>> Search the docs for GROUP BY and HAVING.
>>
>> htht,
>> Urs Metzger

>
> Thank you.
> So I modified the query thus:
> select bldg, count(Floor)
> from T
> group by bldg
> having count(Floor) =1
>
> However, when I try to include the floor number
> select lbcode_key, count(floor ), floor
> it fails: ORA-00979: not a GROUP BY expression 
> I want to be able to see the floor number.
> Also there is another field "type" and I need to filter it by this
> field as well, to include the regexp '%Switch'.
>
> So my original query should have been:
> Select building, floor from T1 where type like '%Switch'
>
> However, the where clause is disallowed when using group by and the
> having clause doesn't like this either.
>
> How do I do this?

OK:

select bldg, floor
from T
group by bldg, floor
having count(*) = 1

hth,
Urs Metzger
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-13-2008, 07:14 PM
Urs Metzger
 
Posts: n/a
Default Re: Limit query output to a single record based on combination offields

Sashi schrieb:
> On May 12, 10:55 am, Urs Metzger <u...@ursmetzger.de> wrote:
>> Sashi schrieb:
>>
>>> Hi all,
>>> I have a query as follows:
>>> Select building, floor from T1
>>> I would like to limit the output where the building, floor combination
>>> is present only once in the output.
>>> For example, if the output is
>>> blgdA, 2
>>> bldgA, 3
>>> bldgA, 3
>>> bldgB, 1
>>> bldgB, 2
>>> bldgB, 2
>>> I would like to filter it so that the output is only
>>> blgdA, 2
>>> bldgB, 1
>>> I'm not sure about how to do this.
>>> Can someone help out?
>>> Thanks,
>>> Sashi

>> Search the docs for GROUP BY and HAVING.
>>
>> htht,
>> Urs Metzger

>
> Thank you.
> So I modified the query thus:
> select bldg, count(Floor)
> from T
> group by bldg
> having count(Floor) =1
>
> However, when I try to include the floor number
> select lbcode_key, count(floor ), floor
> it fails: ORA-00979: not a GROUP BY expression 
> I want to be able to see the floor number.
> Also there is another field "type" and I need to filter it by this
> field as well, to include the regexp '%Switch'.
>
> So my original query should have been:
> Select building, floor from T1 where type like '%Switch'
>
> However, the where clause is disallowed when using group by and the
> having clause doesn't like this either.
>
> How do I do this?

You need to group by (at least) all columns which you
don't aggregate. And you don't need "count(floor)" in
the select list - you already know it to be 1.

WHERE is perfectly legal, but only before the GROUP BY
clause.

select bldg, floor
from T
where type like '%Switch'
group by bldg, floor
having count(*) = 1

hth,
Urs Metzger
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 08:04 AM.


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