1

我正在创建一个程序,除了程序的功能之外,它还提供统计信息。我想添加的一件事是每天的交易数量,然后可能有一个折线图,显示交易如何按天(一个月内),然后按月,然后按年变化。我只是无法真正弄清楚如何将其转换为程序可以提取该数据并将其放入图表的数据库

会是类似的东西还是我缺少什么:?

日表;(AI)id、daynumber、dayofweek、numoftrans 外键:monthnumber、yearnumber

月表

(AI)id, monthnumber, nameofmonth, numoftrans

年表

(AI)id, yearnumber, numberoftrans

4

1 回答 1

0

All you need is a table that stores dates and number of transactions.

create table transactions_per_day (
  transaction_date date primary key,
  num_transactions integer not null check (num_transactions >= 0)
);

The number of transactions per month and per year can be done in a query with aggregate functions (SUM()) and a WHERE clause. For the total per month . . .

select
  extract(year from transaction_date) tr_year, 
  extract(month from transaction_date) tr_month, 
  sum(num_transactions) num_tr
from transactions_per_day
where transaction_date between '2013-01-01' and '2013-12-31'
group by tr_year, tr_month
order by tr_year, tr_month;

It often makes sense to create views for the monthly totals and the annual totals.

create view transactions_per_month as
select
  extract(year from transaction_date) tr_year, 
  extract(month from transaction_date) tr_month, 
  sum(num_transactions) num_tr
from transactions_per_day
where transaction_date between '2013-01-01' and '2013-12-31'
group by tr_year, tr_month
order by tr_year, tr_month;
于 2013-02-07T15:09:58.747 回答