0

I am writing a query like this...

select consumer_name,count(select * from consumer where product='TTT') 
from consumer;

I want filtered count... What will be perfect query for this?

2
  • you tagged with both mysql and oracle, please update the question and remove the tag which is irrelevant. Commented May 28, 2014 at 8:23
  • Can you give starting sample data and desired results? Your current query makes no sense (because the results of the COUNT() have no relation to the rest of the rows. Commented May 28, 2014 at 8:51

4 Answers 4

2

Maybe something like this:

select 
    consumer_name,
    (
        SELECT
            COUNT(*)
        FROM
            consumer AS tbl
        WHERE
            tbl.product='TTT'
    )
from 
    consumer;
Sign up to request clarification or add additional context in comments.

Comments

0

Try

select 
    consumer_name, COUNT(your_column_name which you want to count)
from  consumer
WHERE  tbl.product='TTT' group by colusumer_id;

1 Comment

You're missing a GROUP BY (unless I'm missing something).
0

You can use SUM instead of COUNT and insert the logic by CASE or DECODE

The DECODE one

SELECT consumer_name, SUM(DECODE(product, 'TTT', 1, 0))
FROM   consumer;

The CASE one

SELECT consumer_name, SUM(CASE WHEN product = 'TTT' THEN 1 ELSE 0 END))
FROM   consumer;

Comments

0

if you are asking to hide count ( and whatever in your output ) you can use alias :

select consumer_name,
count(select * from consumer where product='TTT') as A /*or any thing that you want */
from consumer;

or

select 
    consumer_name,
    (
        SELECT
            COUNT(*)
        FROM
            consumer AS tbl
        WHERE
            tbl.product='TTT'
    ) as A 
from 
    consumer;

Comments

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.