web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Regarding complex SQL

(0) ShareShare
ReportReport
Posted on by 351

Hi specialist,

I will create an one custom Enquiry form.

The data based on a 12 month [Jan to Dec and I have a 12 fields in my form.

Note : I'm using temp table in enquiry form.

below the my design sample Complex SQL code

i want to use "case when than" to write a sql statement:

select name,case when month(date)='1' then salary else 0 end as 'Jan',
case when month(date)='2' then salary else 0 end  as 'Feb'
....
....
from MyTable

As  I mentioned above code please give me correct same and i don't know how to use in complex SQL in x .

I know only X without complex sql the same requirement its possible ?

Thanks  

I have the same question (0)
  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    So your requirement is to calculate salary for each month.

    I recommend to create a view that includes the required fields from your table, and a computed column for month.

    Then you can fetch the data with a simple select statement:

    while select sum(salary) from myView
        group by month
    {
    }
    

    Or, make 12 select statements into your table

    select sum(salary) from myTable
        where myTable.Date >= [start date of the month]
            && myTable.Date <= [end date of the month];

  • waytod365 Profile Picture
    351 on at

    Thank you very much for your replay,

    I will follow your way!

    Would mind to specify where to add in while select and 12 select statements.

    Its in computed column ? if so, in computed column how to do 12 month based a salary.?

  • Suggested answer
    nmaenpaa Profile Picture
    101,162 Moderator on at

    Hi waytod365,

    I can't tell where you should put this statement. Remember that we don't know anything about your solution except what you shared with us. I merely illustrated how you can fetch the required data from your table.  But basically you just put it in the exact same place where you would put the statement that you shared in your original question.

    The computed column should contain the number of the month of the Date field. This way you can group and sum by month.

  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    For reference, last week I discussed a similar question in DUG forum: How do I translate SSRS query logic into a Dynamics 365 X++ query.

  • waytod365 Profile Picture
    351 on at

    Thank very much your replay,

    I got it as u mentioned above the thread. still I have an one doubt.

    ["I would create a computed column for each case.

    Each of the method can call the common logic, of course, just with different parameters".]

    Give me a example as I mentioned above your text and I copied and pasted here!

    I'm wondering how to make "Each of the method can call the common logic".

    Would be great If you could specify.

    Thanks.

  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    You will have twelve computed columns, but they all do the same thing. They differ just in two places: the month index (1, 2, ...) and the name (Jan, Feb, ...). Therefore you can create a parametrized method generating T-SQL code and call it with different parameters from individual column methods. For example:

    private static str salaryJanSql()
    {
    	return MyClass::salaryComputedColumn(1, 'Jan');
    }

  • waytod365 Profile Picture
    351 on at

    Thanks for your suggestion sir,

    Your way totally different and I'm interesting to work this way.

    I agreed 12  twelve computed columns.

    Now, I will come to the topic what I need.

    Below image the original output design.

    image_2D00_20210628_2D00_110541.png

    As i mentioned above the picture Jan to Dec calculate total amount.

    Now, shall i create Jan to Dec fields as real data type label should be 'Jan'.

    If its not recommend you to achieve my requirement, what is your thought sir. ? 

    [salaryComputedColumn]

    In this class what should i wrote the inside and give me small example for that plus T-SQL also.

    Once you free let me know sir i don't wanna waste your time, I learn one thing about your replay.:)

    Thanks for your attention.

  • Verified answer
    Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    A computed column generates a piece of T-SQL code, therefore the first step is designing the T-SQL code that you want to generate. Only then write X code to actually generate it.

    In the other thread, the goal was summarizing data from several records (based on a condition). Therefore the T-SQL code would be something like this:

    select sum(Weight) from Inventory
    	where ...

    I don't know your actual requirements here - the code that you showed us above doesn't even aggregate data in any way.

    If you want to sum amount by grouped by month and put it into a temporary table, follow  Nikolaos's suggestion. Create a computed column for the month number, group by it, iterate records and put individual values to the right fields of your temporary buffer.

    Using the twelve computed columns allow you to eliminate the temporary table - you'll simply put the view to a form and all data will be fetched directly from database. I can't say whether it's useful in your particular scenario or not.

    The T-SQL code to generate would look like this:

    select sum(salary) from MyTable
    	where month(date) = ...

    Which can be generated by code like this:

    static str salarySql(int _monthIdx)
    {
    	str pattern = select sum(%1) from %2 where month(%3) = %4;
    	str salaryField = ...
    	str tableName = ...
    	str dateField = ...
    
    	return strFmt(pattern, salaryField, tableName, dateField, _monthIdx);
    }

    I see that you used values like 'Jan' just as field aliases, which we don't need, therefore the month index is the only necessary parameter. You would use field labels to provide user-friendly names for the view fields.

  • waytod365 Profile Picture
    351 on at

    Thank you very much your answer!

    I learnt and its worthwhile for me :)

    Thanks for your time sir. :)

  • waytod365 Profile Picture
    351 on at

    Sorry for the late replay sir,

    I understand in your words , but I'm confusing the below code,

    static str salarySql(int _monthIdx)
    {
    	str pattern = select sum(%1) from %2 where month(%3) = %4;
    	str salaryField = ...
    	str tableName = ...
    	str dateField = ...
    
    	return strFmt(pattern, salaryField, tableName, dateField, _monthIdx);
    }

    My view table name : TEL_EarningSummary

    Fields : Description , Jan to Dec , Total salary.

    14 fields in my table. 

    Now, As i mentioned above code is new class and new method am i right ?

    Then i call that method in my computed column Am i right ?

    How to select [pattern , salaryfield, tableName, fieldName

    Please give me the example in [pattern , salaryfield, tableName, fieldName.

    Thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans