select CustomerName,CreateDate from table1 t1,(select CustomerName,max(CreateDate) from table1 group by CustomerName) t2 where t1.CustomerName=t2.CustomerName and t1.CreateDate=t2.CreateDate
honestface(哗拉拉) 可以解释一下么?
如果 table2: CustomerName CreateDate Column3 a 2004-11-7 345 a 2004-11-6 345658 b 2003-11-7 8574 b 2004-11-7 789 查询结果: CustomerName CreateDate a 2004-11-7 b 2004-11-7xian现在的SQL语句应该怎么写呢?
不好意思,上面更新为: 如果 table2: CustomerName CreateDate Column3 a 2004-11-7 345 a 2004-11-6 345658 b 2003-11-7 8574 b 2004-11-7 789 查询结果: CustomerName Column3 a 345 b 789现在的SQL语句应该怎么写呢?
select customerName,Min(column3) from table2 order by customerName
select customername,max(createdate) from table1 group by customername
select customerName,Min(column3) as column3 from table2 order by customerName
如果 table2: CustomerName CreateDate Column3 a 2004-11-7 345 a 2004-11-6 345658 b 2003-11-7 8574 b 2004-11-7 789456 查询结果: CustomerName Column3 a 345 b 789456 (这里的查询根据还是CreateDate) SQL应该如何写?
select CustomerName,Column3 from table2 aa where CreateDate=(select top 1 createdate from table2 where CustomerName=aa.CustomerName)
select customerName,column3 from table2 where CreateDate=(select max(createdate) from table2 )order by customerName
lsxaa(小李铅笔刀)--> select CustomerName,Column3 from table2 aa where CreateDate=(select top 1 createdate from table2 where CustomerName=aa.CustomerName)这个语句比较接近,但是没有实现.. 我试过后,发现要加上这句才可以:ORDER BY createtime DESC 全句为: select CustomerName,Column3 from table2 aa where CreateDate=(select top 1 createdate from table2 where CustomerName=aa.CustomerName ORDER BY createtime DESC)
select a.CustomerName, (select top 1 b.Column3 from table1 b where a.CustomerName = b.CustomerName order by CreateDate desc ) as Column3 from (select CustomerName from table1 group by CustomerName) a
select CustomerName,Column3 from table2 aa where CreateDate=(select top 1 createdate from table2 where CustomerName=aa.CustomerName ORDER BY createtime DESC)
THX lsxaa(小李铅笔刀) victorycyz(中海) .... ---------------------------- 这是我的一个扩展问题... 如果现在SQL语句写成这样: select a.CustomerName, (select top 1 b.Column3 from (select * from table2) b where a.CustomerName= b.CustomerName order by CreateDate desc ) as Column3 from (select CustomerName from (select * from table2) c group by CustomerName) a 你会发现,其中出现了两个(select * from table2) 我可不可以只使用其中一句 建表d ,另一句直接直接调用表d ? 谢谢!
select CustomerName, max(CreateDate) from t group by CustomerName
表d 是什么?select a.CustomerName, (select top 1 b.Column3 from table2 b --这里直接这样写 where a.CustomerName= b.CustomerName order by CreateDate desc ) as Column3 from (select CustomerName from table2 c --这里直接这样写 group by CustomerName) a
select CustomerName,Column3 from table2 aa where CreateDate=(select top 1 createdate from table2 where CustomerName=aa.CustomerName ORDER BY createtime DESC)
create table table2(CustomerName nvarchar(5),CreateDate datetime,Column3 int)insert into table2 SELECT 'a', '2004-11-7', 345 union all SELECT 'a', '2004-11-6', 345658 union all SELECT 'b', '2003-11-7', 8574 union all SELECT 'b', '2004-11-7', 789456 union all SELECT 'b', '2004-11-7', 78945 go SELECT CustomerName,Column3 FROM table2 b WHERE CreateDate in (SELECT TOP 1 CreateDate FROM table2 WHERE CustomerName=b.CustomerName ORDER BY CreateDate DESC ) a 345 b 789456 b 78945
可以解释一下么?
table2:
CustomerName CreateDate Column3
a 2004-11-7 345
a 2004-11-6 345658
b 2003-11-7 8574
b 2004-11-7 789
查询结果:
CustomerName CreateDate
a 2004-11-7
b 2004-11-7xian现在的SQL语句应该怎么写呢?
如果
table2:
CustomerName CreateDate Column3
a 2004-11-7 345
a 2004-11-6 345658
b 2003-11-7 8574
b 2004-11-7 789
查询结果:
CustomerName Column3
a 345
b 789现在的SQL语句应该怎么写呢?
from table1
group by customername
table2:
CustomerName CreateDate Column3
a 2004-11-7 345
a 2004-11-6 345658
b 2003-11-7 8574
b 2004-11-7 789456
查询结果:
CustomerName Column3
a 345
b 789456
(这里的查询根据还是CreateDate)
SQL应该如何写?
from table2 aa
where CreateDate=(select top 1 createdate
from table2
where CustomerName=aa.CustomerName)
from table2 )order by customerName
select CustomerName,Column3
from table2 aa
where CreateDate=(select top 1 createdate
from table2
where CustomerName=aa.CustomerName)这个语句比较接近,但是没有实现..
我试过后,发现要加上这句才可以:ORDER BY createtime DESC
全句为:
select CustomerName,Column3
from table2 aa
where CreateDate=(select top 1 createdate
from table2
where CustomerName=aa.CustomerName
ORDER BY createtime DESC)
(select top 1 b.Column3
from table1 b
where a.CustomerName = b.CustomerName
order by CreateDate desc ) as Column3
from (select CustomerName
from table1
group by CustomerName) a
from table2 aa
where CreateDate=(select top 1 createdate
from table2
where CustomerName=aa.CustomerName
ORDER BY createtime DESC)
----------------------------
这是我的一个扩展问题...
如果现在SQL语句写成这样:
select a.CustomerName,
(select top 1 b.Column3
from (select *
from table2) b
where a.CustomerName= b.CustomerName
order by CreateDate desc ) as Column3
from (select CustomerName
from (select *
from table2) c
group by CustomerName) a 你会发现,其中出现了两个(select *
from table2)
我可不可以只使用其中一句 建表d ,另一句直接直接调用表d ?
谢谢!
from t
group by CustomerName
(select top 1 b.Column3
from table2 b --这里直接这样写
where a.CustomerName= b.CustomerName
order by CreateDate desc ) as Column3
from (select CustomerName
from table2 c --这里直接这样写
group by CustomerName) a
from table2 aa
where CreateDate=(select top 1 createdate
from table2
where CustomerName=aa.CustomerName
ORDER BY createtime DESC)
union all SELECT 'a', '2004-11-6', 345658
union all SELECT 'b', '2003-11-7', 8574
union all SELECT 'b', '2004-11-7', 789456
union all SELECT 'b', '2004-11-7', 78945
go
SELECT CustomerName,Column3 FROM table2 b
WHERE CreateDate in (SELECT TOP 1 CreateDate FROM table2
WHERE CustomerName=b.CustomerName
ORDER BY CreateDate DESC
)
a 345
b 789456
b 78945