Links in field document (filename) are broken

Hi, I would like to show a link to a pdf in a grid with a field document(filename). In this grid I need some custom SQL code with joins to show up some data. When I run the grid the links to my pdf docs look fine but lead to an error that the document does not exist.
If I remove the joins to other tables the links work fine.

SELECT
tkapp_user.user_lv,
tkapp_an.an_lv,
tkapp_an.an_n,
tkapp_user.login,
tkapp_ag.an_id,
tkapp_ag.ab_id,
tkapp_user.user_id,
tkapp_ag.pdf_data


FROM
tkapp_ag

INNER JOIN tkapp_an ON tkapp_ag.an_id = tkapp_an.an_id
INNER JOIN tkapp_user ON tkapp_ag.login = tkapp_user.login

WHERE tkapp_ag.login = [login]
AND

tkapp_ag.pdf_data != ""
AND
tkapp_user.user_lv = tkapp_an.an_lv

pdf_data is my document(filename) field that doesn’t work anymore after inserting this custom sql code in my grid. I’m no expert in sql and I built this query with a query builder so I don’t really understand what’s happening but it works and shows my expected data.
Can anyone tell me why the document(filename) field does not work with this query?