获取行数

时间:2012-10-12 06:14:03

标签: sql sql-server

我有以下sql查询。我想得到所有的行数。有人可以帮帮我吗?

WITH numberedrows 
     AS (SELECT DISTINCT TOP 10 f_feeid_pk, 
                                f_code, 
                                f_description, 
                                f_fee, 
                                f_areaid_ind_fk, 
                                a_name_ind, 
                                p_name_ind, 
                                s_name_ind, 
                                b_name_ind, 
                                Row_number() 
                                  OVER ( 
                                    ORDER BY b_name_ind, s_name_ind, p_name_ind, 
                                  a_name_ind, f_code ASC) AS 
                                RowNumber 
         FROM   t_fee 
                INNER JOIN t_area 
                        ON f_areaid_ind_fk = a_areaid_pk 
                INNER JOIN t_project 
                        ON a_projectid_fk = p_projectid_pk 
                INNER JOIN t_section 
                        ON p_sectionid_fk = s_sectionid_pk 
                INNER JOIN t_branch 
                        ON s_branchid_fk = b_branchid_pk 
         WHERE  f_feeid_pk > 0) 
SELECT * 
FROM   numberedrows 

1 个答案:

答案 0 :(得分:2)

DISTINCT TOP 10替换为DISTINCT以获取所有不同的结果,假设您只对不同的计数感兴趣,否则也会删除DISTINCT,然后将SELECT更改为CTE使用COUNT(*)

WITH numberedrows 
     AS (SELECT DISTINCT f_feeid_pk, 
                                f_code, 
                                f_description, 
                                f_fee, 
                                f_areaid_ind_fk, 
                                a_name_ind, 
                                p_name_ind, 
                                s_name_ind, 
                                b_name_ind, 
                                Row_number() 
                                  OVER ( 
                                    ORDER BY b_name_ind, s_name_ind, p_name_ind, 
                                  a_name_ind, f_code ASC) AS 
                                RowNumber 
         FROM   t_fee 
                INNER JOIN t_area 
                        ON f_areaid_ind_fk = a_areaid_pk 
                INNER JOIN t_project 
                        ON a_projectid_fk = p_projectid_pk 
                INNER JOIN t_section 
                        ON p_sectionid_fk = s_sectionid_pk 
                INNER JOIN t_branch 
                        ON s_branchid_fk = b_branchid_pk 
         WHERE  f_feeid_pk > 0) 
SELECT COUNT(*) FROM numberedrows;