1

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

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

BQL 是否能够像在 SQL 中一样获取 DATEPART 并按该 DATEPART 结果分组?

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

4

1 回答 1

4

我希望这对你有用:

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。

于 2015-01-01T21:50:18.383 回答