简体   繁体   中英

MS Access 2010 - FIRST function in Access what is it in SQL

I am running a script in SQL server from MS Access and not getting the correct results due to FIRST function in MS Access

Select ClientID, ClientREF, FIRST(AgentID) AS FirstOFAgentID, FIRST(AgentREF) AS FirstOFAgentREF
From table 1
Right join table2 ON table1 = table2
Left join table 4 ON table1 = table4
Group by ClientID, ClienREF, AgentID, AgentREF

Your query doesn't make sense. You have FIRST(AgentID) , but are including AgentId in the GROUP BY . You query is equivalent to simply SELECT DISTINCT with your columns.

Presuming you really want a first value, I can readily think of in SQL Server, both using window functions. I prefer conditional aggregation:

select ClientID, ClientREF,
       max(case when seqnum = 1 then AgentID end) AS FirstOFAgentID, F      
       max(case when seqnum = 1 then AgentREF end) AS FirstOFAgentREF
from (select ClientID, ClientREF, AgentID, AgentREF,
             row_number() over (partition by ClientID, ClientREF order by ?) as seqnum
      from table 1 Right join
           table2
           ON table1 = table2 Left join
           table 4
           ON table1 = table4
     ) cc   
group by ClientID, ClienREF;

The second uses SELECT DISTINCT with FIRST_VALUE() , which is provided as a window function but not an aggregation function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM