D
D
Danil Samodurov2021-06-15 20:14:43
PostgreSQL
Danil Samodurov, 2021-06-15 20:14:43

What SQL query to write?

Hello. For example, there is a table in a PostgreSQL database:

CREATE TABLE Products
(
    Id SERIAL PRIMARY KEY,
    ProductName VARCHAR(30) NOT NULL,
    Company VARCHAR(20) NOT NULL,
    ProductCount INT DEFAULT 0,
    Price NUMERIC NOT NULL,
    IsDiscounted BOOL
);
   
INSERT INTO Products (ProductName, Company, ProductCount, Price, IsDiscounted) 
VALUES
('iPhone X', 'Apple', 3, 76000, false),
('iPhone 8', 'Apple', 2, 71000, true),
('iPhone 7', 'Apple', 5, 42000, true),
('Galaxy S9', 'Samsung', 2, 46000, false),
('Galaxy S8 Plus', 'Samsung', 1, 56000, true),
('Desire 12', 'HTC', 5, 28000, true),
('Nokia 9', 'HMD Global', 6, 38000, true);

You need to display the total price for each manufacturer (company) so that if isdiscounted is true, then the price is added, and if it is false, then the price is subtracted.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-06-15
@samodurOFF

SELECT Company, SUM(CASE WHEN IsDiscounted THEN Price ELSE -Price END) ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question