列1 列2
Q1 1
Q11 7
Q2_1 2
Q2_2 2
Q2_3 2
Q31_1 4
Q444_3 5
Q444_5 5 我想查询结果是:
Q1 1
Q11 7
Q2_1 2
Q31_1 4
Q444_3 5
Q1 1
Q11 7
Q2_1 2
Q2_2 2
Q2_3 2
Q31_1 4
Q444_3 5
Q444_5 5 我想查询结果是:
Q1 1
Q11 7
Q2_1 2
Q31_1 4
Q444_3 5
解决方案 »
- 求一条sql语句,咋试都不行,只能来这里求助了
- sql怎么写?数量累计合计
- 关于SQL Having 子句的发现
- 菜鸟的 模糊查询问题 谢谢
- 大家帮忙看下,我搞不定了在线等待解决
- 太难了。请教如何制作这样复合的报表!!!(用存储过程加游标)
- update 表一对多 的语句
- 请求解决分类汇总问题
- ORACL表中的某个字段的数据是‘aBcD’时,我们怎么才能把他不区分大小写检索出来!
- 请帮忙(vb+ado+mssql7),急,急,急!!!!
- 请问windows server2008 下可否安装sqlserver2000?
- 100分请教:读取EXCEL中某列的值时,如果该列设置了自定义的格式,怎么处理?
declare @tb table (列1 varchar(6),列2 int)
insert into @tb
select 'Q1',1 union all
select 'Q11',7 union all
select 'Q2_1',2 union all
select 'Q2_2',2 union all
select 'Q2_3',2 union all
select 'Q31_1',4 union all
select 'Q444_3',5 union all
select 'Q444_5',5select * from @tb t
where not exists(select * from @tb where 列2=t.列2 and right(列1,1)< right(t.列1,1))列1 列2
------ -----------
Q1 1
Q11 7
Q2_1 2
Q31_1 4
Q444_3 5(5 行受影响)
union all
select * from tb k
where not exists(select * from tb
where left(列1,charindex('_',列1)-1)=left(列1,charindex('_',k.列1)-1)
and stuff(列1,1,charindex('_',列1),'')<stuff(列1,1,charindex('_',k.列1),'')
select min(列1), 列2 from table group by 列2