1
I did this query, which will compose a view.
SELECT DISTINCT
US.EMPLOYEESTATUS
,US.EMPLOYEEID
,US.EMPLOYEEFIRSTNAME + ' ' + US.EMPLOYEEMIDDLENAME + ' ' + US.EMPLOYEELASTNAME AS FULLNAME
,US.GRADE
,SO.SOLICITATIONID
,SO.DATEFINANCING
,SO.MANUFACTURER
,SO.MODEL
,SO.CHASSI
,SO.VALUEPROPERTY
,SO.VALUEGRANTED
,SO.STATUSSOLICITATION
,PY.PAYMENTSTATUS
,PY.VALUEPAYMENT
,PY.DATEPAYMENT
,MAX(PY.NUMBERSEQUENCE) AS SEQUENCE
,MAX(PY.DATEPAYMENT) AS DATEPEYMENT
FROM
[dbo].[USER] US
INNER JOIN SOLICITATION SO ON US.USERID = SO.USERID
INNER JOIN PAYMENTS PY ON US.USERID = PY.USERID AND PY.SOLICITATIONID = SO.SOLICITATIONID
WHERE
PY.PAYMENTSTATUS IN('QT', 'PG')
GROUP BY
US.EMPLOYEESTATUS
,US.EMPLOYEEID
,US.EMPLOYEEFIRSTNAME
,US.EMPLOYEEMIDDLENAME
,US.EMPLOYEELASTNAME
,US.GRADE
,SO.SOLICITATIONID
,SO.DATEFINANCING
,SO.MANUFACTURER
,SO.MODEL
,SO.CHASSI
,SO.VALUEPROPERTY
,SO.VALUEGRANTED
,SO.STATUSSOLICITATION
,PY.PAYMENTSTATUS
,PY.VALUEPAYMENT
,PY.DATEPAYMENT
Note that by the screenshot above, the solicitationid number 7378 and 7380, if repeated 3 times, this is because it has 3 different payment dates. I tried to give a MAX(PY.DATEPAYMENT)
, but that didn’t solve, as you can see in the query. The question is:
As I do to bring in this query, a record of each and in the Payments table I always trzer the last paid date(PG) or quitada(QT). The latter will be only one. The result should be 7 records and not 11, as is.
There in your script you used
MAX(PY.DATEPAYMENT)
but did not remove the same from theGROUP BY
... And there’s another column that could be a problemSEQUENCE
, but with this field you did right, usedMAX(PY.NUMBERSEQUENCE) AS SEQUENCE
and pulled him out of theGROUPBY
– Matheus Ribeiro
You’re right. When I added Datepayment I forgot to remove it from group by and select. Reply to this comment, I mark your reply.
– pnet
Good that I helped you! And keep making well organized scripts so, rare to find such scripts here! This helps a lot when it comes to giving a help!
– Matheus Ribeiro