Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Database Server Software > Oracle > Pl/sql problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-11-03, 10:09
Babsco Babsco is offline
Registered User
 
Join Date: Feb 2003
Posts: 3
Pl/sql problem

Problem with PL/SQL
I want to use the count aggregate function on 3 columns within the same table and write out their answers uisng dbms output.

ex. Table pal
----------------
p1, p2, p3, p4 as columns.

select pl, count(*)amount
from pal
group by pl;

but the next select sats does not allow me......

Immediate help on this will be highly appreciated.

/Babsco
Reply With Quote
  #2 (permalink)  
Old 02-11-03, 10:31
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Pl/sql problem

Quote:
Originally posted by Babsco
but the next select sats does not allow me......
/Babsco

Sorry, what does this mean? Please whow what you are doing, and what error message you are getting (cut and paste from SQL Plus).
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 02-11-03, 17:52
Babsco Babsco is offline
Registered User
 
Join Date: Feb 2003
Posts: 3
Re: Pl/sql problem

Quote:
Originally posted by Babsco
Problem with PL/SQL
I want to use the count aggregate function on 3 columns within the same table and write out their answers uisng dbms output.

ex. Table pal
----------------
p1, p2, p3, p4 as columns.

select pl, count(*)amount
from pal
group by pl;

but the next select sats does not allow me......

Immediate help on this will be highly appreciated.

/Babsco


If I write:
select pl, count(*)as amount
from pal
group by pl;
The above statement works fine with no problem. But I would like to make a
similar selection statement for the next column as well.
like: select p2, count(*) as amount2
from pal
group by pl;
How can I join two select satements together, so that I can use count()
agregate on two columns of the same table? Should I use union all or?

select pl, count(*)as amount
from pal
group by pl;

select p2, count(*) as amount2
from pal
group by pl;
Reply With Quote
  #4 (permalink)  
Old 02-12-03, 06:34
natalia natalia is offline
Registered User
 
Join Date: Feb 2003
Posts: 3
Try:

select pl, p1, count(*) as amount
from pal
group by pl, p1;
Reply With Quote
  #5 (permalink)  
Old 02-12-03, 06:35
natalia natalia is offline
Registered User
 
Join Date: Feb 2003
Posts: 3
Red face

sorry that was meant to be p1, p2 (ignore pl)
Reply With Quote
  #6 (permalink)  
Old 02-12-03, 06:49
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Pl/sql problem

Yes, UNION ALL will give you a combined result like:

a 10
b 20
c 20
x 10
y 10

(where a,b,c are values of p1 and x,y are values of p2)

Is that what you want?
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #7 (permalink)  
Old 02-12-03, 07:03
Babsco Babsco is offline
Registered User
 
Join Date: Feb 2003
Posts: 3
Re: Pl/sql problem

Quote:
Originally posted by andrewst
Yes, UNION ALL will give you a combined result like:

a 10
b 20
c 20
x 10
y 10

(where a,b,c are values of p1 and x,y are values of p2)

Is that what you want?



Yes Andrew that is correct and I did try it already this morning and
it worked fine. I had as well to skip the ; after every group by keyword.
Thanks for the help

Cheers!
Babsco
Reply With Quote
  #8 (permalink)  
Old 02-12-04, 14:21
ada17 ada17 is offline
Registered User
 
Join Date: Dec 2002
Location: London, UK
Posts: 27
Re: Pl/sql problem

Hi, I read your reply to a post entitled: Pl/sql problem.

I am trying to do something similar. I have already managed to print out the column name and the count of matches using UNION All.

Output is:
TEAM ASTON_VILLA_GAMES_LEFT
---------- ----------------------
Charlton 1
Liverpool 0
Arsenal 0
Chelsea 1

What I want to do now is to get the total for the 4 outputs. eg:

TEAM ASTON_VILLA_GAMES_LEFT
---------- ----------------------
Charlton 1
Liverpool 0
Arsenal 0
Chelsea 1
2
Any imediate help will be appreciated.
Many thanks,

** code **
BEGIN

select 'Charlton ' as Team, count(opponent) AS Aston_Villa_Games_Left
from charlton_games@charltonlink
where opponent = 'Aston Villa' AND match_date > sysdate
union all
select 'Liverpool ' as Team, count(opponent) AS Villa_Games_Left
from liverpool_games@liverpoollink
where opponent = 'Aston Villa' AND match_date > sysdate
union all
select 'Arsenal ' as Team, count(opponent) AS Villa_Games_Left
from arsenal_games@arsenallink
where opponent = 'Aston Villa' AND match_date > sysdate
union all
select 'Chelsea ' as Team, count(opponent) AS Villa_Games_Left
from chelsea_games@chelsealink
where opponent = 'Aston Villa' AND match_date > sysdate;


END

Ahmed

Quote:
Originally posted by andrewst
Yes, UNION ALL will give you a combined result like:

a 10
b 20
c 20
x 10
y 10

(where a,b,c are values of p1 and x,y are values of p2)

Is that what you want?
__________________
the one and only ada17
Reply With Quote
  #9 (permalink)  
Old 02-13-04, 05:44
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Re: Pl/sql problem

In SQL Plus you can do this:

COMPUTE SUM OF aston_villa_games_left ON REPORT
BREAK ON REPORT

Then run your query again.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On