Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有如下结构表,请写出符合条件的SQL #17

Open
xx19941215 opened this issue Mar 21, 2019 · 1 comment
Open

有如下结构表,请写出符合条件的SQL #17

xx19941215 opened this issue Mar 21, 2019 · 1 comment

Comments

@xx19941215
Copy link
Owner

product表
product_id
product_status
product_price

category表
category_id
category_name

product_category表
product_id
category_id

找出品类为Shoes的产品,运行sql得出如下结构的数据 第一行为价格范围

状态 [0, 10] [10,20] [20,30] [30,40]
上架 10 10 10 10
下架 10 10 10 10
其他 10 10 10 10
@xx19941215 xx19941215 changed the title 有如下结构表,请写出符合条件的sql 有如下结构表,请写出符合条件的SQL Mar 21, 2019
@lushaobo
Copy link

lushaobo commented Mar 6, 2021

SELECT
product_status,
(
sum( CASE WHEN product_price >= 0 AND product_price < 10 THEN 1 ELSE 0 END )) AS 0_10,
(
sum( CASE WHEN product_price >= 10 AND product_price < 20 THEN 1 ELSE 0 END )) AS 10_20,
(
sum( CASE WHEN product_price >= 20 AND product_price < 30 THEN 1 ELSE 0 END )) AS 20_30,
(
sum( CASE WHEN product_price >= 30 THEN 1 ELSE 0 END )) AS other
FROM
product
WHERE
product_id IN (
SELECT
product_id
FROM
category_product
WHERE
category_id = ( SELECT category_idFROM category WHERE category_name= 'Shoes' ))
GROUP BY
product_status;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants