S BOT S BOT - 6 months ago 9
SQL Question

How to Count advanced query rows

I want to count this query rows . please help

SELECT
CrmDesk.Request.*
FROM
CrmDesk.Request
INNER JOIN
CrmDesk.Customer ON CrmDesk.Request.CustomerId = CrmDesk.Customer.Id
WHERE
(CrmDesk.Customer.GroupeId = '84b4ea32-b39b-49d3-bb24-35fd933fc594')
AND (RequestStatus != 4)
AND CrmDesk.Request.CustomerId IS NOT NULL
AND RequestStatus != 4

UNION

SELECT
CrmDesk.Request.*
FROM
CrmDesk.Customer
INNER JOIN
CrmDesk.CustomerOrder ON CrmDesk.Customer.Id = CrmDesk.CustomerOrder.CustomerId
INNER JOIN
CrmDesk.Request ON CrmDesk.CustomerOrder.Id = CrmDesk.Request.OrderId
WHERE
(CrmDesk.Customer.GroupeId = '84b4ea32-b39b-49d3-bb24-35fd933fc594')
AND (RequestStatus != 4)

UNION

SELECT
*
FROM
[CrmDesk].[Request]
WHERE
UserFrom IS NOT NULL
AND RequestStatus != 4
AND OwnerId IS NOT NULL
AND UserFrom IN (SELECT Id FROM [Karbazi].[User]
WHERE Id = '84b4ea32-b39b-49d3-bb24-35fd933fc594'
AND RequestStatus != 4

UNION

SELECT Id FROM [Karbazi].[User]
WHERE ParentUserId = '84b4ea32-b39b-49d3-bb24-35fd933fc594')
OR OwnerId IN (SELECT UserId
FROM [CrmDesk].[Customer]
WHERE groupeId = '84b4ea32-39b-49d3-bb24- 35fd933fc594')
AND UserFrom IS NOT NULL
AND RequestStatus != 4

UNION

SELECT *
FROM [CrmDesk].[Request]
WHERE UserToDo IN (SELECT id
FROM [Karbazi].[User]
WHERE ParentUserId='84b4ea32-b39b-49d3-bb24-35fd933fc594'
OR id ='84b4ea32-b39b-49d3-bb24-35fd933fc594' )
AND RequestStatus != 4

Answer

The simplest approach would be this

SELECT COUNT(*)
FROM
(
  --put your query here
) AS tbl