SQL Server @@ rowcount限制之前

时间:2012-03-30 20:56:02

标签: sql sql-server

我正在开展一个项目,我需要在有限的结果之前获得总行数。我有这个在mysql中工作,但我也需要它在SQL Server中工作。

我知道我可以用更多的时间来解决这个问题,但我很累,而且还有很多其他我需要做的工作。任何帮助将不胜感激。

这是我的问题:

WITH filter 
     AS (SELECT "order"."amount", 
                "order"."id", 
                "status", 
                "priority", 
                "poNum", 
                "ip", 
                "customer"                                     AS 'cID', 
                "customer"."firstName", 
                "customer"."lastName", 
                "date", 
                "shippingMethod", 
                "site" + ':' + Isnull("admin", '')             AS "site", 
                "displayedCurrency", 
                "address"."country", 
                Isnull((SELECT COUNT("rma"."id") 
                        FROM   "rma" 
                        WHERE  "rma"."order" = "order"."id" 
                        GROUP  BY "rma"."order"), 0)           AS rma, 
                Row_number() OVER (ORDER BY "order"."id" DESC) AS rownum, 
                Isnull((SELECT COUNT("order") 
                        FROM   order_comment 
                        WHERE  "order" = "order"."id" 
                        GROUP  BY "order"), 0)                 AS COMMENT 
         FROM   "order" 
                LEFT JOIN "customer" 
                  ON "customer"."id" = "order"."customer" 
                LEFT JOIN "address" 
                  ON "address"."id" = "order"."sAddress") 
SELECT * 
FROM   "filter" 
WHERE  "rownum" BETWEEN 0 AND 10 
ORDER  BY filter."id" DESC 

SELECT @@ROWCOUNT AS 'cnt' 

1 个答案:

答案 0 :(得分:2)

自己回答。

我在计数(*)后使用了over() clause。我不知道这是否是最佳解决方案,但它对我有用。


WITH filter 
     AS (SELECT count(*) over() AS 'cnt',
                "order"."amount", 
                "order"."id", 
                "status", 
                "priority", 
                "poNum", 
                "ip", 
                "customer"                                     AS 'cID', 
                "customer"."firstName", 
                "customer"."lastName", 
                "date", 
                "shippingMethod", 
                "site" + ':' + Isnull("admin", '')             AS "site", 
                "displayedCurrency", 
                "address"."country", 
                Isnull((SELECT COUNT("rma"."id") 
                        FROM   "rma" 
                        WHERE  "rma"."order" = "order"."id" 
                        GROUP  BY "rma"."order"), 0)           AS rma, 
                Row_number() OVER (ORDER BY "order"."id" DESC) AS rownum, 
                Isnull((SELECT COUNT("order") 
                        FROM   order_comment 
                        WHERE  "order" = "order"."id" 
                        GROUP  BY "order"), 0)                 AS COMMENT 
         FROM   "order" 
                LEFT JOIN "customer" 
                  ON "customer"."id" = "order"."customer" 
                LEFT JOIN "address" 
                  ON "address"."id" = "order"."sAddress") 
SELECT * 
FROM   "filter" 
WHERE  "rownum" BETWEEN 0 AND 10 
ORDER  BY filter."id" DESC