[英]Subtracting count of two different columns
I have the following table我有下表
Table A
UploadDate Destroy
12/23/2020 1
12/31/2025 0
11/11/2020 1
12/16/2021 1
I have aroud 1000 rows like this.我有大约 1000 行这样的行。 I need to write a SQL statement where count(uploadDate) - count of destroy where destroy=1 so in the above case, it will be 4-3.
我需要编写一个 SQL 语句 where count(uploadDate) - 销毁计数 where destroy=1 所以在上述情况下,它将是 4-3。 I want 1 to be returned by the query.
我希望查询返回 1。 Bedlow is the create table statement:
Bedlow是创建表语句:
CREATE TABLE [dbo].[Table_A](
[UploadDate] [datetime] NULL,
[Destroyed] [bit] NULL
) ON [PRIMARY]
GO
below are the insert statement for some sample data:下面是一些示例数据的插入语句:
INSERT INTO [dbo].[Table_A]
([UploadDate]
,[Destroyed])
VALUES
('12/23/2020'
,1)
INSERT INTO [dbo].[Table_A]
([UploadDate]
,[Destroyed])
VALUES
('12/22/2020'
,0)
INSERT INTO [dbo].[Table_A]
([UploadDate]
,[Destroyed])
VALUES
('12/31/2020'
,0)
INSERT INTO [dbo].[Table_A]
([UploadDate]
,[Destroyed])
VALUES
('11/11/2020'
,1)
INSERT INTO [dbo].[Table_A]
([UploadDate]
,[Destroyed])
VALUES
('12/16/2021'
,1)
This is what I have so far:这是我到目前为止所拥有的:
select count(uploadDate) - count(Destroyed)
from document
where destroyed=1
You may use COUNT(CASE WHEN destroyed = 1 THEN 1 END)
to find the count of rows where destroyed = 1
.您可以使用
COUNT(CASE WHEN destroyed = 1 THEN 1 END)
来查找destroyed = 1
的行数。
SELECT COUNT(*) -
COUNT(CASE WHEN destroyed = 1 THEN 1
END) AS RES
FROM Table_A
See a demo from db<>fiddle .查看db<>fiddle的演示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.