在SQL Server中按查询结果计算总行数

时间:2017-10-11 04:05:28

标签: sql-server

我希望通过以下查询获取SQL Server中查询结果返回的行总数:

select a._PROCESS_INST_NO,a._ISSUER,a._ISSUE_DATE,d._PROCESS_CONTENTS,
            d._OPT_DIVISION,b._GOODS_CD,b._GOODS_NAME,b._QTY,
            a._Order_No,c._GOODS_CD,c._GOODS_NAME,a._NOTE,
            ( ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS) )Proc_Step_No
            --,() as Total_Rows
 from [ENVNDIVDB].[dbo].[TBL_PROC_PH] a
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PM] b
on b._PROCESS_INST_NO=a._PROCESS_INST_NO
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c
on c._PROCESS_INST_NO=a._PROCESS_INST_NO
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PN] d
on d._PROCESS_INST_NO=a._PROCESS_INST_NO
where a._PROCESS_INST_NO='609390'

()作为Total_Rows 是我正在等待的语句。 在这种情况下我不能使用count(*)。请帮忙 enter image description here

3 个答案:

答案 0 :(得分:1)

这个怎么样......

为结果集添加别名,并从中获取COUNT(*)

SELECT
  COUNT(*)
FROM (SELECT
  a._PROCESS_INST_NO,
  a._ISSUER,
  a._ISSUE_DATE,
  d._PROCESS_CONTENTS,
  d._OPT_DIVISION,
  b._GOODS_CD,
  b._GOODS_NAME,
  b._QTY,
  a._Order_No,
  c._GOODS_CD,
  c._GOODS_NAME,
  a._NOTE,
  (ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS)) Proc_Step_No
--,() as Total_Rows
FROM [ENVNDIVDB].[dbo].[TBL_PROC_PH] a
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PM] b
  ON b._PROCESS_INST_NO = a._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c
  ON c._PROCESS_INST_NO = a._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PN] d
  ON d._PROCESS_INST_NO = a._PROCESS_INST_NO
WHERE a._PROCESS_INST_NO = '609390') AS tabl

如果您希望COUNT(*)与结果集一起使用,则可以使用CTE。您需要SQL Server 2005或更高版本才能执行CTE

WITH tabl
AS (SELECT
  a._PROCESS_INST_NO,
  a._ISSUER,
  a._ISSUE_DATE,
  d._PROCESS_CONTENTS,
  d._OPT_DIVISION,
  b._GOODS_CD,
  b._GOODS_NAME,
  b._QTY,
  a._Order_No,
  c._GOODS_CD,
  c._GOODS_NAME,
  a._NOTE,
  (ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS)) Proc_Step_No
--,() as Total_Rows
FROM [ENVNDIVDB].[dbo].[TBL_PROC_PH] a
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PM] b
  ON b._PROCESS_INST_NO = a._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c
  ON c._PROCESS_INST_NO = a._PROCESS_INST_NO
INNER JOIN [ENVNDIVDB].[dbo].[TBL_PROC_PN] d
  ON d._PROCESS_INST_NO = a._PROCESS_INST_NO
WHERE a._PROCESS_INST_NO = '609390')
SELECT
  *,
  (SELECT
    COUNT(*)
  FROM tabl)
  total_count
FROM tabl

答案 1 :(得分:0)

使用@@RowCount

select a._PROCESS_INST_NO,a._ISSUER,a._ISSUE_DATE,d._PROCESS_CONTENTS,
            d._OPT_DIVISION,b._GOODS_CD,b._GOODS_NAME,b._QTY,
            a._Order_No,c._GOODS_CD,c._GOODS_NAME,a._NOTE,
            ( ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS) )Proc_Step_No
           ,@@rowCount as Total_Rows
 from [ENVNDIVDB].[dbo].[TBL_PROC_PH] a
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PM] b
on b._PROCESS_INST_NO=a._PROCESS_INST_NO
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c
on c._PROCESS_INST_NO=a._PROCESS_INST_NO
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PN] d
on d._PROCESS_INST_NO=a._PROCESS_INST_NO
where a._PROCESS_INST_NO='609390'
如果您在此查询之前有任何查询,

更新 @@Rowcount将无效。请尝试以下:

;with testCTE as 
(
select a._PROCESS_INST_NO,a._ISSUER,a._ISSUE_DATE,d._PROCESS_CONTENTS,
            d._OPT_DIVISION,b._GOODS_CD,b._GOODS_NAME,b._QTY,
            a._Order_No,c._GOODS_CD,c._GOODS_NAME,a._NOTE,
            ( ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS) )Proc_Step_No

 from [ENVNDIVDB].[dbo].[TBL_PROC_PH] a
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PM] b
on b._PROCESS_INST_NO=a._PROCESS_INST_NO
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c
on c._PROCESS_INST_NO=a._PROCESS_INST_NO
inner join [ENVNDIVDB].[dbo].[TBL_PROC_PN] d
on d._PROCESS_INST_NO=a._PROCESS_INST_NO
where a._PROCESS_INST_NO='609390'
)

select * 
, (select count(*) from testCTE) as Total_Rows
from testCTE

您也可以使用COUNT(*) OVER ()

select a._PROCESS_INST_NO,a._ISSUER,a._ISSUE_DATE,d._PROCESS_CONTENTS,
                d._OPT_DIVISION,b._GOODS_CD,b._GOODS_NAME,b._QTY,
                a._Order_No,c._GOODS_CD,c._GOODS_NAME,a._NOTE,
                ( ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS) )Proc_Step_No
               ,COUNT(*) OVER () as Total_Rows
     from [ENVNDIVDB].[dbo].[TBL_PROC_PH] a
    inner join [ENVNDIVDB].[dbo].[TBL_PROC_PM] b
    on b._PROCESS_INST_NO=a._PROCESS_INST_NO
    inner join [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c
    on c._PROCESS_INST_NO=a._PROCESS_INST_NO
    inner join [ENVNDIVDB].[dbo].[TBL_PROC_PN] d
    on d._PROCESS_INST_NO=a._PROCESS_INST_NO
    where a._PROCESS_INST_NO='609390'

答案 2 :(得分:0)

伯爵在评论中说,伯爵将无法工作......它将返回1。 @@ ROWCOUNT将起作用。

select a._PROCESS_INST_NO,a._ISSUER,a._ISSUE_DATE,d._PROCESS_CONTENTS, d._OPT_DIVISION,b._GOODS_CD,b._GOODS_NAME,b._QTY, a._Order_No,c._GOODS_CD,c._GOODS_NAME,a._NOTE, ( ROW_NUMBER() OVER (PARTITION BY a._PROCESS_INST_NO ORDER BY d._PROCESS_CONTENTS) )Proc_Step_No ,@@ROWCOUNT as Total_Rows from [ENVNDIVDB].[dbo].[TBL_PROC_PH] a inner join [ENVNDIVDB].[dbo].[TBL_PROC_PM] b on b._PROCESS_INST_NO=a._PROCESS_INST_NO inner join [ENVNDIVDB].[dbo].[TBL_PROC_PMS] c on c._PROCESS_INST_NO=a._PROCESS_INST_NO inner join [ENVNDIVDB].[dbo].[TBL_PROC_PN] d on d._PROCESS_INST_NO=a._PROCESS_INST_NO where a._PROCESS_INST_NO='609390'