sql分組查詢的完整語句
分組查詢 group by 主要是對(count,sum,avg,min,max)
例如
表A
a b c
一 1 2
一 1 3
二 2 5
三 7 7
二 9 9
select a,sum(b),sum(c) from A group by a
對分組數據進行限制
select a,sum(b),sum(c) from A group by a having sum(b)>5
sql分組查詢問題(急)100分
select *,sum(case when name='ak47' then * else 0 end)ak47,sum(case when name='m4' then * else 0 end)m4from test aagroup by *不知道到底是想實現什么功能,反正我這個可以實現你說的那樣。
答案補充select data,sum(case when name='ak47' then num else 0 end)ak47,sum(case when name='m4' then num else 0 end)m4from tgroup by data忘記了是t表,呵呵。
sql字符串 分組語句
新建一個找到第N個字串起始位置的函數charindexN
create function *dexN (@indexStr varchar(100)='',--要查找的子串
@str varchar(2000)='', --字符串
@n int=null) --從第n個子串開始查
returns int
begin
declare @tmp varchar(2000) --
declare @m int
declare @i int
set @tmp=@str
set @i=1
set @m=0
while charindex(@indexStr,@tmp)>0 and @i@n set @m=0
return @m
end
select substring(*,1,*dexN('*',*,1)-1) xsgg,count(*) num from T1 a
group by substring(*,1,*dexN('*',*,1)-1)
order by count(*) desc;
select substring(*,1,*dexN('*',*,2)-1) xsgg,count(*) num from T1 a
group by substring(*,1,*dexN('*',*,2)-1)
order by count(*) desc;
create table T1
( xsgg nvarchar(25), num int)
insert into T1(xsgg,num)
select '20*2000*11500',1 union
select '0.25*1500*2000',1 union
select '20*1500*9000',1 union
select '20*2000*8000',1