Wednesday, July 16, 2014

Rank and counting within subgroups

This was done for the inspector Report.

So this is in the select statement:

,G.[Total Number of Inspections on Permit]
,rank() over (PARTITION BY [b1_alt_id] ORDER BY COALESCE([G6ACTION].[G6_COMPL_DD],[G6ACTION].[G6_ACT_DD],G6_ACT_NUM) DESC, G6_ACT_NUM DESC) AS 'Order Number'


And here is the outer apply for the total count within a given record:

OUTER APPLY (SELECT COUNT(DISTINCT [G6_ACT_NUM]) AS 'Total Number of Inspections on Permit'
FROM [G6ACTION] g6a
WHERE g6a."SERV_PROV_CODE" = B1P."SERV_PROV_CODE"
AND g6a."B1_PER_ID1" = B1P."B1_PER_ID1"
AND g6a."B1_PER_ID2" = B1P."B1_PER_ID2"
AND g6a."B1_PER_ID3" = B1P."B1_PER_ID3" AND G6A.[REC_STATUS] = 'A' AND COALESCE(g6a.[G6_ACT_DD],g6a.[G6_COMPL_DD],'') BETWEEN @startdate
AND DATEADD(d, 1, @enddate)
and (
(ISNULL(@InspectorName, '') = '')
OR (
ISNULL([GA_FNAME], '') + CASE
WHEN ISNULL([GA_MNAME], '') = ''
THEN ' '
ELSE ' ' + [GA_MNAME] + ' '
END + ISNULL([GA_LNAME], '') = @InspectorName
)
) ) AS G

No comments:

Post a Comment