简体   繁体   English

SQL命令按C#WinForms计数后的行数

[英]SQL command that count number of rows after group by c# winforms

I have a table called Order(Id,Number,Date). 我有一个名为Order(Id,Number,Date)的表。 "Id" and "Number" are main keys. “ Id”和“ Number”是主键。

table Order 表顺序

Id Number Date
0   1     1995   
0   2     1995        
0   3     1995   
1   1     1999   
1   2     1999   
2   1     2001

I need the number of rows but without duplicates (in this example: 3 ) I know that the "group by Id" will give me: Id(0,1,2) but I cant count it after the group by. 我需要行数,但没有重复项(在此示例中:3),我知道“按ID分组”会给我:Id(0,1,2),但在分组依据之后我无法计数。

Check this SQL Fiddle working code , it returns what you are looking for. 检查此SQL Fiddle工作代码 ,它返回您要查找的内容。 It groups by Number and then counts the total of rows, on this case, 3 rows. 它按Number分组,然后计算总行数(在这种情况下为3行)。

EDIT 编辑

Sorry, I see you are counting by ID, modified the code. 抱歉,我看到您正在按ID进行计数,修改了代码。 Nevertheless is the same logic just different column. 不过,相同的逻辑只是不同的列。

SELECT COUNT(*) Rows_count
FROM (
      SELECT COUNT([Id]) count_rows
        FROM tOrder
       GROUP BY [Id]) AS T1

Result: 结果:

ROWS_COUNT
   3

EDIT 编辑

This code works too indeed and is shorter. 此代码确实可以工作,并且更短。 My previous one helps if you need to add a HAVING clause after the GROUP BY in case you want some particular value to be counted or not. 如果您需要在GROUP BY之后添加HAVING子句,以防不计某些特殊值,我的上一篇文章会有所帮助。

SELECT COUNT(DISTINCT id) FROM [Order]
select count(distinct id) from order

Try this : 尝试这个 :

select DISTINCT COUNT(Id) from Order 从订单中选择DISTINCT COUNT(Id)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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