Unix Technical Forum

simple sql question - group by having ?

This is a discussion on simple sql question - group by having ? within the Oracle Miscellaneous forums, part of the Oracle Database category; --> I know this works Select columna From tablea Group by columna Having count(*) > 1 But how can get ...


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 04-08-2008, 12:38 PM
jobs
 
Posts: n/a
Default simple sql question - group by having ?

I know this works

Select columna From tablea Group by columna Having count(*) > 1


But how can get columnb and columnc of the tablea table if i don't
want to group by those columns?

A join? a union?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 12:38 PM
DA Morgan
 
Posts: n/a
Default Re: simple sql question - group by having ?

jobs wrote:
> I know this works
>
> Select columna From tablea Group by columna Having count(*) > 1
>
>
> But how can get columnb and columnc of the tablea table if i don't
> want to group by those columns?
>
> A join? a union?


SELECT cola, colb, colc
FROM t
WHERE cola IN (SELECT cola FROM (your_query_here));

One way among many. Also look at using EXISTS.

In the future always include your version when you post.
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 12:38 PM
Gints Plivna
 
Posts: n/a
Default Re: simple sql question - group by having ?

DA Morgan wrote:
> jobs wrote:
>
> SELECT cola, colb, colc
> FROM t
> WHERE cola IN (SELECT cola FROM (your_query_here));
>
> One way among many. Also look at using EXISTS.
>

Actually no need to scan table more than once.

assuming you haven't too ancient Oracle version:
SQL> desc test
Name Null? Type
----------------------------------------- --------
---------------------
TST_ID NOT NULL NUMBER
TST_OBJ_NAME VARCHAR2(100 CHAR)

SQL> ed
Wrote file afiedt.buf

1 select tst_obj_name, count(*)
2 from test
3 group by tst_obj_name
4* having count(*) >10
SQL> /

TST_OBJ_NAME
--------------------------------------------------------------------------------
COUNT(*)
----------
COSTS
13

COSTS_PROD_BIX
13

COSTS_TIME_BIX
13

SALES
17

SALES_CHANNEL_BIX
17

SALES_CUST_BIX
17

SALES_PROD_BIX
17

SALES_PROMO_BIX
17

SALES_TIME_BIX
17


9 rows selected.

SQL> ed
Wrote file afiedt.buf

1 select * from (
2 select tst_obj_name, tst_id, count(*) over (partition by
tst_obj_name) cnt
3 from test
4 )
5* where cnt > 10
SQL> /

TST_OBJ_NAME
--------------------------------------------------------------------------------
TST_ID CNT
---------- ----------
COSTS
28572 13

COSTS
28575 13

COSTS
28577 13

<skipped>

Hopefully original OP will be able to adjust this to your table.
Analytic functions rules!

Gints Plivna
http://www.gplivna.eu

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 07:57 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