Skip to content
Advertisement

How to get the sum of values in a table interval?

The question is very simple :-). I’m a beginner.

tables

Result: enter image description here

A task….. There are two date variables.

How to get the sum of values(qty) between dates(@startDate – @endDate).

How to get the sum of values(qty) up to @startDate.

How get the sum of values(qty) down to @endDate.

If DocumentType is 1. Then the value(qty) minus.

Advertisement

Answer

Looks like you need conditional aggregation SUM(CASE WHEN....

CROSS APPLY also makes it easier to pre-calculate the negative qty

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