The SQL OVER() clause - when and why is it useful?

后端 未结 8 1259
有刺的猬
有刺的猬 2020-11-28 17:49
    USE AdventureWorks2008R2;
GO
SELECT SalesOrderID, ProductID, OrderQty
    ,SUM(OrderQty) OVER(PARTITION BY SalesOrderID) AS \'Total\'
    ,AVG(OrderQty) OVER(PAR         


        
8条回答
  •  青春惊慌失措
    2020-11-28 18:27

    prkey   whatsthat               cash   
    890    "abb                "   32  32
    43     "abbz               "   2   34
    4      "bttu               "   1   35
    45     "gasstuff           "   2   37
    545    "gasz               "   5   42
    80009  "hoo                "   9   51
    2321   "ibm                "   1   52
    998    "krk                "   2   54
    42     "kx-5010            "   2   56
    32     "lto                "   4   60
    543    "mp                 "   5   65
    465    "multipower         "   2   67
    455    "O.N.               "   1   68
    7887   "prem               "   7   75
    434    "puma               "   3   78
    23     "retractble         "   3   81
    242    "Trujillo's stuff   "   4   85
    

    That's a result of query. Table used as source is the same exept that it has no last column. This column is a moving sum of third one.

    Query:

    SELECT prkey,whatsthat,cash,SUM(cash) over (order by whatsthat)
        FROM public.iuk order by whatsthat,prkey
        ;
    

    (table goes as public.iuk)

    sql version:  2012
    

    It's a little over dbase(1986) level, I don't know why 25+ years has been needed to finish it up.

提交回复
热议问题