Question on Window Function

Mikhail Firsov 1,801 Reputation points
2021-03-10T11:09:01.377+00:00

Hello,

Would you please help me with this question?

76245-question0.png

76229-question1.png

Thank you in advance,
Michael

0 comments No comments
{count} votes

Accepted answer
  1. Russel Loski 416 Reputation points
    2021-03-10T14:16:40.237+00:00

    @Mikhail Firsov

    Here is how you need to read the query. What you understand already is that your over clause partitions the data by warehouse. What you are not understanding is the Order by clause.

    The Order by clause states that the window your aggregates acts on starts with the value in the Product table and goes back to the beginning.

    Let's take the San Francisco warehouse. The first three rows have a window that includes all three of these rows since iPhone is the first value in the order by.

    What is the min quantity in that window? 10
    What is the sum of the quantity in that window? 260

    Then look at the next two rows. The window includes all rows that have a product Samsung or have a value that is less than Samsung. That means that the iPhone rows are included in the window.

    What is the min quantity in that window? 10
    What is the sum of the quantity in that window? 560

    Can you insert a row for San Francisco for product Samsung with a quantity of 5? If you did then this would be the results:

    For iPhone the min would be 10
    For Samsung the min would be 5.

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Mikhail Firsov 1,801 Reputation points
    2021-03-10T13:31:28.477+00:00

    P.S. Is it because of this?

    "If ORDER BY is not specified entire partition is used for a window frame. This applies only to functions that do not require ORDER BY clause. If ROWS/RANGE is not specified but ORDER BY is specified, RANGE UNBOUNDED PRECEDING AND CURRENT ROW is used as default for window frame. This applies only to functions that have can accept optional ROWS/RANGE specification. For example, ranking functions cannot accept ROWS/RANGE, therefore this window frame is not applied even though ORDER BY is present and ROWS/RANGE is not."

    0 comments No comments

  2. Mikhail Firsov 1,801 Reputation points
    2021-03-10T15:20:46.04+00:00

    Hello RusselLoski-0634,

    Thank you so much for your clarification - I did understand!!!

    Regards,
    Michael

    0 comments No comments

  3. EchoLiu-MSFT 14,516 Reputation points
    2021-03-11T08:25:58.67+00:00

    Hi @Mikhail Firsov

    You can also refer to:
    SELECT - OVER Clause (Transact-SQL)

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  4. Mikhail Firsov 1,801 Reputation points
    2021-03-11T08:30:01.637+00:00

    Hi EchoLiy-msft,

    I've already done it - and haven't found it of much help :(