Help with queries in SQL

Status
Not open for further replies.

jbode

Ars Legatus Legionis
11,752
Subscriptor
I can't figure out how to do it in a single query.

My approach would be to create a view on the table, something like
Code:
create view namecounts(name, num) as select lastname, count(lastname) from names group by lastname

and then query that view for names with a count greater than 1:
Code:
select firstname, lastname from names where lastname in (select name from namecounts where count > 1)

But, I haven't done this for a while, so I'm probably missing something obvious.
 

Uchiha

Ars Scholae Palatinae
807
I did not know it was violation sorry.
I thought it was easier to read the top rather than the bottom lol sorry.
I am using Oracle 10g SQL Plus.

New question:
What code is used for this?
So if I have a table with multiple rows for people lets say in a business. Each person has different phone numbers. If I were to run a query to count how many people with the same area code and if the person does not have a phone number it would say no number.
The query would display their name and phone number. I know how to display the count but how do I display the "No number"?
 

A.W.

Ars Tribunus Angusticlavius
6,790
So if I have a table with multiple rows for people lets say in a business. Each person has different phone numbers. If I were to run a query to count how many people with the same area code and if the person does not have a phone number it would say no number.
The query would display their name and phone number. I know how to display the count but how do I display the "No number"?
That's kind of ambiguous. Do you want a count of how many people have the same area code (columns "area code" and "number of people with area code"), or do you want to display the names and phone numbers of people with a given area code? You can't do both with one query.
 

Uchiha

Ars Scholae Palatinae
807
A.W.":3ozbttpn said:
So if I have a table with multiple rows for people lets say in a business. Each person has different phone numbers. If I were to run a query to count how many people with the same area code and if the person does not have a phone number it would say no number.
The query would display their name and phone number. I know how to display the count but how do I display the "No number"?
That's kind of ambiguous. Do you want a count of how many people have the same area code (columns "area code" and "number of people with area code"), or do you want to display the names and phone numbers of people with a given area code? You can't do both with one query.

I want to count the number of people with phone numbers in the same area code. If the person does not have a phone number, I would like to display No Numbers. So I would count all the people who does not have a phone number and display as No number.
Like:
Area Code Count
843---------6
909---------5
No Number---6
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
Uchiha":gs05c5ii said:
I did not know it was violation sorry.
I thought it was easier to read the top rather than the bottom lol sorry.
I am using Oracle 10g SQL Plus.

New question:
What code is used for this?
So if I have a table with multiple rows for people lets say in a business. Each person has different phone numbers. If I were to run a query to count how many people with the same area code and if the person does not have a phone number it would say no number.
The query would display their name and phone number. I know how to display the count but how do I display the "No number"?

you can use a case nvl2 statement, something like this: (Assuming phone numbers are in ###-###-#### format)

Code:
select
    nvl2(phone_number,substr(phone_number,1,3),'No Number') as area_code,
    count(1) as records
from 
    employee_table
group by
     nvl2(phone_number,substr(phone_number,1,3),'No Number');

This would give you each unique area code present with a count of records for each, and a category of 'No Number' that would also have a count.

EDIT: Obviously as this is a aggregate query, it won't be listing any individual records or their details. That would be done with a separate query.

EDIT2: updated with nvl2 instead of case
 

Keen

Ars Legatus Legionis
13,440
Subscriptor
[url=http:/civis/viewtopic.php?p=21636379#p21636379:3k1ykj0n said:
Uchiha[/url]":3k1ykj0n]
I want to count the number of people with phone numbers in the same area code. If the person does not have a phone number, I would like to display No Numbers. So I would count all the people who does not have a phone number and display as No number.
Like:
Area Code Count
843---------6
909---------5
No Number---6
select area_code, count(*)
from table
where area_code <> 'No Number'
group by area_code;

Remove the where clause if you actually want a count of how many people don't have numbers.
 

C0rinthian

Ars Tribunus Angusticlavius
6,858
Uchiha":1u5m20gx said:
I did this and it works. Thanks guys!

select nvl(phone, 'No Number') "Phone Numbers", count(*)
from table
group by nvl(phone, 'No Number')

That'll run and give you output, but unless the 'phone' field only has the area code, it won't return what you were asking for. :p Unless people share phones, you'll get the following:

###-###-###1 - 1
###-###-###2 - 1
###-###-###3 - 1
.
.
.

with one record for each phone number found in the table.
 
Status
Not open for further replies.