Home » SQL & PL/SQL » SQL & PL/SQL » Creating a temporary view or table
Creating a temporary view or table [message #2539] Wed, 24 July 2002 12:42 Go to next message
Johnny Whiplash
Messages: 7
Registered: May 2002
Junior Member
I'm trying to create a query that not only give me the names of a particular column, but also give me the count for each distinct record. My pseudo code is as follows:

CREATE VIEW TEMP AS
(SELECT NAME FROM DBO.TABLE1 A, DBO.TABLE2 B
WHERE A.ID = B.GROUPID
AND
B.GROUPID IN
(SELECT GROUPID FROM DBO TABLE2)

Then count the records for each name with a count per name. So I want my results to be something like:

NAME COUNT
NAME COUNT
NAME COUNT

Any suggestions would be great. Thanks.
Re: Creating a temporary view or table [message #2540 is a reply to message #2539] Wed, 24 July 2002 12:59 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
I'm not sure about your view definition (checking that b.groupid is in the list of groupid values from b looks redundant), but once you have a set of names:

select name, count(*)
  from temp
 group by name;
Re: Creating a temporary view or table [message #2542 is a reply to message #2539] Wed, 24 July 2002 16:49 Go to previous messageGo to next message
Johnny Whiplash
Messages: 7
Registered: May 2002
Junior Member
Thanks Mahesh. I was missing the ever critical GROUP BY clause. This works fine.
Re: Creating a temporary view or table [message #2547 is a reply to message #2539] Thu, 25 July 2002 07:36 Go to previous message
Johnny Whiplash
Messages: 7
Registered: May 2002
Junior Member
This is the query I ended up with.

SELECT B.NAME, COUNT(A.GROUP_ID)
FROM TABLE1 A, TABLE2 B
WHERE A.GROUP_ID = B.ROW_ID
GROUP BY B.NAME

The results:

B.NAME A.COUNT
B.NAME A.COUNT
B.NAME A.COUNT
Previous Topic: Formatting sqlplus results
Next Topic: Simple questions
Goto Forum:
  


Current Time: Thu Apr 25 17:30:34 CDT 2024