我想把user表的数据插入到临时表,满足的条件如下
-----------------------------------------------------
year和month必须是最高的,也说就说是把时间最晚的插入到临时表就是了
请问我t-sql怎么写
8 个解决方案
#1
select * into # from user a where not exists(
select 1 from tb where year>a.year and (year=a.year and month>a.month))
#2
把上面那些红色的关键字作列名的都用方括号括起来.
#3
select * into #tb from [User] order by year desc,month desc
select * from #tb
#4
select * into #tb from [User] order by [year] desc,[month] desc
select * from #tb
#5
--取一条加top 1
select top 1 * into #tb from [User] order by [year] desc,[month] desc
select * from #tb
#6
Create table #temp
(
ID nvarchar(50),
[year] nvarchar(4),
[month] nvarchar(2)
)
insert into #temp
select top 1 id,[YEAR],[month] from User order by [YEAR],[MONTH] desc
#7
--感觉一楼的带头大哥还靠点谱,其他的还不敢恭维
--但窃以为1楼的语句中and要改为or
--但窃以为1楼的语句中and要改为or
#8
正确,应为 or!
#1
select * into # from user a where not exists(
select 1 from tb where year>a.year and (year=a.year and month>a.month))
#2
把上面那些红色的关键字作列名的都用方括号括起来.
#3
select * into #tb from [User] order by year desc,month desc
select * from #tb
#4
select * into #tb from [User] order by [year] desc,[month] desc
select * from #tb
#5
--取一条加top 1
select top 1 * into #tb from [User] order by [year] desc,[month] desc
select * from #tb
#6
Create table #temp
(
ID nvarchar(50),
[year] nvarchar(4),
[month] nvarchar(2)
)
insert into #temp
select top 1 id,[YEAR],[month] from User order by [YEAR],[MONTH] desc
#7
--感觉一楼的带头大哥还靠点谱,其他的还不敢恭维
--但窃以为1楼的语句中and要改为or
--但窃以为1楼的语句中and要改为or
#8
正确,应为 or!