計數資料

AI 輔助 SQL 查詢入門

Jasmin Ludolf

Senior Data Science Content Developer

COUNT 函式

Prompt: 我們有多少產品?

SELECT COUNT(*) AS total_count
FROM products;
|total_count|
|-----------|
|50         |
AI 輔助 SQL 查詢入門

COUNT 函式

Prompt: 我們有多少產品?

SELECT COUNT(*)
FROM products;
|?count?|
|-------|
|50     |
AI 輔助 SQL 查詢入門

COUNT 函式

Prompt: 計算 products 資料表的列數

SELECT COUNT(*) AS total_count
FROM products;
|total_count|
|-----------|
|50         |
AI 輔助 SQL 查詢入門

計數紀錄或欄位值

Prompt: 我們有多少產品?

SELECT COUNT(*) AS total_count
FROM products;
|total_count|
|-----------|
|50         |
  • 計數資料表中的每筆紀錄

Prompt: 我們有多少個產品名稱?

SELECT COUNT(product_name) AS product_count
FROM products;
|product_count|
|-------------|
|49           |
  • 只計數有值的紀錄
AI 輔助 SQL 查詢入門

計數唯一值

Prompt: 我們販售多少種不同的類別?

SELECT COUNT(DISTINCT category) AS unique_categories
FROM products;
|unique_categories|
|-----------------|
|9                |
AI 輔助 SQL 查詢入門

重點回顧

$$

  • 需要 COUNT(*)
    • 「多少筆紀錄」或「多少列」
  • 需要 COUNT(field)
    • 「多少個產品名稱」或「多少位顧客」
  • 加上「不同」、「唯一」或「不重複」時,需加入 DISTINCT

一個人坐在桌旁使用筆電

AI 輔助 SQL 查詢入門

一起來練習吧!

AI 輔助 SQL 查詢入門

Preparing Video For Download...