you could also write the query this way
select distinct f.ReferrerSearchPhrase,
(
select COUNT(f1.ReferrerSearchPhrase)
from FromSession f1
where f1.ReferrerSearchPhrase = f.ReferrerSearchPhrase
) as Landed,
(
select COUNT(f1.Converted)
from FromSession f1
where f1.ReferrerSearchPhrase = f.ReferrerSearchPhrase
and (f1.Converted = 1)
) as Converted
from FromSession
where Created between @start and @end
I would then save Converted / Landed * 100 for the business logic. if you use a dynamic list it would look something like this
IEntityField2 Landed= new EntityField2("Landed", new ScalarQueryExpression(FromSessionFields.ReferrerSearchPhrase.SetAggregateFunction(AggregateFunction.Count), (FromSessionFields.ReferrerSearchPhrase == FromSessionFields.ReferrerSearchPhrase)));
IEntityField2 Converted = new EntityField2("Converted ", new ScalarQueryExpression(FromSessionFields.Converted .SetAggregateFunction(AggregateFunction.Count), (FromSessionFields.ReferrerSearchPhrase == FromSessionFields.ReferrerSearchPhrase) & (FromSessionFields.Converted == 1)));
IEntityField2 Precent = new EntityField2("Precentage", (Converted / Landed * 100));
ResultSet fields = new ResultSet(4);
fields.Define(FromSessionFields.ReferrerSearchPhrase, 0);
fields.Define(Landed, 1);
fields.Define(Converted, 2);
fields.Define(Precent , 3);
DateTime start = DateTime.Today;
DateTime stop = DateTime.Today.AddMonths(1);
IRelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.PredicateExpression.Add(new BetweenFieldPredicate(FromSessionFields.Created, start, stop);
DataTable resutls = new DataTable();
using(IDataAccessAdapter adapter = new DataAccesAdapter())
{
//fields, data, filter, max rows, sort, distinct records.
adpater.FetchTypedList(fields, results, bucket, 0, null, true);
}
if the precentage column doesn't work drop the 4th result set field and after fetched create an expression on the datatable like this.
ResultSet fields = new ResultSet(3);
fields.Define(FromSessionFields.ReferrerSearchPhrase, 0);
fields.Define(Landed, 1);
fields.Define(Converted, 2);
...
DataTable resutls = new DataTable();
using(IDataAccessAdapter adapter = new DataAccesAdapter())
{
//fields, data, filter, max rows, sort, distinct records.
adpater.FetchTypedList(fields, results, bucket, 0, null, true);
}
results.Columns.Add("Precent", typeof(float), "[Converted] / [Landed] * 100");
if nothing else this should get you real close