I’m working on a project where I need to get the total row count before the limited results. I have this working in mysql, but I need it to work in SQL Server as well.
I know I can figure this out with some more time, but I’m tired and I have a lot of other work I need to be doing. Any help would be greatly appreciated.
Here is my query:
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'
Answered it myself.
I used the over() clause after count(*). I don’t know if that is the best solution, but it worked for me.