count(case when)
时间: 2023-10-19 15:06:25 浏览: 128
SQL case_when用法
"COUNT(CASE WHEN)" is a SQL statement used to count the number of rows in a table that meet a specific condition. Here is an example:
Suppose we have a table called "orders" with the following columns: order_id, customer_name, order_date, and order_total.
If we want to count the number of orders made by customers named "John" in the year 2021, we can use the following SQL statement:
```
SELECT COUNT(CASE WHEN customer_name = 'John' AND YEAR(order_date) = 2021 THEN 1 END) AS john_2021_orders
FROM orders;
```
This statement will return a single column with the count of orders made by John in 2021.
阅读全文