0

How can I do it so that when I run this query.

SELECT distinct cus_areacode AS "Area Code", cus_code AS "Number"
FROM CUSTOMER
WHERE cus_areacode = 713 OR cus_areacode = 615;

instead of showing the following.

Area Code   Number
713         10015
713         10018
615         10019
615         10017
713         10011
615         10010
615         10016
615         10012
615         10014
615         10013

It may show this.

 Area Code  Number
    615       7
    713       3

I tried this

SELECT distinct cus_areacode AS "Area Code", count(cus_code) AS "Number"
FROM CUSTOMER
WHERE cus_areacode = 713 OR cus_areacode = 615;   

But it does not work.

6 Answers 6

2
SELECT cus_areacode AS "Area Code", count(cus_code) AS "Number"
FROM CUSTOMER GROUP BY cus_areacode
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much it worked beautifully. I had to add more parts to the query and now every thing worked perfect. Thank you.
2

Try this

SELECT cus_areacode AS "Area Code", count(cus_code) AS "Number"
FROM CUSTOMER 
WHERE cus_areacode IN(713,615)
GROUP BY cus_areacode;

OR:

SELECT cus_areacode AS "Area Code", count(cus_code) AS "Number"
FROM CUSTOMER
WHERE cus_areacode = 713 OR cus_areacode = 615
GROUP BY cus_areacode;

Fiddle Demo Here

Comments

1

you need this:

First of all I appreciate to tried query, your going right way, And almost got it you should just remove the distinct and add the Group By your area code.

And they distinct mean its In a table, a column may contain many duplicate values; and sometimes you only want to list the different (distinct) values.

And, The Group by means in a statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.

And you code is following as:

SELECT cus_areacode AS "Area Code", count(cus_code) AS "Number"
FROM CUSTOMER
WHERE cus_areacode = 713 OR cus_areacode = 615
GROUP BY cus_areacode;

SQL FIDDLE

1 Comment

Thank you very much jmail. I appreciate your explanation, you seem to know a lot.
0
SELECT areacode,count(cus_code) FROM CUSTOMER GROUP BY areacode

Comments

0

try this:

select * from 
(
select cus_areacode , cus_code 
FROM CUSTOMER
WHERE cus_areacode = 713 OR cus_areacode = 615 
) tempalias  
group by cus_areacode   

Comments

0
SELECT cus_areacode AS "Area Code", count(cus_code) AS "Number" FROM CUSTOMER
WHERE cus_areacode = 713 OR cus_areacode = 615
GROUP BY cus_areacode;

2 Comments

Your Query is Wrong its giving some syntax error, you should must change your query,.. But I'm not going to give the (-1) for query,..
Wow thank you for not giving a (-1) I really appreciate it and for so much highlighting text in your comment and answer, you really have all the time in the world but some of us are not so lucky :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.