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

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
Calculate window average in tableau
Complexity asymptotic relation (theta, Big O, little o, Big Omega, little omega) between functions
What are TOP_COUNT and TOP_MAXCOUNT in BigQuery?

Categories

HOME
service
postgresql-9.3
xamarin.forms
file-upload
glsl
readdir
app-store
powerquery
cdn
rmarkdown
elastalert
osclass
guidewire
xmlhttprequest
responsive
md2
url-parameters
descriptor
monad-transformers
serenity-js
dev-c++
fastboot
onclicklistener
android-xml
spring-data-gemfire
undo
ibm-bpm
http-proxy
incapsula
tunnel
ansible-inventory
android-databinding
yelp
const
linkurious
android-graphview
rexx
amazon-rds-aurora
f#-fake
ack
hdpi
nao-robot
monaca
try-catch-finally
was
computed-properties
mongodb-3.3
class-design
geokit
init
vitamio
hotkeys
aws-kms
database-administration
alchemy
markerspiderfier
webservices-client
bootstrap-tabs
eaaccessory
stereotype
page-refresh
data-representation
system.diagnostics
avd
jad
indices
iphone-6
random-access
novell
xmlserializer
zoneminder
serializer
triplestore
visual-c#-express-2010
image-preloader
object-oriented-database
git-subtree
mixradio
starling-framework
mongoid4
adabas
mysql-error-1146
ardor3d
scidb
sygic
infomaker
dd4t
removeall
pageload
blotter
database-restore
yii-components
shapado
certificate-revocation
ios6-maps
django-1.4
mysql-connector
win-prolog
xhtmlrenderer
datarelation
pitch-shifting
hp-trim
calendarextender
retrospectiva

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