W.E.B
asked on
SQL MAX Date
Hello,
Can you please help,
I'm using below statement.
I'm having a hard time selecting only the last ChqDate per Employee.
SELECT DISTINCT "eeRegKeys"."eeEEnum","eeE mployee"." LastName", "eeEmployee"."FirstName", "erEmployer"."ERnum","RegD ata"."RegV alue",
MAX(CONVERT(Datetime,"Regi ster"."Chq Date",1)) AS [ChqDate],"RegTable"."CurY TD"
FROM ((((("erEmployer" "erEmployer" INNER JOIN "eeRegKeys" "eeRegKeys" ON "erEmployer"."ERnum"="eeRe gKeys"."ee ERnum") INNER JOIN "eeEmployee" "eeEmployee" ON "eeRegKeys"."eeLink"="eeEm ployee"."e eLink") INNER JOIN "stmUserSecurity" "stmUserSecurity" ON "eeRegKeys"."eeLink"="stmU serSecurit y"."eeLink ") INNER JOIN "RegData" "RegData" ON "eeEmployee"."eeLink"="Reg Data"."eeL ink") INNER JOIN "RegTable" "RegTable" ON "RegData"."RegCode"="RegTa ble"."RegC ode") INNER JOIN "Register" "Register" ON "RegData"."RegLink"="Regis ter"."RegL ink"
WHERE "eeEmployee"."idCalendar"> =0 AND "eeRegKeys"."EndDate" IS NULL AND "erEmployer"."EndDate" IS NULL AND "RegTable"."EndDate" IS NULL AND "Register"."ERnum" ='2783' AND ("RegTable"."PayCode"='GRO ') AND "stmUserSecurity"."idUser" =1 AND "Register"."EndDate" IS NULL AND "RegData"."EndDate" IS NULL AND "eeEmployee"."EndDate" IS NULL AND "RegTable"."CurYTD" LIKE 'YTD' AND (CONVERT(Datetime,"Registe r"."ChqDat e",1)) > = '2014-01-01' and "eeRegKeys"."eeEEnum" like '150%'
GROUP BY "eeRegKeys"."eeEEnum","eeE mployee"." LastName", "eeEmployee"."FirstName", "erEmployer"."ERnum", "RegTable"."PayCode","RegD ata"."RegV alue", "Register"."ChqDate","RegT able"."Cur YTD"
ORDER BY "eeRegKeys"."eeEEnum","Reg ister"."Ch qDate"
Thank you,
Can you please help,
I'm using below statement.
I'm having a hard time selecting only the last ChqDate per Employee.
SELECT DISTINCT "eeRegKeys"."eeEEnum","eeE
MAX(CONVERT(Datetime,"Regi
FROM ((((("erEmployer" "erEmployer" INNER JOIN "eeRegKeys" "eeRegKeys" ON "erEmployer"."ERnum"="eeRe
WHERE "eeEmployee"."idCalendar">
GROUP BY "eeRegKeys"."eeEEnum","eeE
ORDER BY "eeRegKeys"."eeEEnum","Reg
Thank you,
sorry just noticed something.
What is the actual data type Register.ChqDate?
What is the actual data type Register.ChqDate?
ASKER
Hello,
Register.ChqDate
APR 07 2014
FEB 07 2014
---
thanks
Register.ChqDate
APR 07 2014
FEB 07 2014
---
thanks
I am assuming that field is a date or datetime and does not need converting for use in date logic.
I think you need something like row_number() to get only the latest cheque.
I think you need something like row_number() to get only the latest cheque.
SELECT
eeRegKeys.eeEEnum
, eeEmployee.LastName
, eeEmployee.FirstName
, erEmployer.ERnum
, RegData.RegValue
, CONVERT(varchar(10), MAX(Register.ChqDate), 120) AS [ChqDate]
, RegTable.CurYTD
FROM erEmployer erEmployer
INNER JOIN eeRegKeys eeRegKeys
ON erEmployer.ERnum = eeRegKeys.eeERnum
INNER JOIN eeEmployee eeEmployee
ON eeRegKeys.eeLink = eeEmployee.eeLink
INNER JOIN stmUserSecurity stmUserSecurity
ON eeRegKeys.eeLink = stmUserSecurity.eeLink
INNER JOIN RegData RegData
ON eeEmployee.eeLink = RegData.eeLink
INNER JOIN RegTable RegTable
ON RegData.RegCode = RegTable.RegCode
INNER JOIN (
SELECT
RegLink
, ERnum
, EndDate
, ChqDate
, ROW_NUMBER() OVER (PARTITION BY reglink ORDER BY ChqDate DESC) AS RN
FROM Register
) Register
ON RegData.RegLink = Register.RegLink
AND RN = 1
WHERE eeEmployee.idCalendar >= 0
AND eeRegKeys.EndDate IS NULL
AND erEmployer.EndDate IS NULL
AND RegTable.EndDate IS NULL
AND Register.ERnum = '2783'
AND (RegTable.PayCode = 'GRO')
AND stmUserSecurity.idUser = 1
AND Register.EndDate IS NULL
AND RegData.EndDate IS NULL
AND eeEmployee.EndDate IS NULL
AND RegTable.CurYTD LIKE 'YTD'
AND Register.ChqDate >= '20140101'
AND eeRegKeys.eeEEnum LIKE '150%'
GROUP BY
eeRegKeys.eeEEnum
, eeEmployee.LastName
, eeEmployee.FirstName
, erEmployer.ERnum
, RegTable.PayCode
, RegData.RegValue
, Register.ChqDate
, RegTable.CurYTD
ORDER BY
eeRegKeys.eeEEnum
, Register.ChqDate
ASKER
HI Paul,
Still getting all dates., all values.
thanks
Still getting all dates., all values.
thanks
@Wass_QA
It is important to know the actual data type of that field (not just what it looks like )
This will list the needed details (in COLUMN_NAME &TYPE_NAME)
exec sp_columns Register
It is important to know the actual data type of that field (not just what it looks like )
This will list the needed details (in COLUMN_NAME &TYPE_NAME)
exec sp_columns Register
ASKER
Register ChqDate char
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
CHAR
Register.ChqDate
APR 07 2014
FEB 07 2014
and in an unsortable format. good grief.
Then you will have to convert that field to date (no much point converting to datetime)
It's really not a good idea to store dates as strings, especially strings based on a language
Register.ChqDate
APR 07 2014
FEB 07 2014
and in an unsortable format. good grief.
Then you will have to convert that field to date (no much point converting to datetime)
It's really not a good idea to store dates as strings, especially strings based on a language
ASKER
HI Paul,
I don't own this program,
I'm just trying to get some reports out of it.
I tried your statement,
I'm getting one value per Employee, but the dates are from 2008, 2009,...
thanks
I don't own this program,
I'm just trying to get some reports out of it.
I tried your statement,
I'm getting one value per Employee, but the dates are from 2008, 2009,...
thanks
You have to go back to converting that field into dates
line 44 should read:
AND convert(date,Register.ChqD ate,1) >= '20140101'
line 44 should read:
AND convert(date,Register.ChqD
ASKER
I've requested that this question be closed as follows:
Accepted answer: 0 points for Wass_QA's comment #a40282176
for the following reason:
Thank you Paul,
I used my convert datetime.
it's working,
Thanks again.
Accepted answer: 0 points for Wass_QA's comment #a40282176
for the following reason:
Thank you Paul,
I used my convert datetime.
it's working,
Thanks again.
No problem, Glad I could help. Do you really intend to award yourself the solution? To be honest that isn't the case here.
The actual solution to your problem is the use of ROW_NUMBER() at HTTP:#a40282167
(The conversion to date or datetime is a side-issue because I thought it ChqDate would logically be date or datetime.)
The actual solution to your problem is the use of ROW_NUMBER() at HTTP:#a40282167
(The conversion to date or datetime is a side-issue because I thought it ChqDate would logically be date or datetime.)
ASKER
oops,
sorry.
sorry.
Open in new window