具有通用表表达式的复杂SQL的示例
示例
假设我们要从“热门类别”中查询“最便宜的产品”。
这是使用公用表表达式的查询示例
-- all_sales: just a simple SELECT with all the needed JOINS WITH all_sales AS ( SELECT product.priceas product_price, category.idas category_id, category.descriptionas category_description FROM sale LEFT JOIN product onsale.product_id= product.id LEFT JOIN category onproduct.category_id= category.id ) -- Group by category , sales_by_category AS ( SELECT category_id, category_description, sum(product_price) as total_sales FROM all_sales GROUP BY category_id, category_description ) -- Filtering total_sales > 20 , top_categories AS ( SELECT * from sales_by_category WHERE total_sales > 20 ) -- all_products: just a simple SELECT with all the needed JOINS , all_products AS ( SELECT product.idas product_id, product.descriptionas product_description, product.priceas product_price, category.idas category_id, category.descriptionas category_description FROM product LEFT JOIN category onproduct.category_id= category.id ) -- Order by product price , cheapest_products AS ( SELECT * from all_products ORDER by product_price ASC ) -- Simple inner join , cheapest_products_from_top_categories AS ( SELECT product_description, product_price FROM cheapest_products INNER JOIN top_categories ON cheapest_products.category_id = top_categories.category_id ) --The main SELECT SELECT * from cheapest_products_from_top_categories
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短