Calculating sum of date differences

Posts   
 
    
zra
User
Posts: 3
Joined: 08-Feb-2011
# Posted on: 08-Feb-2011 02:08:49   

Hi All,

I have a question about how to perform the following in llblgen - this is probably a simple quesiton but cannot see how to approach this.

I have a table that contains a start datetime and an end datetime. I would like to calculate the sum of the differences of these values. The end result would be a value of duration.

How can this be achieved?

Thanks!

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Feb-2011 08:35:50   

Would you please write down the SQL query you want to produce.

zra
User
Posts: 3
Joined: 08-Feb-2011
# Posted on: 09-Feb-2011 23:31:29   

select sum(datediff(MINUTE, start_timestamp, end_timestamp)) from cuda_run group by datediff(MINUTE, start_timestamp, end_timestamp)

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Feb-2011 05:29:13   

Here is an approximate code (assuming you are using Adapter):

EntityField2 dateDiffField = new EntityField2("GlobalDiff",
    new DbFunctionCall("DATEDIFF(MINUTE, {0}, {1})",
        new object[] { OrderFields.OrderDate, OrderFields.ShippedDate}));

int globalDiff = 0;
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    object result = adapter.GetScalar(dateDiffField, AggregateFunction.Sum);
    globalDiff = (result == null) ? 0 : (int) result;               
} 
David Elizondo | LLBLGen Support Team
zra
User
Posts: 3
Joined: 08-Feb-2011
# Posted on: 14-Feb-2011 01:11:18   

daelmo wrote:

Here is an approximate code (assuming you are using Adapter):

EntityField2 dateDiffField = new EntityField2("GlobalDiff",
    new DbFunctionCall("DATEDIFF(MINUTE, {0}, {1})",
        new object[] { OrderFields.OrderDate, OrderFields.ShippedDate}));

int globalDiff = 0;
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    object result = adapter.GetScalar(dateDiffField, AggregateFunction.Sum);
    globalDiff = (result == null) ? 0 : (int) result;               
} 

This looks good - what I was after, however, am actually using Self Service. How do you get the field index for the newly added field "dateDiffField"?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Feb-2011 09:54:02   

Here is the code in SelfServicing:

            var field = new EntityField("CalculatedField", new DbFunctionCall("DATEDIFF(MINUTE, {0},{1})", new Object[] { OrderFields.OrderDate, OrderFields.ShippedDate }));

            var fields = new ResultsetFields(1);
            fields.DefineField(field, 0, AggregateFunction.Sum);

            var groupBy = new GroupByCollection(field);

            var results = new DataTable();
            var dao = new TypedListDAO();
            dao.GetMultiAsDataTable(fields, results, 0, null, null, null, true, groupBy, null, 0, 0);

This returns a dataTable of multiple rows and a single column/field.