自上一年每个国家的收入增长百分比

时间:2018-03-16 12:55:08

标签: sql-server

我需要获得自去年以来每个国家的增加收入百分比。

create table Customer
(
customer_mail_address   varchar(255) not null,
lastname                varchar(50) not null,
firstname               varchar(50) not null,
payment_method          varchar(10) not null,
payment_card_number     varchar(30) not null,
contract_type           varchar(20) not null,
subscription_start      date        not null,
subscription_end        date                ,
user_name               varchar(30) not null,
password                varchar(50) not null,
country_name            varchar(50) not null,
gender                  char(1)             ,
birth_date              date                ,

constraint pk_Customer primary key(customer_mail_address),
constraint fk_Customer1 foreign key(country_name) references Country(country_name) ON UPDATE CASCADE,
constraint fk_Customer2 foreign key(contract_type) references Contract(contract_type) ON UPDATE CASCADE,
constraint fk_Customer3 foreign key(payment_method) references Payment(payment_method) ON UPDATE CASCADE,
constraint chk_Customer1 check(subscription_start < subscription_end),
constraint uc_Customer unique(user_name),
constraint chk_Customer2 check(len([password]) >= 8 AND [password] like '%[0-9]%'),
constraint chk_Customer3 check(birth_date < subscription_start),
constraint chk_Customer4 check(user_name NOT LIKE '%[^A-Z0-9]%') 
)


create table Watchhistory
(
movie_id                integer     not null,
customer_mail_address   varchar(255)not null,
watch_date              date        not null,
price                   numeric(5,2)not null,
invoiced                bit         not null,

constraint pk_Watchhistory primary key(movie_id, customer_mail_address, watch_date),
constraint fk_Watchhistory1 foreign key(movie_id) references Movie(movie_id),
constraint fk_Watchhistory2 foreign key(customer_mail_address) references Customer(customer_mail_address) ON UPDATE CASCADE ON DELETE CASCADE
)

我用这两个表来计算这个。我尝试了很多东西,但没有真正做到这一点。这就是我现在要计算的每个国家每年的收入,而不是每个国家的增长百分比。

CREATE VIEW OmzetStijgingDalingPerLand AS
SELECT DATEPART(yy,watch_date) as [jaar], country_name, SUM(price) AS [omzet]
FROM Watchhistory w
INNER JOIN Customer c ON w.customer_mail_address = c.customer_mail_address
GROUP BY DATEPART(yy,watch_date), country_name

1 个答案:

答案 0 :(得分:0)

正如前面提到的文章所述,LAG功能是关键

nixpkgs