Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex DataLens
  • Getting started
  • Practical guidelines
    • All tutorials
    • Visualizing data from a CSV file
    • Creating and publishing a chart with a map of Moscow from a CSV file
    • Analyzing a retail chain's sales from a ClickHouse database
    • Analyzing public data on road accidents in Russia
    • Analyzing sales and locations of pizzerias based on data from ClickHouse DB and Marketplace
    • Web analytics with a connection to Yandex Metrica
    • Web analytics with funnels and cohorts calculated based on Yandex Metrica data
    • Mobile app analytics based on AppMetrica data
    • Analyzing Yandex Music podcast statistics (for podcasters)
    • Visualizing data with a QL chart
    • Visualizing data using parameters
    • Visualizing Yandex Monitoring data
    • Building customer journey charts based on AppMetrica data
  • Concepts
    • Service overview
    • Organizations in DataLens
    • Connection
    • Data types
    • Datasets
      • Overview
      • Data model
      • Dataset settings
    • Charts
      • Overview
      • Chart settings
      • Measure Values and Measure Names
    • Dashboards
    • Joining data
    • Using Markdown in DataLens
    • DataLens Public
    • Calculated fields
      • Overview
      • Formula syntax
    • Parameterization
    • Marketplace
    • Backups
    • Caching
    • Quotas and limits
  • Step-by-step instructions
    • All instructions
    • Working with connections
      • Creating a ClickHouse connection
      • Creating a connection to a file
      • Creating a Google Sheets connection
      • Creating a MySQL connection
      • Creating a PostgreSQL connection
      • Creating a SQL Server connection
      • Creating a BigQuery connection
      • Creating an Oracle Database connection
      • Creating a YDB connection
      • Creating a Greenplum® connection
      • Creating a Prometheus connection
      • Creating a Yandex Monitoring connection
      • Creating a Yandex Metrica API connection
      • Creating an AppMetrica connection
      • Creating a Yandex Cloud Billing connection
      • Managing connection access
    • Working with datasets
      • Creating a dataset
      • Combining data from multiple tables
      • Creating a data field
      • Creating a calculated data field
      • Creating a default filter for new charts
      • Updating fields in datasets
      • Describing a dataset via a source SQL query
      • Managing dataset access
      • Managing access to data rows
      • Adding parameters to a dataset
    • Working with charts
      • Creating a chart
      • Creating a QL chart
      • Creating a multi-dataset chart
      • Adding a hierarchy
      • Configuring the navigator
      • Publishing a chart
      • Managing chart access
      • Adding ID as a parameter
      • Adding parameters to a chart
      • Configuring the display of null values
    • Working with dashboards
      • Creating dashboards
      • Adding charts to dashboards
      • Adding selectors to dashboards
      • Creating an alias
      • Deleting an alias field
      • Publishing dashboards
      • Managing dashboard access
      • Adding parameters to a dashboard
      • Dashboard auto-update
      • Widget display order on a mobile device
    • Working with permissions
      • Granting permissions
      • Deleting permissions
      • Request permissions
    • Working with DataLens Marketplace
      • Adding a product from Marketplace
      • Removing a product from Marketplace
      • Creating and connecting a connector (for partners)
    • Working with organizations
      • Switching between DataLens instances
  • Access management
    • Managing access to DataLens
    • Managing access at the data row level
  • Pricing policy
  • Visualization reference
    • All visualizations
    • Line chart
    • Stacked area chart
    • Normalized stacked area chart
    • Column chart
    • Normalized column chart
    • Bar chart
    • Normalized bar chart
    • Scatter chart
    • Pie chart
    • Indicator
    • Tree chart
    • Donut chart
    • Table
    • Pivot table
    • Map
      • Overview
      • Point map
      • Point map with clusters
      • Polyline map
      • Choropleth map
      • Heat map
    • Combined chart
  • Function reference
    • All Functions
    • Aggregate functions
      • Overview
      • ALL_CONCAT
      • ANY
      • ARG_MAX
      • ARG_MIN
      • AVG
      • AVG_IF
      • COUNT
      • COUNTD
      • COUNTD_APPROX
      • COUNTD_IF
      • COUNT_IF
      • MAX
      • MEDIAN
      • MIN
      • QUANTILE
      • QUANTILE_APPROX
      • STDEV
      • STDEVP
      • SUM
      • SUM_IF
      • TOP_CONCAT
      • VAR
      • VARP
    • Array functions
      • Overview
      • ARRAY
      • ARR_AVG
      • ARR_MAX
      • ARR_MIN
      • ARR_PRODUCT
      • ARR_REMOVE
      • ARR_STR
      • ARR_SUM
      • CAST_ARR_FLOAT
      • CAST_ARR_INT
      • CAST_ARR_STR
      • CONTAINS
      • COUNT_ITEM
      • GET_ITEM
      • REPLACE
      • SLICE
      • STARTSWITH
      • UNNEST
    • Date/Time functions
      • Overview
      • DATEADD
      • DATEPART
      • DATETRUNC
      • DAY
      • DAYOFWEEK
      • HOUR
      • MINUTE
      • MONTH
      • NOW
      • QUARTER
      • SECOND
      • TODAY
      • WEEK
      • YEAR
    • Logical functions
      • Overview
      • CASE
      • IF
      • IFNULL
      • ISNULL
      • ZN
    • Mathematical functions
      • Overview
      • ABS
      • ACOS
      • ASIN
      • ATAN
      • ATAN2
      • CEILING
      • COMPARE
      • COS
      • COT
      • DEGREES
      • DIV
      • EXP
      • FLOOR
      • GREATEST
      • LEAST
      • LN
      • LOG
      • LOG10
      • PI
      • POWER
      • RADIANS
      • ROUND
      • SIGN
      • SIN
      • SQRT
      • SQUARE
      • TAN
    • Operators
      • Overview
      • AND
      • Addition and concatenation (+)
      • BETWEEN
      • Comparison
      • Division (/)
      • IN
      • IS FALSE
      • IS TRUE
      • LIKE
      • Modulo (%)
      • Multiplication (*)
      • NOT
      • Negation (-)
      • OR
      • Power (^)
      • Subtraction (-)
    • String functions
      • Overview
      • ASCII
      • CHAR
      • CONCAT
      • CONTAINS
      • ENDSWITH
      • FIND
      • ICONTAINS
      • IENDSWITH
      • ISTARTSWITH
      • LEFT
      • LEN
      • LOWER
      • LTRIM
      • REGEXP_EXTRACT
      • REGEXP_EXTRACT_NTH
      • REGEXP_MATCH
      • REGEXP_REPLACE
      • REPLACE
      • RIGHT
      • RTRIM
      • SPACE
      • SPLIT
      • STARTSWITH
      • SUBSTR
      • TRIM
      • UPPER
      • UTF8
    • Text markup functions
      • Overview
      • BOLD
      • ITALIC
      • MARKUP
      • URL
    • Time series functions
      • Overview
      • AGO
      • AT_DATE
    • Type conversion functions
      • Overview
      • BOOL
      • DATE
      • DATETIME
      • DATETIME_PARSE
      • DATE_PARSE
      • DB_CAST
      • FLOAT
      • GEOPOINT
      • GEOPOLYGON
      • INT
      • STR
    • Window functions
      • Overview
      • AVG
      • AVG_IF
      • COUNT
      • COUNT_IF
      • FIRST
      • LAG
      • LAST
      • MAVG
      • MAX
      • MCOUNT
      • MIN
      • MMAX
      • MMIN
      • MSUM
      • RANK
      • RANK_DENSE
      • RANK_PERCENTILE
      • RANK_UNIQUE
      • RAVG
      • RCOUNT
      • RMAX
      • RMIN
      • RSUM
      • SUM
      • SUM_IF
    • Function Availability
  • Function tutorials
    • Aggregate functions
    • Window functions
    • LOD expressions and filtering in aggregate functions
  • Public materials
    • Educational projects
    • Webinars and conferences
    • Articles and publications
    • Public dashboards and charts
  • Troubleshooting
    • Questions and answers
    • DataLens errors
  1. Function reference
  2. Window functions
  3. MSUM

MSUM (window)

Written by
Yandex Cloud
,
improved by
amatol

    Syntax

    Standard
    Extended
    MSUM( value, rows_1 [ , rows_2 ] )
    
    MSUM( value, rows_1 [ , rows_2 ]
          [ TOTAL | WITHIN ... | AMONG ... ]
          [ ORDER BY ... ]
          [ BEFORE FILTER BY ... ]
        )
    

    More info:

    • TOTAL, WITHIN, AMONG
    • ORDER BY
    • BEFORE FILTER BY

    Description

    Warning

    The sorting order is based on the fields listed in the sorting section of the chart and in the ORDER BY clause. First, ORDER BY fields are used, and then they are complemented by the fields from the chart.

    Returns the moving sum of values in a fixed-size window defined by the sort order and arguments:

    rows_1 rows_2 Window
    positive - The current row and rows_1 preceding rows.
    negative - The current row and -rows_1 following rows.
    any sign any sign rows_1 preceding rows, the current row and rows_2 following rows.

    Window functions with a similar behavior: MCOUNT, MMIN, MMAX, MAVG.

    See also SUM, RSUM.

    Argument types:

    • value — Fractional number | Integer
    • rows_1 — Integer
    • rows_2 — Integer

    Return type: Same type as (value)

    Note

    Only constant values are accepted for the arguments (rows_1, rows_2).

    Examples

    Example with two and three arguments

    Source data

    Date City Category Orders Profit
    '2019-03-01' 'London' 'Office Supplies' 8 120.80
    '2019-03-04' 'London' 'Office Supplies' 2 100.00
    '2019-03-05' 'London' 'Furniture' 1 750.00
    '2019-03-02' 'Moscow' 'Furniture' 2 1250.50
    '2019-03-03' 'Moscow' 'Office Supplies' 4 85.00
    '2019-03-01' 'San Francisco' 'Office Supplies' 23 723.00
    '2019-03-01' 'San Francisco' 'Furniture' 1 1000.00
    '2019-03-03' 'San Francisco' 'Furniture' 4 4000.00
    '2019-03-02' 'Detroit' 'Furniture' 5 3700.00
    '2019-03-04' 'Detroit' 'Office Supplies' 25 1200.00
    '2019-03-04' 'Detroit' 'Furniture' 2 3500.00

    Grouped by [City].

    Sorted by [City].

    Result

    [City] SUM([Orders]) MSUM(SUM([Orders]), 1) MSUM(SUM([Orders]), -2) MSUM(SUM([Orders]) 1, 1)
    'Detroit' 32 32 49 43
    'London' 11 43 45 49
    'Moscow' 6 17 34 45
    'San Francisco' 28 34 28 34
    Example with ORDER BY

    Source data

    Date City Category Orders Profit
    '2019-03-01' 'London' 'Office Supplies' 8 120.80
    '2019-03-04' 'London' 'Office Supplies' 2 100.00
    '2019-03-05' 'London' 'Furniture' 1 750.00
    '2019-03-02' 'Moscow' 'Furniture' 2 1250.50
    '2019-03-03' 'Moscow' 'Office Supplies' 4 85.00
    '2019-03-01' 'San Francisco' 'Office Supplies' 23 723.00
    '2019-03-01' 'San Francisco' 'Furniture' 1 1000.00
    '2019-03-03' 'San Francisco' 'Furniture' 4 4000.00
    '2019-03-02' 'Detroit' 'Furniture' 5 3700.00
    '2019-03-04' 'Detroit' 'Office Supplies' 25 1200.00
    '2019-03-04' 'Detroit' 'Furniture' 2 3500.00

    Grouped by [City].

    Sorted by [City].

    Result

    [City] SUM([Orders]) MSUM(SUM([Orders]), 1 ORDER BY [City] DESC) MSUM(SUM([Orders]), 1 ORDER BY [Order Sum])
    'Detroit' 32 43 60
    'London' 11 17 17
    'Moscow' 6 34 6
    'San Francisco' 28 28 39
    Example with grouping

    Source data

    Date City Category Orders Profit
    '2019-03-01' 'London' 'Office Supplies' 8 120.80
    '2019-03-04' 'London' 'Office Supplies' 2 100.00
    '2019-03-05' 'London' 'Furniture' 1 750.00
    '2019-03-02' 'Moscow' 'Furniture' 2 1250.50
    '2019-03-03' 'Moscow' 'Office Supplies' 4 85.00
    '2019-03-01' 'San Francisco' 'Office Supplies' 23 723.00
    '2019-03-01' 'San Francisco' 'Furniture' 1 1000.00
    '2019-03-03' 'San Francisco' 'Furniture' 4 4000.00
    '2019-03-02' 'Detroit' 'Furniture' 5 3700.00
    '2019-03-04' 'Detroit' 'Office Supplies' 25 1200.00
    '2019-03-04' 'Detroit' 'Furniture' 2 3500.00

    Grouped by [City], [Category].

    Sorted by [City], [Category].

    Result

    [City] [Category] SUM([Orders]) MSUM(SUM([Orders]), 1 TOTAL) MSUM(SUM([Orders]), 1 WITHIN [City]) MSUM(SUM([Orders]), 1 AMONG [City])
    'Detroit' 'Furniture' 7 7 7 7
    'Detroit' 'Office Supplies' 25 32 32 48
    'London' 'Furniture' 1 26 1 8
    'London' 'Office Supplies' 10 11 11 14
    'Moscow' 'Furniture' 2 12 2 3
    'Moscow' 'Office Supplies' 4 6 6 29
    'San Francisco' 'Furniture' 5 9 5 7
    'San Francisco' 'Office Supplies' 23 28 28 23

    Data source support

    ClickHouse 19.13, Microsoft SQL Server 2017 (14.0), MySQL 5.6, Oracle Database 12c (12.1), PostgreSQL 9.3.

    Was the article helpful?

    Language / Region
    Yandex project
    © 2023 Yandex.Cloud LLC