AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 12.04.2021, 19:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,459 / 846 (79) +++++++
Регистрация: 28.10.2006
Switch in View Computed column
Источник: http://alexvoy.blogspot.com/2021/04/...ed-column.html
==============

If you need to compare multiple fields in your view while populating a computed column, take into consideration that the standard implementation of method SysComputedColumn::switch uses a map enumerator.



public static client server str switch(str _controlExpression, Map _comparisonExpressionMap, str _defaultExpression)
{
MapEnumerator mapEnumerator;
str caseExpression = '';

caseExpression ='CASE ' + _controlExpression;
mapEnumerator = _comparisonExpressionMap.getEnumerator();
while (mapEnumerator.moveNext())
{
caseExpression += ' WHEN ' + mapEnumerator.currentKey() + ' THEN ' + mapEnumerator.currentValue();
}
caseExpression += ' ELSE ' + _defaultExpression;
caseExpression += ' END';

return caseExpression;
}



It means that your given order will be replaced in final SQL clause by alphabetical one of your keys.



CASE
WHEN (T4.PROJID1) != ('') THEN T4.PROJNAME1
WHEN (T4.PROJID2) != ('') THEN T4.PROJNAME2
WHEN (T4.PROJID3) != ('') THEN T4.PROJNAME3
ELSE T4.PROJNAME END



For example, we want to populate field mgcProjInvoiceGLTransView.ParentProjectName for a given project id up to three level up in the project hierarchy. Let's assume that they are prepopulated in mgcProjInvoiceOnAccView view: projId1 is the parent of projId, projId2 is the parent of projId1, and so on.

Once we reference this computed column to the following method, we will get a new order in CASE construction on the SQL side.



private static str projNameParent()
{
str ret;
SysComputedColumnBase::switch cannot keep a given order while using Map enumerator; so we put the SQL string as a string constant
tableName viewName = identifierStr(mgcProjInvoiceGLTransView);
tableName tableName = identifierStr(mgcProjInvoiceOnAccView);
str compareValue = '';


Map comparisonExpressionMap = SysComputedColumn::comparisionExpressionMap();
str fieldNameProjName3 = SysComputedColumn::returnField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjName3));
str fieldNameProjName2 = SysComputedColumn::returnField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjName2));
str fieldNameProjName1 = SysComputedColumn::returnField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjName1));
str fieldNameProjName = SysComputedColumn::returnField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjName));

comparisonExpressionMap.insert(
SysComputedColumn::notEqualExpression(
SysComputedColumn::comparisonField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjId3)),
SysComputedColumn::comparisonLiteral(compareValue)),
fieldNameProjName3);

comparisonExpressionMap.insert(
SysComputedColumn::notEqualExpression(
SysComputedColumn::comparisonField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjId2)),
SysComputedColumn::comparisonLiteral(compareValue)),
fieldNameProjName2);

comparisonExpressionMap.insert(
SysComputedColumn::notEqualExpression(
SysComputedColumn::comparisonField(viewName, tableName, fieldStr(mgcProjInvoiceOnAccView, ProjId1)),
SysComputedColumn::comparisonLiteral(compareValue)),
fieldNameProjName1);

ret = SysComputedColumn::switch(
'',
comparisonExpressionMap,
fieldNameProjName);
return ret;
}



Of course, it always returns the name of the parent on the first level, even though it had its own parent, which is incorrect result.

Therefore, the easiest way is to replace the aforementioned construction with a simple string.

private static str projNameParent()
{
str ret;
ret = "CASE WHEN (T4.PROJID3) != ('') THEN T4.PROJNAME3 WHEN (T4.PROJID2) != ('') THEN T4.PROJNAME2 WHEN (T4.PROJID1) != ('') THEN T4.PROJNAME1 ELSE T4.PROJNAME END";
return ret;
}





Источник: http://alexvoy.blogspot.com/2021/04/...ed-column.html
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
Computed column for union values from multiple outer joined data sources in view Blog bot DAX Blogs 0 06.03.2019 21:13
View computed column to get OR for two flags Blog bot DAX Blogs 0 31.01.2019 03:15
Запретить пересчет computed column у View kitty DAX: Программирование 4 30.01.2018 12:40
dynamicsaxbi: Better together: Microsoft Dynamics AX 2012 R2 and SQL Server Power View Blog bot DAX Blogs 0 12.12.2012 13:11
daxmusings: Computed View Columns in AX 2012 Blog bot DAX Blogs 5 19.10.2011 13:55
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 17:59.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.