我在 SQL Server 2012 中有一个表。表中Messages有three columns
ID which is an identity column,
MessageID (int) which is a foreign key to other table and
IsRead field which is a bit not null column.
Run Code Online (Sandbox Code Playgroud)
在给定的时间点,表可以包含大量具有IsRead列值的记录1 or 0。我想编写一个查询,在其中查找两个单独列中已读和未读消息的计数。我正在尝试使用 SQL Server 2012 中引入的新 Windows 函数来提高效率。我该怎么做 ?
请检查这是否有帮助。
SELECT
IsRead,
COUNT(*) ReadCount
FROM
YourTable
GROUP BY IsRead
Run Code Online (Sandbox Code Playgroud)
或者您可以在两列中进行选择,例如:
SELECT SUM(IsRead - 0) AS ReadCount,
SUM(1 - IsRead) AS UnreadCount
FROM YourTable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2117 次 |
| 最近记录: |