Skip to content
Advertisement

Translate Excel business logic to T-SQL

I need to ‘translate’ some business logic from Excel to T-SQL, I’m having a hard time with it.

It’s about figures from gates that count how many customers go IN and OUT of the stores.

All DATA you need is in the following table:

‘RESULT’ is the column that should be calculated, based on ‘countersOUT and ‘countersIN’.

‘countersOUT and ‘countersIN’ is the INPUT data you need for the calculation.

The business user made a help column in Excel to make the calculation (column AA) . . . . . . .From business point of view: this is the number of customers at the end of each half hour present in the shop.

enter image description here

Then, the actual calculation below: (screenshot also from Excel)

Note: the calculation makes use of the help column (col. AA) enter image description here

Now my task is to make over this calculation in T-SQL.

The business user its only input data is ‘countersIN’ and ‘countersOUT’, in other words it should be achievable in T-SQL. Only, I don’t manage, that’s why I came to ask my question.

Lastly, I can advise to have a look at the Excel file (mediafire link) http://www.mediafire.com/file/mtdvlgmmbj3f8dd/Example_20190725_SQLforum.xlsx/file

Thanks a lot in advance for any help

Advertisement

Answer

Here’s a solution using a Cursor. No good style but effective, since you move through the rows. The attribut [RESULT_by_Cursor] is the computed one in comparison to your target value.

Btw: Your SQL-example has an error, the in and out – columns are twisted.


User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement