久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

SQL - 選擇分組行的一個實例

SQL - Select one instance of grouped rows(SQL - 選擇分組行的一個實例)
本文介紹了SQL - 選擇分組行的一個實例的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個對行進行分組的查詢.產品 ID 相同但日期和數量不同的多行被分組,以便我可以計算數量、平均價格等的總和.

I have a query where rows are grouped. Multiple rows where the product ID are the same but where date and quantity differs, are grouped so I can calculate the sum of quantities, average prices, etc.

我現在需要添加上次銷售的日期和相應的數量.日期加了MAX(date),怎么加對應的數量?

I now need to add the date when the last sale occurred and the corresponding quantity. I added the date with MAX(date), but how do I add the corresponding quantity?

我使用 MS SQL Server Management Studio.

I use MS SQL Server Management Studio.

示例查詢:

SELECT id,
       SUM(quantity) as 'Total q',
       AVG(price) as 'Avg price',
       MAX(price) as 'Max price',
       MAX(date) as 'Last sale date'
FROM table
WHERE date > 2018
GROUP BY id

原始樣本數據:

id     quantity    price     date
1      20          2.30      2018-6-2
1      10          2.40      2018-6-4
1      5           2.55      2018-6-10
2      15          12.50     2018-5-20
2      100         7.50      2018-6-1
2      50          10.00     2018-6-12

預期結果:

id    total q    avg price   max price   last sale    last q  last p
1     35         2.42        2.55        2018-6-10    5       2.55
2     165        10.00       12.50       2018-6-12    50      10.00

推薦答案

你可以使用窗口函數:

SELECT DISTINCT id,
       SUM(quantity) OVER(PARTITION BY id) as "Total q",
       AVG(price) OVER(PARTITION BY id) as "Avg price",
       MAX(price) OVER(PARTITION BY id) as "Max price",
       MAX(_date) OVER(PARTITION BY id) as "Last sale date",
       FIRST_VALUE(quantity) OVER(PARTITION BY id ORDER BY _date DESC) AS last_q,
       FIRST_VALUE(price) OVER(PARTITION BY id ORDER BY _date DESC) AS last_p
FROM tab
WHERE _date > '2018-01-01';

輸出:

┌────┬─────────┬───────────┬───────────┬─────────────────────┬────────┬────────┐
│ id │ Total q │ Avg price │ Max price │   Last sale date    │ last_q │ last_p │
├────┼─────────┼───────────┼───────────┼─────────────────────┼────────┼────────┤
│  1 │      35 │  2.416666 │      2.55 │ 10/06/2018 00:00:00 │      5 │   2.55 │
│  2 │     165 │ 10.000000 │     12.50 │ 12/06/2018 00:00:00 │     50 │  10.00 │
└────┴─────────┴───────────┴───────────┴─────────────────────┴────────┴────────┘

DBFiddle 演示

這篇關于SQL - 選擇分組行的一個實例的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Modify Existing decimal places info(修改現有小數位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 极情综合网| 91精品久久久久久久 | www.伊人.com | 国内自拍真实伦在线观看 | 日韩国产精品一区二区三区 | 中国美女撒尿txxxxx视频 | 亚洲视频第一页 | 日韩国产在线观看 | 国产a级毛毛片 | 中文字幕在线第一页 | 欧美综合久久 | 欧美极品少妇xxxxⅹ免费视频 | 亚洲精品久久久一区二区三区 | 国产精品日本一区二区不卡视频 | 二区不卡 | 成人影院av | 97成人精品| 精品一区二区在线观看 | 国产一区二区三区亚洲 | 国产精品视频在线播放 | 欧美a∨ | 91xxx在线观看 | 欧美国产日韩在线 | 成人亚洲视频 | 国产高清免费 | 亚洲一区二区三区国产 | 涩爱av一区二区三区 | 国产成人精品一区二区三区视频 | 欧美日韩亚洲一区 | 久久国产精品无码网站 | 久久精品国产一区二区三区 | 精品欧美一区免费观看α√ | 亚洲性爰| 久久久99精品免费观看 | 国产免费一区二区 | 日韩一区中文字幕 | 91精品国产色综合久久不卡98 | 亚洲精品一区二区二区 | 精品二区 | av大片 | 国产精品久久 |