SQL to QuerySpec

Posts   
 
    
MPR Tech
User
Posts: 2
Joined: 06-Aug-2021
# Posted on: 20-Apr-2022 20:57:49   

Is it possible to create an expression in QuerySpec (Self Servicing) that is equivalent to the SQL below?

SELECT      COUNT(*) 
FROM        PROJECT p
WHERE       CAST(p.LastReportSendDateUTC as DATE) = (SELECT CAST(MAX(LastReportSendDateUTC) AS Date) FROM PROJECT) 
AND         p.LastReportSendSuccessful = 1

Additional Info: PROJECT.LastReportSendDateUTC as a DateTime in SQL Server

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 21-Apr-2022 10:09:00   

This is for using it as a scalar expression in a larger query, or as a standalone scalar query?

Something like this?

var qf = new QueryFactory();
var q = qf.Project
          .Where(ProjectFields.LastReportSendDateUTC.Date().Equal(qf.Project.Select(ProjectFields.LastReportSendDateUTC.Max().Date()).ToScalar())
                            .And(ProjectFields.LastReportSendSuccessful.Equal(1)))
          .Select(Functions.CountRow());
var result = new TypedListDAO().GetScalar<int>(q);

Be aware that the Date() function call produces complex DateAdd code. If you want to use a DATE() call, you have to define the function mapping for that yourself using a custom function mapping. This is easy: https://www.llblgen.com/Documentation/5.9/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/QuerySpec/gencode_queryspec_functionmappings.htm

Frans Bouma | Lead developer LLBLGen Pro