关于sql查询的问题

qq_21919381 2017-09-20 12:38:23
表1 、表2 和表3 三个表
表1的字段有 卡号,姓名
表2的字段有 卡号 日期
表3的字段有 卡号 日期

现在需要得到的结果为:统计同一个人在表3中最后的日期以后表2的次数统计
例如: 表1
01 张三
02 李四
03 王五
表2
01 2017-05-01
01 2017-05-02
01 2017-05-03
02 2017-05-02
02 2017-05-03
02 2017-05-04
03 2017-05-03
03 2017-05-04
03 2017-05-05
03 2017-05-06
表3
01 2017-05-01
02 2017-05-02
03 2017-05-03
希望得到的结果为
01 张三 2
02 李四 2
03 王五 3
...全文
310 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_21919381 2017-09-26
  • 打赏
  • 举报
回复
引用 17 楼 sinat_28984567 的回复:
没用过access,试试加个临时表。
SELECT  id ,
        MAX(time) AS [time]
INTO    #temp
FROM    #T3
GROUP BY id;
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN #temp AS t3 ON t3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > t3.time
GROUP BY #T1.id ,
        #T1.name;
只能创建一个表来确保数据同步了,谢谢大家
qq_21919381 2017-09-24
  • 打赏
  • 举报
回复
引用 15 楼 sinat_28984567 的回复:
[quote=引用 13楼我是你的主体 的回复:][quote=引用 12 楼 sinat_28984567 的回复:]
--测试数据
if not object_id(N'Tempdb..#T1') is null
    drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
    drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN ( SELECT   id ,
                        MAX(time) AS [time]
               FROM     #T3
               GROUP BY id
             ) AS t3 ON t3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > t3.time
GROUP BY #T1.id ,
        #T1.name
我这边执行以后提示java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException java.lang.NullPointerException 显示就是查询结果异常,用java写的,是不是java不支持这种写法 另外我用的是access数据库[/quote]把语句直接在数据库中执行试试,不要在java 中,看看有问题吗?[/quote] 在数据库中提示 from子句语法错误
二月十六 2017-09-24
  • 打赏
  • 举报
回复
引用 13楼我是你的主体 的回复:
[quote=引用 12 楼 sinat_28984567 的回复:]
--测试数据
if not object_id(N'Tempdb..#T1') is null
    drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
    drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN ( SELECT   id ,
                        MAX(time) AS [time]
               FROM     #T3
               GROUP BY id
             ) AS t3 ON t3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > t3.time
GROUP BY #T1.id ,
        #T1.name
我这边执行以后提示java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException java.lang.NullPointerException 显示就是查询结果异常,用java写的,是不是java不支持这种写法 另外我用的是access数据库[/quote]把语句直接在数据库中执行试试,不要在java 中,看看有问题吗?
二月十六 2017-09-24
  • 打赏
  • 举报
回复
没用过access,试试加个临时表。
SELECT  id ,
        MAX(time) AS [time]
INTO    #temp
FROM    #T3
GROUP BY id;
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN #temp AS t3 ON t3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > t3.time
GROUP BY #T1.id ,
        #T1.name;
qq_21919381 2017-09-24
  • 打赏
  • 举报
回复
引用 11 楼 qq_37170555 的回复:
--测试数据 if not object_id(N'Tempdb..#T1') is null drop table #T1 Go Create table #T1([id] int,[name] nvarchar(22)) Insert #T1 select 1,N'张三' union all select 2,N'李四' union all select 3,N'王五' GO if not object_id(N'Tempdb..#T2') is null drop table #T2 Go Create table #T2([id] int,[time] Date) Insert #T2 select 1,'2017-05-01' union all select 1,'2017-06-02' union all select 1,'2017-06-03' union all select 2,'2017-05-02' union all select 2,'2017-05-03' union all select 2,'2017-05-04' union all select 3,'2017-05-03' union all select 3,'2017-05-04' union all select 3,'2017-05-05' union all select 3,'2017-05-06' GO if not object_id(N'Tempdb..#T3') is null drop table #T3 Go Create table #T3([id] int,[time] Date) Insert #T3 select 1,'2017-05-01' union ALL select 1,'2017-06-01' union all select 2,'2017-05-02' union all select 3,'2017-05-03' Go --测试数据结束 WITH cte AS ( SELECT distinct a.*,max(b.[TIME]) OVER( PARTITION BY b.id) AS maxtime FROM #T1 a INNER JOIN #t3 b ON a.id=b.id ) SELECT distinct a.id,a.name,COUNT(b.[time]) OVER( PARTITION BY a.id) FROM cte a LEFT JOIN #T2 b ON a.id=b.id WHERE b.[time]>a.maxtime id name ----------- ---------------------- ----------- 1 张三 2 2 李四 2 3 王五 3 [/code] 表三中有多条记录去最大时间的那一条的写法
在java中使用access数据库 在执行这个语句的时候提示with sql语句错误
qq_21919381 2017-09-24
  • 打赏
  • 举报
回复
引用 12 楼 sinat_28984567 的回复:
--测试数据
if not object_id(N'Tempdb..#T1') is null
    drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
    drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN ( SELECT   id ,
                        MAX(time) AS [time]
               FROM     #T3
               GROUP BY id
             ) AS t3 ON t3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > t3.time
GROUP BY #T1.id ,
        #T1.name
我这边执行以后提示java.sql.SQLException: java.lang.ArrayIndexOutOfBoundsException java.lang.NullPointerException 显示就是查询结果异常,用java写的,是不是java不支持这种写法 另外我用的是access数据库
二月十六 2017-09-23
  • 打赏
  • 举报
回复
引用 9 楼 qq_21919381 的回复:
[quote=引用 7 楼 sinat_28984567 的回复:]
[quote=引用 6 楼 qq_21919381 的回复:]
[quote=引用 4 楼 qq_21919381 的回复:]
[quote=引用 2 楼 sinat_28984567 的回复:]
--测试数据
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
SELECT #T1.id ,
#T1.name ,
COUNT(1) AS 次数
FROM #T1
JOIN #T3 ON #T3.id = #T1.id
JOIN #T2 ON #T2.id = #T1.id
AND #T2.time > #T3.time
GROUP BY #T1.id ,
#T1.name





那如果是要查第三张表里同一个人最近的日期怎么处理呢?[/quote]
、比如
1,'2017-05-01
2,'2017-05-01
2,'2017-05-02
3,'2017-05-03'
3,'2017-05-04'[/quote]

想要什么结果?[/quote]


数据库前面内容不变,想要的结果依然是
01 张三 2
02 李四 2
03 王五 3[/quote]
有3 2017-05-04 那王五的应该是2了吧?
--测试数据
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04'
Go
--测试数据结束
SELECT #T1.id ,
#T1.name ,
COUNT(1) AS 次数
FROM #T1
JOIN ( SELECT id ,
MAX(time) AS [time]
FROM #T3
GROUP BY id
) AS t3 ON t3.id = #T1.id
JOIN #T2 ON #T2.id = #T1.id
AND #T2.time > t3.time
GROUP BY #T1.id ,
#T1.name


qq_21919381 2017-09-22
  • 打赏
  • 举报
回复
引用 8 楼 ch21st 的回复:
另外一种写法

select t.id,r.name,count(time) from (
   select * from #t2 except select * from #t3
) as t 
inner join #t1 as r on r.id=t.id
group by t.id,r.name

  	id	name	(No column name)
	1	张三	2
	2	李四	2
	3	王五	3

那如果第三张表中同一个人有很多条记录,我只想获得最后一个记录起的计数统计怎么做 之前的是一条记录,现在是多条记录,以每个人的最后一条记录来统计每个人的次数
qq_21919381 2017-09-22
  • 打赏
  • 举报
回复
引用 7 楼 sinat_28984567 的回复:
[quote=引用 6 楼 qq_21919381 的回复:] [quote=引用 4 楼 qq_21919381 的回复:] [quote=引用 2 楼 sinat_28984567 的回复:]
--测试数据
if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
	drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN #T3 ON #T3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > #T3.time
GROUP BY #T1.id ,
        #T1.name
那如果是要查第三张表里同一个人最近的日期怎么处理呢?[/quote] 、比如 1,'2017-05-01 2,'2017-05-01 2,'2017-05-02 3,'2017-05-03' 3,'2017-05-04'[/quote] 想要什么结果?[/quote] 数据库前面内容不变,想要的结果依然是 01 张三 2 02 李四 2 03 王五 3
听雨停了 2017-09-22
  • 打赏
  • 举报
回复
引用 10 楼 qq_21919381 的回复:
[quote=引用 8 楼 ch21st 的回复:] 另外一种写法

select t.id,r.name,count(time) from (
   select * from #t2 except select * from #t3
) as t 
inner join #t1 as r on r.id=t.id
group by t.id,r.name

  	id	name	(No column name)
	1	张三	2
	2	李四	2
	3	王五	3

那如果第三张表中同一个人有很多条记录,我只想获得最后一个记录起的计数统计怎么做 之前的是一条记录,现在是多条记录,以每个人的最后一条记录来统计每个人的次数[/quote]

--测试数据
if not object_id(N'Tempdb..#T1') is null
    drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
    drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-06-02' union all
select 1,'2017-06-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
    drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union ALL
select 1,'2017-06-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
WITH cte AS (
SELECT distinct a.*,max(b.[TIME]) OVER( PARTITION BY b.id) AS maxtime FROM #T1 a
INNER JOIN #t3 b ON a.id=b.id 
)
SELECT distinct a.id,a.name,COUNT(b.[time]) OVER( PARTITION BY a.id) FROM cte a
LEFT JOIN #T2 b ON a.id=b.id
WHERE b.[time]>a.maxtime

id          name                   
----------- ---------------------- -----------
1           张三                     2
2           李四                     2
3           王五                     3
表三中有多条记录去最大时间的那一条的写法
道素 2017-09-22
  • 打赏
  • 举报
回复
另外一种写法

select t.id,r.name,count(time) from (
   select * from #t2 except select * from #t3
) as t 
inner join #t1 as r on r.id=t.id
group by t.id,r.name

  	id	name	(No column name)
	1	张三	2
	2	李四	2
	3	王五	3

二月十六 2017-09-21
  • 打赏
  • 举报
回复
引用 6 楼 qq_21919381 的回复:
[quote=引用 4 楼 qq_21919381 的回复:] [quote=引用 2 楼 sinat_28984567 的回复:]
--测试数据
if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
	drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN #T3 ON #T3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > #T3.time
GROUP BY #T1.id ,
        #T1.name
那如果是要查第三张表里同一个人最近的日期怎么处理呢?[/quote] 、比如 1,'2017-05-01 2,'2017-05-01 2,'2017-05-02 3,'2017-05-03' 3,'2017-05-04'[/quote] 想要什么结果?
qq_21919381 2017-09-21
  • 打赏
  • 举报
回复
引用 4 楼 qq_21919381 的回复:
[quote=引用 2 楼 sinat_28984567 的回复:]
--测试数据
if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
	drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN #T3 ON #T3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > #T3.time
GROUP BY #T1.id ,
        #T1.name
那如果是要查第三张表里同一个人最近的日期怎么处理呢?[/quote] 、比如 1,'2017-05-01 2,'2017-05-01 2,'2017-05-02 3,'2017-05-03' 3,'2017-05-04'
qq_21919381 2017-09-21
  • 打赏
  • 举报
回复
引用 3 楼 baidu_36457652 的回复:
表间关联就可以了,建议百度先看下语法 印象更深刻
好的 谢谢~
qq_21919381 2017-09-21
  • 打赏
  • 举报
回复
引用 2 楼 sinat_28984567 的回复:
--测试数据
if not object_id(N'Tempdb..#T1') is null
	drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
	drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
	drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
SELECT  #T1.id ,
        #T1.name ,
        COUNT(1) AS 次数
FROM    #T1
        JOIN #T3 ON #T3.id = #T1.id
        JOIN #T2 ON #T2.id = #T1.id
                    AND #T2.time > #T3.time
GROUP BY #T1.id ,
        #T1.name
那如果是要查第三张表里最近的日期怎么处理呢?
qq_21919381 2017-09-20
  • 打赏
  • 举报
回复
大神在哪里?
  • 打赏
  • 举报
回复
表间关联就可以了,建议百度先看下语法 印象更深刻
二月十六 2017-09-20
  • 打赏
  • 举报
回复
--测试数据
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([id] int,[name] nvarchar(22))
Insert #T1
select 1,N'张三' union all
select 2,N'李四' union all
select 3,N'王五'
GO
if not object_id(N'Tempdb..#T2') is null
drop table #T2
Go
Create table #T2([id] int,[time] Date)
Insert #T2
select 1,'2017-05-01' union all
select 1,'2017-05-02' union all
select 1,'2017-05-03' union all
select 2,'2017-05-02' union all
select 2,'2017-05-03' union all
select 2,'2017-05-04' union all
select 3,'2017-05-03' union all
select 3,'2017-05-04' union all
select 3,'2017-05-05' union all
select 3,'2017-05-06'
GO
if not object_id(N'Tempdb..#T3') is null
drop table #T3
Go
Create table #T3([id] int,[time] Date)
Insert #T3
select 1,'2017-05-01' union all
select 2,'2017-05-02' union all
select 3,'2017-05-03'
Go
--测试数据结束
SELECT #T1.id ,
#T1.name ,
COUNT(1) AS 次数
FROM #T1
JOIN #T3 ON #T3.id = #T1.id
JOIN #T2 ON #T2.id = #T1.id
AND #T2.time > #T3.time
GROUP BY #T1.id ,
#T1.name


22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧