[英]How to count employee readings in current month
I'm trying to work out how to make this view for my database 我正在尝试找出如何为我的数据库创建此视图
The number of meters read in the current month by each of the meter readers (this view should be able to be used at any time without having to be changed to accommodate the date). 每个抄表员在当月读取的抄表数量(该视图应该可以随时使用,而不必更改以适应日期)。
The main trouble I'm having is counting how many times the employee has read a meter in the month. 我遇到的主要麻烦是计算该员工该月读过电表的次数。 I'm using oracle and sql developer. 我正在使用oracle和sql开发人员。
These are my tables: 这些是我的表:
a2_METERREADER
- EMPLOYEEID
- FIRSTNAME
- LASTNAME
a2_READING
- READINGID
- METERID
- EMPLOYEEID
- BILLNUMBER
- READING
- DATERECORD
a2_Watermeter
- METERID
- ADDRESS
- SUBURB
- POSTCODE
- STATUS
- CUSTOMERID
- REPLACE
- INSTALLDATE
You need to join the meter reader and reading tables and count the number of rows, grouped by employee for the month in question. 您需要加入抄表器和读取表并计算行数,该行是按雇员在相关月份进行分组的。
Something along these lines: 遵循以下原则:
SELECT reader.EMPLOYEEID, COUNT(reading.READINGID)
FROM a2_METERREADER reader, a2_READING reading
WHERE reader.EMPLOYEEID = reading.EMPLOYEEID
AND YEAR(reading.DATERECORD) = YEAR(SYSDATE)
AND MONTH(reading.DATERECORD) = MONTH(SYSDATE)
GROUP BY reader.EMPLOYEEID
IF your daterecord is a DATE then to access current month from Windows you can do this. 如果您的日期记录是DATE,则可以从Windows访问当前月份。
SELECT EMPLOYEEID, COUNT(*)
FROM a2_READING
WHERE TO_CHAR(DATERECORD, 'MM/YYYY') = TO_CHAR(SYSDATE, 'MM/YYYY')
GROUP BY EMPLOYEEID
This way you eliminate the days from DATERECORD (only keeps months and year) and it has to be the same with the SYSDATE (Current Date) again converted to keep month and year. 这样,您就可以消除DATERECORD中的日期(仅保留月份和年份),并且必须与再次转换为保留月份和年份的SYSDATE(当前日期)相同。
You dont need to join with METERREADER table if you only need the IDs 如果只需要ID,则不需要与METERREADER表联接
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.