SQL如何正常价格降低10%,如何提高10%

时间:2017-09-19 13:22:42

标签: sql postgresql

我还是SQL的新手,我有2个问题,我不知道该怎么做。

第一个是:如果购买价格低于100美元的书籍的清单标题和正常购买价格,如果它的价格降低了20%!

第二个是:如果价格上涨10%,列出所有图书的标题和价格上涨!

表格

BOOKTYPES ( book_type_id (PK), book_type )

CATEGORIES ( category_id (PK), category )

PUBLISHERS ( publisher_id (PK), publisher, speciality, country )

BOOKS ( book_id (PK), title, publisher_id (FK), published_year,
 purchase_price, category_id (FK),purchase_date, pages,
 book_type_id (FK) )

AUTHORS ( author_id (PK), first_name, last_name, pseudonym )

AUTHORSHIP ( author_id (PK), book_id (PK) )

1 个答案:

答案 0 :(得分:-1)

我在SQL Server中创建了这些示例,但您可以将其改编为postgresql

declare @BOOKS      table ( book_id int , title varchar(200), publisher_id int , 
                            published_year int, purchase_price decimal(16,2) , category_id int,purchase_date date , pages int , book_type_id int )

insert into @BOOKS (book_id, title , purchase_price) values 
 (1, 'Miscarriages of Justice in Canada: Causes, Responses, Remedies', 96.32)
,(2,'Crime and Punishment in the Russian Revolution: Mob Justice and Police in Petrograd', 34.84) 
,(3,'Selective Enforcement and International Criminal Law: The International Criminal Court and Africa', 101.77) 
,(4,'Secret Intelligence: A Reader',236.43)
,(5,'Skinhead History, Identity, and Culture', 186.24)

SELECT title, purchase_price ,purchase_price  * 0.80 purchase_priceOff  from @BOOKS where purchase_price * 0.80 < 100.

SELECT title, purchase_price ,purchase_price  * 1.10 purchase_priceUp  from @BOOKS 

enter image description here