创建在两个表中包含两列的视图

发布于 2024-12-21 21:06:56 字数 1637 浏览 1 评论 0原文

有两个表:salesexpensessales 有两列:sale_dateprice,费用表有两列:dateamount< /代码>。还有一个视图,其中包含天数 年。 (创建 365 天的视图

我想向此视图添加另外两列: salesexpenses,其中数据位于 sales费用表。

示例:

Sales Table

sale_date   price

05-DEC-11   1,500
05-DEC-11     500
11-DEC-11     800
17-DEC-11   2,400

Expenses Table

date        amount

06-DEC-11      300
11-DEC-11      100
11-DEC-11      400
15-DEC-11    1,100

View 应创建如下

date        sales   expenses

01-JAN-11    null    null
02-JAN-11    null    null
03-JAN-11    null    null
04-JAN-11    null    null
05-JAN-11    null    null

...


05-DEC-11   2,000    null
06-DEC-11    null     300
07-DEC-11    null    null
08-DEC-11    null    null
09-DEC-11    null    null
10-DEC-11    null    null
11-DEC-11     800     500
12-DEC-11    null    null
13-DEC-11    null    null
14-DEC-11    null    null
15-DEC-11    null   1,100
16-DEC-11    null    null
17-DEC-11   2,400    null

...


28-DEC-11   null     null
29-DEC-11   null     null
30-DEC-11   null     null
31-DEC-11   null     null

sales视图中的expenses 列由 priceamount 列的总和创建 sale>费用表按日期分组在这两个表中。

我怎么能这样做呢?

There are two tables: sales and expenses. sales has two columns: sale_date and price, expenses table has two columns: date and amount. also have a view with days in
year. (Create View with 365 days)

i want to add another two columns to this view: sales and expenses with data in sales and
expenses tables.

example :

Sales Table

sale_date   price

05-DEC-11   1,500
05-DEC-11     500
11-DEC-11     800
17-DEC-11   2,400

Expenses Table

date        amount

06-DEC-11      300
11-DEC-11      100
11-DEC-11      400
15-DEC-11    1,100

View should create as below

date        sales   expenses

01-JAN-11    null    null
02-JAN-11    null    null
03-JAN-11    null    null
04-JAN-11    null    null
05-JAN-11    null    null

...


05-DEC-11   2,000    null
06-DEC-11    null     300
07-DEC-11    null    null
08-DEC-11    null    null
09-DEC-11    null    null
10-DEC-11    null    null
11-DEC-11     800     500
12-DEC-11    null    null
13-DEC-11    null    null
14-DEC-11    null    null
15-DEC-11    null   1,100
16-DEC-11    null    null
17-DEC-11   2,400    null

...


28-DEC-11   null     null
29-DEC-11   null     null
30-DEC-11   null     null
31-DEC-11   null     null

sales and expenses columns in view created with sum of price and amount Columns in sale and expenses table group by date in those two tables.

how could i do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪荡不羁 2024-12-28 21:06:56

这应该可以让你到达那里:

SELECT the_day, price, amount
  FROM year_days LEFT JOIN (SELECT sale_date, SUM(price) price
                              FROM sales
                             GROUP BY sale_date) ON the_day = sale_date
                 LEFT JOIN (SELECT expense_date, SUM(expenses) amount
                              FROM expenses
                             GROUP BY expense_date) ON the_day = expense_date
 ORDER BY the_day;

THE_DAY        PRICE     AMOUNT
--------- ---------- ----------
...
01-DEC-11
02-DEC-11
03-DEC-11
04-DEC-11
05-DEC-11       2000
06-DEC-11                   300
07-DEC-11
08-DEC-11
09-DEC-11
10-DEC-11
11-DEC-11        800        500
12-DEC-11
13-DEC-11
14-DEC-11
15-DEC-11                  1100
16-DEC-11
17-DEC-11       2400
18-DEC-11
...

This should get you there:

SELECT the_day, price, amount
  FROM year_days LEFT JOIN (SELECT sale_date, SUM(price) price
                              FROM sales
                             GROUP BY sale_date) ON the_day = sale_date
                 LEFT JOIN (SELECT expense_date, SUM(expenses) amount
                              FROM expenses
                             GROUP BY expense_date) ON the_day = expense_date
 ORDER BY the_day;

THE_DAY        PRICE     AMOUNT
--------- ---------- ----------
...
01-DEC-11
02-DEC-11
03-DEC-11
04-DEC-11
05-DEC-11       2000
06-DEC-11                   300
07-DEC-11
08-DEC-11
09-DEC-11
10-DEC-11
11-DEC-11        800        500
12-DEC-11
13-DEC-11
14-DEC-11
15-DEC-11                  1100
16-DEC-11
17-DEC-11       2400
18-DEC-11
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文