使用 GROUP BY IN MYSQL 对状态列的 id 计数以及特定行计数的总和
count of id on status coloumn with sum of particular rows count using GROUP BY IN MYSQL
我有以下 mysql table
id status
------------
1 available
2 hold
3 available
4 so-hold
5 so-hold
6 hold
7 hold
8 available
当我在 STATUS
列上使用 GROUP BY
时,我得到以下结果:
count(id) status
---------------------
3 available
3 hold
2 so-hold
status
栏下'hold'和'so-hold'属于同一个类别所以我需要按以下方式计数:
count(id) status
---------------------
3 available
5 hold
以下是我目前正在使用的 MySQL 查询:
SELECT count(id), status FROM `inventory_master` GROUP BY status
尝试用空字符串替换 "so-",然后尝试如下查询:
SELECT newStatus, COUNT(*)
FROM
(SELECT REPLACE(status, 'so-', '') as newStatus
FROM MYTABLE) AS tab
GROUP BY newStatus
您可以尝试这样的操作:
SELECT COUNT(*), status FROM table WHERE status NOT IN('hold', 'so-hold') GROUP BY status
UNION
SELECT COUNT(*), 'hold' FROM table WHERE status IN('hold', 'so-hold')
首先你 COUNT
普通组然后 UNION
和 COUNT
两个子组 hold
+ so-hold
.
SQLFiddle
您可以使用CASE
语句
select
count(id),
case when status = 'so-hold'
then 'hold'
else status
end as status_col
from inventory_master
group by status_col
您可以在这种情况下使用案例:
select status, count(*) from `inventory_master` group by case status when 'so-hold' then 'hold' else status end;
两步。首先(Sub-Select 将 'so-hold' 替换为 'hold')使用 CASE。第二步 GROUP BY.
SELECT g1.status, COUNT(*)
FROM ( SELECT CASE status
WHEN 'so-hold' THEN 'hold'
ELSE status
END AS status,
id
FROM test_tw
) g1
GROUP BY g1.status
ORDER BY g1.status;
(使用 ORACLE 测试)
试试这个
Select Case When status='So-Hold' Then 'Hold' Else status End,COUNT(*)
From TableName
Group by Case When status='So-Hold' Then 'Hold' Else status End
我有以下 mysql table
id status
------------
1 available
2 hold
3 available
4 so-hold
5 so-hold
6 hold
7 hold
8 available
当我在 STATUS
列上使用 GROUP BY
时,我得到以下结果:
count(id) status
---------------------
3 available
3 hold
2 so-hold
status
栏下'hold'和'so-hold'属于同一个类别所以我需要按以下方式计数:
count(id) status
---------------------
3 available
5 hold
以下是我目前正在使用的 MySQL 查询:
SELECT count(id), status FROM `inventory_master` GROUP BY status
尝试用空字符串替换 "so-",然后尝试如下查询:
SELECT newStatus, COUNT(*)
FROM
(SELECT REPLACE(status, 'so-', '') as newStatus
FROM MYTABLE) AS tab
GROUP BY newStatus
您可以尝试这样的操作:
SELECT COUNT(*), status FROM table WHERE status NOT IN('hold', 'so-hold') GROUP BY status
UNION
SELECT COUNT(*), 'hold' FROM table WHERE status IN('hold', 'so-hold')
首先你 COUNT
普通组然后 UNION
和 COUNT
两个子组 hold
+ so-hold
.
SQLFiddle
您可以使用CASE
语句
select
count(id),
case when status = 'so-hold'
then 'hold'
else status
end as status_col
from inventory_master
group by status_col
您可以在这种情况下使用案例:
select status, count(*) from `inventory_master` group by case status when 'so-hold' then 'hold' else status end;
两步。首先(Sub-Select 将 'so-hold' 替换为 'hold')使用 CASE。第二步 GROUP BY.
SELECT g1.status, COUNT(*)
FROM ( SELECT CASE status
WHEN 'so-hold' THEN 'hold'
ELSE status
END AS status,
id
FROM test_tw
) g1
GROUP BY g1.status
ORDER BY g1.status;
(使用 ORACLE 测试)
试试这个
Select Case When status='So-Hold' Then 'Hold' Else status End,COUNT(*)
From TableName
Group by Case When status='So-Hold' Then 'Hold' Else status End