如何在子查询中使用DISTINCT。

huangapple go评论60阅读模式
英文:

How to use distinct in sub select

问题

I have trouble to get distinct count of column.
The result I get right now is total of all not different ones.
This select is as a sub select

select count (distinct tss.Deliverypostadre) from Shipment tss
where  tss.DATAAREAID='wh' and tss.PARTITION=1234) as client_count

And if I just go using distinct

distinct s.Deliverypostadre as Client_count

Msg 156, Level 15, State 1, Line 105 Incorrect syntax near the keyword
'distinct'.

can't figure out how to make it work.

英文:

Hello I have trouble to get distinct count of column.
The result I get right now is total of all not different ones.
This select is as a sub select

select count (distinct tss.Deliverypostadre) from Shipment tss
where  tss.DATAAREAID='wh' and tss.PARTITION=1234) as client_count

And if I just go using distinct

distinct s.Deliverypostadre as Client_count

> Msg 156, Level 15, State 1, Line 105 Incorrect syntax near the keyword
> 'distinct'.

can't figure out how to make it work.

答案1

得分: 1

请检查以下查询:

选择 .. 在(选择计数(不同 tss.Deliverypostadre )作为 client_count  Shipment tss
其中 tss.DATAAREAID='wh'  tss.PARTITION=1234

选择 Code  [dbo].[Codes] where codeId 在(选择计数(不同 Code)作为 code  [dbo].[Codes]
英文:

Please Check below query

Select ..in (select count(distinct tss.Deliverypostadre)  as client_count from Shipment tss
where  tss.DATAAREAID='wh' and tss.PARTITION=1234)



 Select Code from [dbo].[Codes] where codeId in (select count (distinct Code) as code from  [dbo].[Codes]) 

答案2

得分: 0

这部分可以翻译如下:

这应该有效:

选择 . . .
       (选择 count(distinct tss.Deliverypostadre)
        从发货 tss
        其中 tss.DATAAREAID = 'wh' 且 tss.PARTITION = 1234
       )作为客户数量
从 . . .

一些数据库可能对 count( 之间的空格有问题,但语法基本上看起来是正确的。

英文:

This should work:

select . . .
       (select count(distinct tss.Deliverypostadre)
        from Shipment tss
        where tss.DATAAREAID = 'wh' and tss.PARTITION = 1234
       ) as client_count
from . . .

Some databases might have a problem with the space between count and (. but the syntax basically looks correct.

huangapple
  • 本文由 发表于 2020年1月6日 22:46:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614078.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定