简体   繁体   中英

SQL Counting non numeric values from a column

I have a table named tasks inside is

id
tasks
tasks_status
task_date

The tasks statuses are "done","pending","forwarded","cancelled".

How can I query the table of counting all the done, all the pending, all the forwarded and all cancelled.

I tried doing all the tweaks from COUNT but to no avail. It's been 2 hours of looking for a way. Please help

My goal is to specifically count only those DONE, I can get the count of all the specifics but just would need to show the total count of done (for example)

Group by is better but if you just want the ones specified as 'Done' the below should work.

SELECT COUNT(*) FROM TASTS WHERE TASKS_STATUS='done'

Select what you want with count and Group Them.

SELECTS tasks_status, count(tasks_status) FROM status GROUP BY tasks_status;
select tasks_status, count(*) from tasks group by tasks_status

http://sqlfiddle.com/#!17/1e27e/4/0

If you want to get all counts then you need to use group by but if you want to get count of just one item then group by is not required.

Following is query to generate scenario

CREATE TABLE [dbo].[tasks](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [tasks] [varchar](50) NULL,
    [tasks_status] [varchar](50) NULL,
    [task_date] [date] NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[tasks] ON 
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (1, N'task1', N'pending', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (2, N'task2', N'forwarded', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (3, N'task3', N'pending', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (4, N'task4', N'done', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (5, N'task5', N'done', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (6, N'task6', N'cancelled', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (7, N'task7', N'done', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (8, N'task8', N'forwarded', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (9, N'task9', N'pending', CAST(N'2022-09-30' AS Date))
GO
INSERT [dbo].[tasks] ([id], [tasks], [tasks_status], [task_date]) VALUES (10, N'task10', N'done', CAST(N'2022-09-30' AS Date))
GO
SET IDENTITY_INSERT [dbo].[tasks] OFF
GO

You can use group by if you want to get count of all unique tasks_status or some of tasks_status using following query

For All

SELECT tasks_status, count(tasks_status) FROM dbo.tasks GROUP BY tasks_status;

For done and pending

SELECT tasks_status, count(tasks_status) FROM dbo.tasks 
WHERE tasks_status IN ('done','pending')
GROUP BY tasks_status

if you want to get count for done only then you can use either of the following. Use group by if you want to use other columns in select, else if you want to get just count and no other column then just 2nd query without group by

SELECT tasks_status, count(tasks_status) FROM dbo.tasks 
WHERE tasks_status =  'done'
GROUP BY tasks_status

or

SELECT COUNT(tasks_status) FROM dbo.tasks WHERE tasks_status='done'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM