2012-03-30 23 views
-2

私は限られた結果の前に総行数を取得する必要があるプロジェクトに取り組んでいます。私はこれをmysqlで動作させていますが、SQL Serverでも動作する必要があります。SQL Server @@ rowcount before limit

もう少し時間をかけてこれを把握することができますが、私は疲れていて、他にもたくさんの仕事が必要です。どんな助けでも大歓迎です。ここで

は私のクエリです:

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' 
+0

@sllありがとうございます。 –

+1

今後の投稿用にhttp://www.dpriver.com/pp/sqlformat.htmを使用 – sll

+0

ありがとう、ブックマークしました。 –

答えて

2

はそれを自分自身に答えました。

私はcount(*)の後に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 
 
+0

通行人のためのあなたの答えについて少し説明してください –

関連する問題