我有一個如下表 1 的表格資料,想用表 1 創建一個如下視圖 1 的視圖,這可能嗎?
表格1
| 代碼 | 分店名稱 | 日期 | 全部的 |
|---|---|---|---|
| 一個 | 出口 A | 2022 年 1 月 9 日 | 10 |
| 一個 | 出口 A | 2022 年 2 月 9 日 | 20 |
| 乙 | 出口 B | 2022 年 1 月 9 日 | 30 |
| 乙 | 出口 B | 2022 年 2 月 9 日 | 40 |
查看 1
| 日期 | 總出口 A | 出口 B 總計 |
|---|---|---|
| 2022 年 1 月 9 日 | 10 | 30 |
| 2022 年 2 月 9 日 | 20 | 40 |
uj5u.com熱心網友回復:
WITH totals AS
( -- get the total info by outlet and date
SELECT outletname, date, sum(total) as total
FROM table1
GROUP BY outletname, date
), dates AS
( -- get list of all the dates
SELECT date
FROM table1
GROUP BY date
) -- use left joins to get the results
SELECT dates.date, ja.total as [outlet a total], jb.total as [outlet b total]
FROM dates
LEFT JOIN totals ja on dates.date = j1.date and outletname = 'Outlet A'
LEFT JOIN totals jb on dates.date = j1.date and outletname = 'Outlet B'
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507793.html
