BQL是否允许DatePart?

时间:2014-12-31 21:18:32

标签: acumatica

我有一个bql聚合查询,我希望按年和月分组。 这在SQL中很简单,但在BQL中却不那么明显。

表(DAC)中有一个日期字段。 如果我单独按日期字段分组,它基本上给我每个单独的记录。 我想对日期中的年份进行分组。

BQL是否能够像在DATEPART结果中一样抓取DATEPART在SQL和组中?

我看到它有一个约会函数,但没有关于我想做什么的文档。

1 个答案:

答案 0 :(得分:2)

我希望这适合你:

public sealed class DatePart
{
    public const string Day = "dd";
    public const string Hour = "hh";
    public const string Minute = "mi";
    public const string Month = "mm";
    public const string Year = "yyyy";
    public const string Second = "ss";
    public const string Millisecond = "ms";

    public class day : Constant<string> { public day() : base(Day) { } }
    public class hour : Constant<string> { public hour() : base(Hour) { } }
    public class minute : Constant<string> { public minute() : base(Minute) { } }
    public class month : Constant<string> { public month() : base(Month) { } }
    public class year : Constant<string> { public year() : base(Year) { } }
    public class second : Constant<string> { public second() : base(Second) { } }
    public class millisecond : Constant<string> { public millisecond() : base(Millisecond) { } }
}


public sealed class DatePart<UOM, Operand> : BqlFunction, IBqlOperand, IBqlCreator
    where Operand: IBqlOperand
    where UOM : Constant<string>, new()
{
    private IBqlCreator _operand;

    public void Verify(PXCache cache, object item, List<object> pars, ref bool? result, ref object value)
    {
        value = null;
        object date1;
        if (!getValue<Operand>(ref _operand, cache, item, pars, ref result, out date1) || date1 == null) return;


        DateTime period = Convert.ToDateTime(date1);
        switch ((string)new UOM().Value)
        {
            case DatePart.Day:
                value = Convert.ToInt32(period.Day);
                break;
            case DatePart.Hour:
                value = Convert.ToInt32(period.Hour);
                break;
            case DatePart.Minute:
                value = Convert.ToInt32(period.Minute);
                break;
            case DatePart.Second:
                value = Convert.ToInt32(period.Second);
                break;
            case DatePart.Millisecond:
                value = Convert.ToInt32(period.Millisecond);
                break;
            case DatePart.Month:
                value = Convert.ToInt32(period.Month);
                break;
            case DatePart.Year:
                value = Convert.ToInt32(period.Year);
                break;
        }
    }


    public void Parse(PXGraph graph, List<IBqlParameter> pars, List<Type> tables, List<Type> fields,
                      List<IBqlSortColumn> sortColumns, StringBuilder text, BqlCommand.Selection selection)
    {
        if (graph != null && text != null)
        {
            text.Append(" DATEPART(").Append((string)new UOM().Value).Append(", ");
            parseOperand<Operand>(ref _operand, graph, pars, tables, fields, sortColumns, text, selection);
            text.Append(")");
        }
        else
        {
            parseOperand<Operand>(ref _operand, graph, pars, tables, fields, sortColumns, text, selection);
        }
    }
}     

注意:这是针对MsSQL的,并且不适用于mySQL。