Aggregate Function note

Source: Internet
Author: User

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;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.