显示查询输出/总记录数

时间:2014-08-04 13:24:46

标签: sql sqlite

我有以下查询

select Count(*), FUNCTIONAL_CLASS from RDF_NAV_LINK where 
(LINK_ID NOT IN (Select DEST_LINK_ID from RDF_SIGN_DESTINATION) AND
LINK_ID NOT IN (Select ORIGINATING_LINK_ID from RDF_SIGN_ORIGIN))
GROUP BY FUNCTIONAL_CLASS

我想显示查询输出,如下所示

Count(*) FUNCTIONAL_CLASS
152/252   1
2563/2655 2
251/5485  3
451/562   4
542/562   5

这里152是满足条件的记录数 252是满足FUNCTIONAL_CLASS = 1

的总记录数

有可能吗?

3 个答案:

答案 0 :(得分:1)

您可以考虑使用LEFT JOIN来确定ORIGIN和DESTINATION表中缺少的LINK_ID,如下所示:

SELECT
    fn_class_groups.FUNCTIONAL_CLASS,
    CONCAT(fn_class_groups.not_in_cnt,'/',fn_class_groups.total_cnt)
FROM
    (SELECT 
        FUNCTIONAL_CLASS,
        COUNT(rnl.LINK_ID) total_cnt,
        SUM
        (CASE
            WHEN rsd.DEST_LINK_ID IS NULL AND rso.ORIGINATING_LINK_ID IS NULL THEN 1
            ELSE 0
        END) not_in_cnt
    FROM RDF_NAV_LINK rnl
    LEFT JOIN (SELECT DISTINCT DEST_LINK_ID FROM RDF_SIGN_DESTINATION) rsd ON rnl.LINK_ID = rsd.DEST_LINK_ID
    LEFT JOIN (SELECT DISTINCT ORIGINATING_LINK_ID FROM RDF_SIGN_ORIGIN) rso ON rnl.LINK_ID = rso.ORIGINATING_LINK_ID
    GROUP BY FUNCTIONAL_CLASS
    ) fn_class_groups
ORDER BY fn_class_groups.FUNCTIONAL_CLASS;

答案 1 :(得分:1)

使用WHERE子句,您已丢弃有关被忽略行的所有信息。

要获得行总数,您必须单独计算:

SELECT count(*) || '/' || (SELECT count(*)
                           FROM RDF_NAV_LINK AS link2
                           WHERE link2.FUNCTIONAL_CLASS = link.FUNCTIONAL_CLASS),
       FUNCTIONAL_CLASS
FROM RDF_NAV_LINK AS link
WHERE LINK_ID NOT IN (SELECT DEST_LINK_ID        FROM RDF_SIGN_DESTINATION)
  AND LINK_ID NOT IN (SELECT ORIGINATING_LINK_ID FROM RDF_SIGN_ORIGIN)
GROUP BY FUNCTIONAL_CLASS

答案 2 :(得分:0)

您需要使用2个查询或使用子查询的查询:

select CONCAT((
    select Count(*) from RDF_NAV_LINK where 
   (LINK_ID NOT IN (Select DEST_LINK_ID from RDF_SIGN_DESTINATION) AND
   LINK_ID NOT IN (Select ORIGINATING_LINK_ID from RDF_SIGN_ORIGIN))
   AND FUNCTIONAL_CLASS = parentTable.FUNCTIONAL_CLASS 
   GROUP BY FUNCTIONAL_CLASS`)
   ,"/",Count(*)), FUNCTIONAL_CLASS from RDF_NAV_LINK as parentTable
GROUP BY FUNCTIONAL_CLASS`