declare
@hitung int,
@salesid int,
@ulang int = 0
select @hitung=count(distinct person.person.BusinessEntityID),
@salesid = min(distinct Person.Person.BusinessEntityID)
from Sales.SalesOrderHeader
inner join person.person
on Sales.SalesOrderHeader.SalesOrderID = Person.person.BusinessEntityID
我想知道count(distinct person.person.businessEntityID)的含义是什么。和min的意思(distinct person.person.businessentityid)
我使用adventureworks2008作为上面查询的测试
答案 0 :(得分:1)
它会计算person.person.BusinessEntityID
的唯一值并获得最低person.person.BusinessEntityID
值。
因此,如果该表包含person.person.BusinessEntityID
值
1001
1002
1001
1003
1002
count(person.person.BusinessEntityID) = 5 // all
count(distinct person.person.BusinessEntityID) = 3 //1001, 1002, 1003
min(person.person.BusinessEntityID) = 1001
min( distinct person.person.BusinessEntityID) = 1001
答案 1 :(得分:0)
原始查询涉及连接两个表,但计数或最小值不需要一个表。实际上,由于该连接,查询已经变得非常复杂。如果没有裁员,这将产生相同的结果:
declare
@hitung int,
@salesid int,
@ulang int = 0
select @hitung=count(person.person.BusinessEntityID),
@salesid = min(Person.Person.BusinessEntityID)
from person.person
where exists (select 1
from Sales.SalesOrderHeader
where Sales.SalesOrderHeader.SalesOrderID = Person.person.BusinessEntityID
)
BUT:另外,请考虑SalesOrderID的正确性等于BusinessEntityID 这要求一个人发出的每个订单都具有相同的SalesOrderID。
此关系(Sales.SalesOrderHeader.SalesOrderID = Person.person.BusinessEntityID)似乎不合逻辑并且会引发整个查询。