34,576
社区成员
发帖
与我相关
我的任务
分享
select COUNT(*) from [tab_limited] where id in (select min(id) from [tab_limited] group by text)
select count(distinct(text)) from [tab_limited]
select COUNT(*) from [tab_limited] where id in (select min(id) from [tab_limited] group by text) and createtime_string=20170806
select count(distinct(text)) from [tab_limited] where createtime_string=20170806
--测试数据
if not object_id(N'Tempdb..#tab_limited') is null
drop table #tab_limited
Go
Create table #tab_limited([id] int,[text] nvarchar(23),[createtime_string] INT)
Insert #tab_limited
select 1,N'测试1',20170807 union all
select 2,N'测试1',20170806 union all
select 4,N'测试2',20170806
Go
--测试数据结束
--1、
SELECT COUNT(*)
FROM [#tab_limited]
WHERE id IN ( SELECT MIN(id)
FROM [#tab_limited]
GROUP BY text ) --这里搜索出来的id是1、4 但显然1是不符合where条件的,但是2符合条件
AND createtime_string = 20170806
--2、
SELECT COUNT(DISTINCT ( text )) --这里搜索出来的id是2、4
FROM [#tab_limited]
WHERE createtime_string = 20170806