0

我使用聚合函数“SUM”编写了以下查询。

SELECT 
       D.PODetailID, 
       SUM(D.AcceptedQty) 
FROM STR_MRVDetail D 
INNER JOIN STR_MRVHeader H ON H.MRVID = D.MRVID 
INNER JOIN PUR_POHeader PH ON PH.POID = H.POID 
INNER JOIN PUR_PODetail PD ON PD.PODetailID = D.PODetailID
WHERE H.StatusID = 4
AND PH.POID = 839
AND (SELECT 
           SUM(AcceptedQty) 
     FROM 
           STR_MRVDetail 
     WHERE 
           STR_MRVDetail.PODetailID = PD.PODetailID) = 
                              (SELECT POQuantity FROM PUR_PODetail 
                               WHERE PUR_PODetail.PODetailID = PD.PODetailID)
                               GROUP BY D.PODetailID

目前,此查询返回 2 行。我想检索此查询的计数。如何计算上述查询的行数?

4

2 回答 2

2

您可以添加@@ROWCOUNT到查询中,它还会为您提供最终结果集以及结果集中所有行的计数。

SELECT D.PODetailID, SUM(D.AcceptedQty), @@ROWCOUNT FROM STR_MRVDetail D  
INNER JOIN STR_MRVHeader H ON H.MRVID = D.MRVID  
INNER JOIN PUR_POHeader PH ON PH.POID = H.POID  
INNER JOIN PUR_PODetail PD ON PD.PODetailID = D.PODetailID 
WHERE H.StatusID = 4 
AND PH.POID = 839 
AND (SELECT SUM(AcceptedQty) FROM STR_MRVDetail  
     WHERE STR_MRVDetail.PODetailID = PD.PODetailID) =  
     (SELECT POQuantity FROM PUR_PODetail  
      WHERE PUR_PODetail.PODetailID = PD.PODetailID) 
GROUP BY D.PODetailID 
于 2012-05-10T11:52:46.400 回答
1
SELECT COUNT(*) as Total_Rows
FROM
(
SELECT D.PODetailID as PODetailID, SUM(D.AcceptedQty) as Total_AcceptedQty
FROM STR_MRVDetail D 
INNER JOIN STR_MRVHeader H ON H.MRVID = D.MRVID 
INNER JOIN PUR_POHeader PH ON PH.POID = H.POID 
INNER JOIN PUR_PODetail PD ON PD.PODetailID = D.PODetailID
WHERE H.StatusID = 4
AND PH.POID = 839
AND (SELECT SUM(AcceptedQty) FROM STR_MRVDetail 
     WHERE STR_MRVDetail.PODetailID = PD.PODetailID) = 
     (SELECT POQuantity FROM PUR_PODetail 
      WHERE PUR_PODetail.PODetailID = PD.PODetailID)
GROUP BY D.PODetailID
) as t
于 2012-05-10T11:40:09.820 回答