This afternoon I want to write such a script to count the number of employees logging on in the last three months, including the total number of employees, the number of employees logging on in the last three months, and the logon rate, I first thought of the add_moths function for the time function. This is no problem. It is no problem to use the count function in the aggregate function for statistics, the problem is that when I want to check the total number of workers and the number of logged-in workers together, the following error message is displayed, indicating that the field is not a grouping function, later, a select statement was added to the query result set to achieve the effect.
Fields queried with the aggregate function are grouped fields, which are used for aggregate function compute, and other fields report errors.
1. Incorrect syntax: The first query is not an error of the grouping field.
Select (select count (1) from tb_sm_sysstaff where staff_status = 's0a') Total number of workers
Count (distinct t. staff_id) Number of logins in the last three months
From tb_sm_loginsess t where t. login_time> = add_months (sysdate,-3)
2. Correct writing
--- Logon status of the current network in the last three months. The invalid employee ID is not included in the calculation scope.
Select (select count (1) from tb_sm_sysstaff where staff_status = 's0a') Total number of workers,
Tb. rn login count in the last three months,
Trunc (tb. rn/(select count (1) from tb_sm_sysstaff where staff_status = 's0a') *) | '%' login rate in the last three months
From (select count (distinct t. staff_id) rn from tb_sm_loginsess t where t. login_time> = add_months (sysdate,-3) tb;