SQL计数结果

SQL计数结果,sql,Sql,在列出的PO中,我得到了一次PO,但PO Count列仍然给我141的结果,计算PO在where或distinct过滤器之前的实例数 也许我完全走错了路 以下是一些结果: select distinct LsPO AS 'PO Listed', LsSuID AS 'Supplier ID', LSShipSpeed AS 'Ship Speed', count('PO Listed') as 'PO Count' From csn_order..TblLoadShipping where d

在列出的PO中,我得到了一次PO,但PO Count列仍然给我141的结果,计算PO在where或distinct过滤器之前的实例数

也许我完全走错了路

以下是一些结果:

select distinct
LsPO AS 'PO Listed',
LsSuID AS 'Supplier ID',
LSShipSpeed AS 'Ship Speed',
count('PO Listed') as 'PO Count'
From csn_order..TblLoadShipping
where 
datalength(LSPO) = '10' 
and LsDateLoaded >= DATEADD(day,-1, GETDATE())
and lspo like 'CS%'
and lsshipspeed is not null
group by lspo, lssuid, lsshipspeed
order by 'PO Listed' desc

我喜欢最后一列,计数,以指示CS89980113出现两次

如果您想要列的非空值的数目,可以使用

CS89980125  14347   153 1
CS89980122  22471   1   1
CS89980113  420 153 1
CS89980113  420 GR  1
如果需要第二列的非空值相对计数,则可以使用

select count(your_col1)
from your_table 
如果您在group by中使用的列与在count中使用的列相同,则无法获得group by

如果您需要使用distinct值的count,可以在
count(distinct您的列1)
函数中添加distinct

根据您的示例,您不能在select子句中使用别名,因此您不能使用

select your_col2, count(your_col1)
from your_table 
group by your_col2
但是你应该使用

count('PO Listed') as 'PO Count' 
  count(LsPO ) as 'PO Count'
对于不同的值,您应该使用

count('PO Listed') as 'PO Count' 
  count(LsPO ) as 'PO Count'

您的查询在sql中没有意义。。尝试显示真实案例或正确的sql查询。。请提供一些SqlFiddle我更新了原始帖子,使其包含了我的确切脚本。因此我尝试将distinct添加到计数中,现在我得到了这些结果
CS89790849 14695 GR 1
CS89790848 11898 FDHD 1
CS89790847 33430 1
CS89790847 33430 GR 1
很抱歉格式化,我认为我无法在这里使结果看起来很好。无论如何,CS89790847出现了两次,我想让计数显示2。您的评论不清楚(您提供的sql也是错误的)。。。无论如何,我已经用其他建议更新了asnwer