aggregate-functions


SQL Query Aggregate Error


I have written a query to get a list of users who have not logged anytime within the last 45 days. Here's the query I have so far:
SELECT DISTINCT(EMPLOYEE.EMP_CODE),EMP_TIME_DTL.DATE_ENTERED),EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME FROM EMP_TIME LEFT JOIN
EMP_TIME_DTL ON EMP_TIME.ET_ID = EMP_TIME_DTL.ET_ID LEFT JOIN
EMPLOYEE ON EMP_TIME.EMP_CODE = EMPLOYEE.EMP_CODE
WHERE MAX(EMP_TIME_DTL.DATE_ENTERED) < DATEADD(day, -45, GETDATE())
GROUP BY EMPLOYEE.EMP_CODE, EMP_TIME_DTL.DATE_ENTERED, EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME
The EMP_TIME table contains the hours and the day they worked them. The EMP_TIME_DTL contains the date the employee actually entered the time. That table will have one row for each time entry made. I just want to find: each distinct EMP_CODE for those people who haven't entered time in the last 45 days.
The query I've written will get me those employees who haven't entered time in 45 days, but it also returns all the dates they entered time prior to that 45 day window.
So for example, if the last time John Doe entered time was on MAy 1st, then I want to return that date and his employee id. What I don't want is to return all the time he entered up until May 1st.
I've also tried this:
SELECT DISTINCT(EMPLOYEE.EMP_CODE), (EMP_TIME_DTL.DATE_ENTERED), EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME
FROM EMP_TIME LEFT JOIN
EMP_TIME_DTL ON EMP_TIME.ET_ID = EMP_TIME_DTL.ET_ID LEFT JOIN
EMPLOYEE ON EMP_TIME.EMP_CODE = EMPLOYEE.EMP_CODE
GROUP BY EMPLOYEE.EMP_CODE, EMP_TIME_DTL.DATE_ENTERED, EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME
HAVING MAX(EMP_TIME_DTL.DATE_ENTERED) < DATEADD(day, -45, GETDATE())
Have also tried:
SELECT MAX(EMP_TIME_DTL.ET_ID), MAX(EMP_TIME_DTL.SEQ_NBR), (EMP_TIME.EMP_CODE), EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME, (EMP_TIME_DTL.DATE_ENTERED)
FROM EMP_TIME LEFT JOIN
EMPLOYEE ON EMP_TIME.EMP_CODE = EMPLOYEE.EMP_CODE LEFT JOIN
EMP_TIME_DTL ON EMP_TIME.ET_ID = EMP_TIME_DTL.ET_ID
GROUP BY EMP_TIME.EMP_CODE, EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME, (EMP_TIME_DTL.DATE_ENTERED), EMP_TIME_DTL.ET_ID, EMP_TIME_DTL.SEQ_NBR
HAVING MAX(EMP_TIME_DTL.DATE_ENTERED) < DATEADD(day, -45, GETDATE())
ORDER BY EMP_TIME.EMP_CODE, EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME, EMP_TIME_DTL.ET_ID, EMP_TIME_DTL.SEQ_NBR, (EMP_TIME_DTL.DATE_ENTERED)
Any help or insight would be greatly appreciated!!
I decided to use the following:
WITH EMP_TIME_CTE
(ET_ID, SEQ_NBR, EMP_CODE, LNAME, FNAME, DATE_ENTERED)
AS
(
SELECT MAX(EMP_TIME_DTL.ET_ID), MAX(EMP_TIME_DTL.SEQ_NBR), (EMP_TIME.EMP_CODE), EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME, (EMP_TIME_DTL.DATE_ENTERED)
FROM EMP_TIME LEFT JOIN
EMPLOYEE ON EMP_TIME.EMP_CODE = EMPLOYEE.EMP_CODE LEFT JOIN
EMP_TIME_DTL ON EMP_TIME.ET_ID = EMP_TIME_DTL.ET_ID
WHERE EMPLOYEE.EMP_TERM_DATE IS NULL
GROUP BY EMP_TIME.EMP_CODE, EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME, (EMP_TIME_DTL.DATE_ENTERED), EMP_TIME_DTL.ET_ID, EMP_TIME_DTL.SEQ_NBR
HAVING MAX(EMP_TIME_DTL.DATE_ENTERED) < DATEADD(day, -45, GETDATE())
-- ORDER BY EMP_TIME.EMP_CODE, EMPLOYEE.EMP_LNAME, EMPLOYEE.EMP_FNAME, EMP_TIME_DTL.ET_ID, EMP_TIME_DTL.SEQ_NBR, (EMP_TIME_DTL.DATE_ENTERED)
)
SELECT DISTINCT(EMP_CODE), FNAME + ' ' + LNAME AS FULL_NAME, MAX(DATE_ENTERED) AS DATE_ENTERED
from EMP_TIME_CTE
GROUP BY EMP_CODE, FNAME, LNAME
HAVING MAX(DATE_ENTERED) < DATEADD(day, -45, GETDATE())
ORDER BY EMP_CODE asc

Related Links

How to do an aggregated OR on a column in Amazon Redshift
'Your query does not include the specified expression as part of an aggregate function' error for calculated fields
Creating percentile_cont as aggregate function in Vertica
Create Expression in Report Builder 3.0 Report to sum a column
SAP Hana - STRING_AGG issue with ORDER BY CLAUSE
rrdtool does not keep max
Tableau Aggregate Percentage
use aggregates (min, max, avg) in CONSTRUCT query [duplicate]
R: transposing and aggregating columns
SQL Query Aggregate Error
Usage of aggregate functions in MarkLogic
Querying customDimensions in Google Bigquery
NHibernate - QueryOver criteria appearing in Where instead in Having clause, error
Howto aggregate on full data set in Spring Batch jobs?
Creating an aggregate function fails
Necessity of declaration of function in c and cpp

Categories

HOME
asp.net
model-view-controller
url
optimization
netty
ssh
jenkins-pipeline
x86-64
app-store
confluence-rest-api
glympse
vsixmanifest
ionic-native
phpexcel
logstash-configuration
minimum
game-engine
openscad
pylint
xna
android-navigation-drawer
klipfolio
screen-scraping
inline-assembly
connection-pooling
functional-dependencies
poisson
android-xml
utc
stack-trace
tftp
boosting
windows-10-mobile
spock
request-uri
ds-5
ocl
ios9
jmsserializerbundle
exacttarget
visual-studio-debugging
jsonapi-resources
bootcamp
dropdownbox
tunnel
aweber
respect-validation
origami
azure-web-roles
vertical-alignment
directadmin
irr
datagridviewcombobox
text-extraction
python-venv
azure-data-catalog
xmldom
g77
spring-cloud-bus
playframework-1.x
openpgp.js
aws-rds
programming-pearls
multi-targeting
wicketstuff
acceleo
turbolinks
dbi
ldd
foreground
konakart
change-password
kik
mrunit
hana-sql-script
askbot
jts
windows-phone-8-emulator
autoscaling
callfire
log-analysis
grails-3.0.9
waterline
ultraedit
nfa
pysvn
apache-pivot
yosemite
atg-dynamo
nspopupbutton
traceur
sqldatasource
new-operator
slimscroll
node-mongodb-native
adabas
xcode6.1-gm-seed
friendly-id
angularjs-select2
generator-expression
microsoft-speech-platform
motordriver
blackberry-eclipse-plugin
wxperl
virtual-printer
cherokee
symfony-1.2
response-time
mass-emails
carbon-emacs
dgml
morfik
djangoappengine
zend-acl

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App