select dt,max(sign) from a
group by month(dt)
group by month(dt)
解决方案 »
- 关于通过“链接服务器”的方式,调用目标服务器上的存储过程的问题。
- 求一类查询SQL语句的写法??
- *********** ??? 求教一挑战性排序方法 ???? *******************************************************
- SQL统计问题,立即解决,立即给分
- 菜鸟第三问……
- 数据转换的中文乱码问题
- 数据库表之间复制
- 一个简单的SQL语句
- 在实际开发中如何使用SQL Server中的游标?
- sql注入攻击
- 问:如何在存储过程中定义变量类型为ntext
- 请问 在用 备份 语句 备份时 能不能 只备份 指定的一些表, 就是不全部表备份 , 能不能实现 ???????? 急!!!
Inner Join (Select Month(dt) As dt,Max([sign]) As [sign] from T1 Group By Month(dt)) B
On Month(A.dt)=B.dt And A.[sign]=B.[sign]
2005-07 2
2005-08 4无法得到详细日期
Create Table TEST
(dt Varchar(10),
sign Int)
Insert TEST Values('2005-07-01', 1)
Insert TEST Values('2005-07-12', 2)
Insert TEST Values('2005-08-01', 1)
Insert TEST Values('2005-08-11', 3)
GO
--测试
Select A.* from TEST A
Inner Join (Select Month(dt) As dt,Max([sign]) As [sign] from TEST Group By Month(dt)) B
On Month(A.dt)=B.dt And A.[sign]=B.[sign]
GO
--删除测试环境
Drop Table TEST
GO
--结果
/*
dt sign
2005-07-12 2
2005-08-11 3
*/
Create Table TEST
(dt Varchar(10),
sign Int)
Insert TEST Values('2005-07-01', 1)
Insert TEST Values('2005-07-12', 2)
Insert TEST Values('2005-08-01', 1)
Insert TEST Values('2005-08-11', 3)
GO
--测试
Select A.* from TEST A
Inner Join (Select Left(dt,7) As dt,Max([sign]) As [sign] from TEST Group By Left(dt,7)) B
On Left(A.dt,7)=B.dt And A.[sign]=B.[sign]
GO
--删除测试环境
Drop Table TEST
GO
--结果
/*
dt sign
2005-07-12 2
2005-08-11 3
*/
create table T1
(
dt varchar(10),
sign int
)
insert T1
select '2005-07-01',1 union
select '2005-07-12',2 union
select '2005-08-01',1 union
select '2005-08-11',3--测试
select distinct a.* from T1 a left join T1 b
on year(a.dt)=year(b.dt) and month(a.dt)=month(b.dt) and a.sign<b.sign
where b.dt is null--删除测试环境
drop table T1
(所影响的行数为 4 行)dt sign
---------- -----------
2005-07-12 2
2005-08-11 3(所影响的行数为 2 行)
当将两个表作按他写的关键字连接时, 每组最小值记录对应的b.dt的值
正好是null, 所以他那样写也是对的.
你还可以将这个表连接写成嵌套查询的形式.楼主最后提的那个问题可以试试(没试过, 觉得应该快一些):
select smon
from (select distinct smon=convert(varchar(10),salary_mon,120)
from employee_basesalary)t
order by smon desc
select b.dt,a.sign from
(select month(dt)dt,max(sign) sign from t1 group by month(dt)) a,
t1 b where datepart(mm,b.dt)=a.dt and a.sign=b.sign
create table table1(dt datetime,sign1 int)
go
insert table1 select '2005-07-01',1
union all select '2005-07-12',2
union all select '2005-08-01',1
union all select '2005-08-11',3--找出同一月里 sign最大的 dtselect * from table1 where datepart(dd,dt) in (select max(datepart(dd,dt))
from table1 group by datepart(mm,dt))
where sign
in //得出每个月的最大值
(select max(sign) from test2
group by substring(dt,1,7))