Question : Add preset percentile counts to SQL SP

Add percentile count to SQL SPQuestion: I need to find the number of vehicles in the following percentiles .... 0 - 25, 26 - 50, 51 - 75, 76 - 100, 101+

Here is a SP that get's the percentage figure for a single vehicle



I'M LOOKING FOR:
Range   Cnt
00-25    13
26-50    22
51-75    24
76-100  10
100+      3




CREATE PROCEDURE [dbo].[GetPercentageOfMaintenanceCycleUsedByGPRSDeviceHardwareID]
(
     @GPRSDeviceHardwareID As varchar(50)
)
AS

DECLARE @X As Int
DECLARE @Y As Int
DECLARE @A As Int
DECLARE @B As Int
DECLARE @HL As Int
DECLARE @Z As Int
DECLARE @ZZ As Double Precision
DECLARE @ZZZ As Double Precision
DECLARE @PercentageUsed As Int


BEGIN

            SET @X = (SELECT [PMHourMeter] FROM [PMSchedule] WHERE GPRSDeviceHardwareID = @GPRSDeviceHardwareID)
           
            SET @A = (SELECT MAX(RunningTotal) FROM HourMeter WHERE GPRSDeviceHardwareID = @GPRSDeviceHardwareID AND MaintenanceBit = 0)

            SET @B =(SELECT TotalHourMeterDifference FROM HourMeterDifference WHERE GPRSDeviceHardwareID = @GPRSDeviceHardwareID AND MaintenanceBit = 0)

            Set @Y = (@A + @B) / 60
           
            Set @HL = (@X - @Y)
           
            SET @Z = (SELECT [Hours] FROM [GPRSDevice] WHERE [GPRSDeviceHardwareID] = @GPRSDeviceHardwareID)

            Set @ZZ = (@Z - @HL)

            Set @ZZZ = ((@ZZ / @Z) * 100)

            SET @PercentageUsed = @ZZZ

RETURN @PercentageUsed

END

Answer : Add preset percentile counts to SQL SP

Sorry, should have read the full details above first,  this query looks a little more complicated but the guts are the same, just had to do some tricks to get the Ranges and keep them in the right order.  You can just use the query or put it inside the SP like I did above if you need to.
HTH,
Chris
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
SELECT  
				CASE	WHEN x.PctUsed <= 25 THEN '00-25'
						WHEN x.PctUsed <= 50 THEN '26-50'
						WHEN x.PctUsed <= 75 THEN '51-75'
						WHEN x.PctUsed <= 100 THEN '75-100'
						ELSE '101+'
				END RANGE,
				COUNT(*) Cnt
            FROM (
            SELECT	D.[GPRSDeviceHardwareID], 
					CAST(((CAST(([Hours] - ([PMHourMeter] - ((MaxRunningTotal + TotalHourMeterDifference) / 60))) AS DOUBLE PRECISION) / [Hours]) * 100) AS INT) PctUsed 
            FROM [GPRSDevice] D
            INNER JOIN [PMSchedule] S ON D.[GPRSDeviceHardwareID] = S.[GPRSDeviceHardwareID]
            INNER JOIN HourMeterDifference HMD ON S.[GPRSDeviceHardwareID] = HMD.[GPRSDeviceHardwareID]
            INNER JOIN (SELECT GPRSDeviceHardwareID, MAX(RunningTotal) AS MaxRunningTotal 
						FROM HourMeter 
						WHERE MaintenanceBit = 0 GROUP BY GPRSDeviceHardwareID) MRT 
				ON MRT.GPRSDeviceHardwareID = D.GPRSDeviceHardwareID
			) x
			GROUP BY
				CASE	WHEN x.PctUsed <= 25 THEN '00-25'
						WHEN x.PctUsed <= 50 THEN '26-50'
						WHEN x.PctUsed <= 75 THEN '51-75'
						WHEN x.PctUsed <= 100 THEN '75-100'
						ELSE '101+'
				END, 
				CASE	WHEN x.PctUsed <= 25 THEN 1
						WHEN x.PctUsed <= 50 THEN 2
						WHEN x.PctUsed <= 75 THEN 3
						WHEN x.PctUsed <= 100 THEN 4
						ELSE 5
				END 
			ORDER BY
				CASE	WHEN x.PctUsed <= 25 THEN 1
						WHEN x.PctUsed <= 50 THEN 2
						WHEN x.PctUsed <= 75 THEN 3
						WHEN x.PctUsed <= 100 THEN 4
						ELSE 5
				END
Random Solutions  
 
programming4us programming4us