如何选择两个不同列的计数?

时间:2011-08-19 02:10:04

标签: sql count distinct

我想计算从这个SQL返回的不同COMPONENT_ID和PACKAGE_ID的总数。

SELECT DISTINCT COMPONENT_ID, PACKAGE_ID FROM BILL;

3 个答案:

答案 0 :(得分:5)

您可以使用:

select count(distinct (COMPONENT_ID || PACKAGE_ID))
  from BILL;

提供||是字符串连接运算符。

答案 1 :(得分:1)

select count(*)
from
(
    select distinct component_id, package_id
    from yourtable
) as distinctquery

答案 2 :(得分:1)

SELECT COUNT(*) FROM (
    SELECT DISTINCT component_id FROM bill
    UNION SELECT DISTINCT package_id FROM bill
)