How do I return a value of an entity in a table that is less than but closest to the value in another table for each element in the last table in SQL?

感情迁移 提交于 2019-12-02 03:47:22

I would approach this type of query using a correlated subquery. I think the following words in Access:

SELECT jc.ClassFile,
       (select top 1 ml.Module
        from ModuleList as ml
        where ml.[Order] < jc.[Order]
       )
FROM JavaClassFileList as jc;

I'm assuming Order is unique for Module. If it isn't, JavaClassFileRecords may show up multiple times in the resultset.

If no module can be found for a JavaClassFile then it will not show up in the results. If you do want it to show up in cases like that (with a null module), replace INNER JOIN with LEFT OUTER JOIN.

SELECT j.ClassFile, m.Module
FROM JavaClassFileList j
INNER JOIN ModuleList m
ON m.Order =
    (SELECT MAX(Order)
     FROM ModuleList
     WHERE Order < j.Order)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!