SET SERVEROUTPUT ON;
declare
cursor myCursor is (select auth_code, lastname, dob, driverlicensenumber, driverlicensestate
from snap_driver_record where auth_date >= sysdate - 13 and auth_sequence=0);
vAuthCode snap_driver_record.auth_code%type;
vLastName snap_driver_record.lastname%type;
vDob snap_driver_record.dob%type;
vLicenseNumber snap_driver_record.driverlicensenumber%type;
vLicenseState snap_driver_record.driverlicensestate%type;
vCrashCount integer;
vInspCount integer;
vCrashTot integer;
vInspTot integer;
vDirCount integer;
begin
select count(*) into vDirCount from snap_driver_record where auth_date >= sysdate - 13 and auth_sequence=0;
open myCursor;
loop
fetch myCursor into vAuthCode, vLastName, vDob, vLicenseNumber, vLicenseState;
EXIT WHEN myCursor%NOTFOUND;
-- get crash record count
select count(*) into vCrashCount
from crash_driver cd
,crash_master cm
where cd.crash_id = cm.crash_id
and cd.updated_last_name = vLastName
and cd.updated_license_number = vLicenseNumber
and cd.updated_license_state = vLicenseState
and trunc(cd.updated_dob) = to_date(vDob, 'mmddyyyy')
and cd.dir_flag = 1;
vCrashTot := vCrashTot + vCrashCount;
--Get Inspection record count
select count(*) into vInspCount
from insp_driver idr
,inspection insp
where insp.inspection_id = idr.inspection_id
and idr.updated_last_name = vLastName
and idr.updated_license_number = vLicenseNumber
and idr.updated_license_state = vLicenseState
and trunc(idr.updated_dob) = to_date(vDob, 'mmddyyyy')
and idr.dir_flag = 1;
vInspTot := vInspTot + vInspCount;
dbms_output.put_line('Auth Code = ' || vAuthCode || ' Crash Count = ' || vCrashCount || ' Insp Count = ' || vInspCount);
end loop;
close myCursor;
dbms_output.put_line('Dir Total = ' || vDirCount || ' Crash Total = ' || vCrashTot || ' Insp Total = ' || vInspTot);
end;
I recently added an ad hoc column to a heavily used query with paging and sorting. To get the sorting to work on the new aliased column it was necessary to pull the "row_number() over" clause in to an outer query. I just wanted to document how this was done for future reference. SELECT * FROM (SELECT my_table.*, ROW_NUMBER () OVER ( ORDER BY UPPER ( DECODE ( pSortDir, 'asc', DECODE (pSortOrder, 'HasDemographicData', has_demographic_data pc.legal_name))), UPPER ( DECODE ( pSortDir, 'desc', DECODE (pSortOrder, 'HasDemographicData', has_demographic_data ...
Comments
Post a Comment