如何在Oracle中选择特定行的计数?

时间:2014-05-28 08:22:16

标签: sql database oracle

我正在写这样的查询...

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

我想要过滤计数...对此有什么完美的查询?

4 个答案:

答案 0 :(得分:2)

也许是这样的:

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

答案 1 :(得分:0)

尝试

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

答案 2 :(得分:0)

如果要求隐藏计数(以及输出中的任何内容),您可以使用别名:

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

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

答案 3 :(得分:0)

您可以使用SUM代替COUNT并按CASEDECODE

插入逻辑

DECODE一个

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

CASE一个

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