當前位置:首頁 » 編程語言 » sql如何統計連續編號個數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql如何統計連續編號個數

發布時間: 2023-04-12 22:45:48

sql 連續編號

因為你的欄位全敬喚或是數字,所以可以用max()函數來取最鏈跡大值,然後轉為int,加1,再轉為亮伍char(6). 如果你覺得6位太長,可以把下面代碼中的6改稱你要的長度。

先創建一個自定義函數:
create function f_getMaxCode()
returns varchar(6)
as
begin
declare @maxid int
declare @maxcode varchar(6)
set @maxcode=''
select @maxid=Cast(isnull(max(bh欄位名),0)+1 as int) from 表名
set @maxcode=cast(@maxid as varchar(6))
while len(@maxcode)<6
begin
set @maxcode='0' + @maxcode
end
return @maxcode
end

在insert的時候:
insert into 表名(bh欄位名,....)
values(dbo.f_getMaxCode(),....)

② SQl資料庫自動編號(連續)

假如你的原來的表tb有
id,name,address三列
其中id是自動增長列,
刪除其中若干行以後,
你可以這樣啊
創建存儲過程
create proc getTempTable
as
begin
declare @temp table
(
id int identity(1,1),
name varchar(20),
address varchar(20)
)
insert @temp
select name, address from tb

select * from @temp
end
這樣你就可以得到一個ID連續的新表了

③ 關於如何用sql語句查詢出連續的一串數字

從資料庫取出來字元串後
用split截成數組,然後循環數組判斷
string s="1,2,4,5,7,8,9,10";
int c=0;
int a=0;
int[] ss=s.split(",");
for(int i=0;i{
int b=int.Parse(ss[i]);

if(i==0)

{
a=b;

}
else

{
if(c==4)

{
break;

//連續達到4個,跳出循環

}
a++;

if(a==b)

{
c++;
//符合繼續

}

else

{
break;//不符合

}

}

}

④ sql求連續出現的數字

distinct關鍵字進行去重。sql求連續出現的數字可將單表查詢多次,用where條件組合,用distinct關鍵字進行去重。

⑤ sql如何進行多個欄位的統計個數

一種查詢SQL如下, 利用union獲得b和c各自的統計結果, 然後再一次統計整合到最終結果:

selectsum(d.b_cnt)+sum(d.c_cnt)astotal_cnt,sum(d.b_cnt)asb_cnt,case襪或whensum(d.b_cnt)=0then''elsed.valendasb_label,sum(d.c_cnt)asc_cnt,casewhensum(d.c_cnt)=0then''elsed.valendasc_labelfrom(selectbasval,count(b)asb_cnt,0asc_,0,count(c)asc_cntfromAgroupbyc)dgroup宴銀byd.val

SQLSerer上的測試結果(欄位次序有變化),

total_cnt為總數, b_label為b欄值, b_cnt為b欄個數, c_labe為c欄值, c_cnt為c欄個數.

這個結果跟欄位是否晌好宴為整型無關, 它是統計記錄出現的次數.

⑥ sql如何根據人名統計人數,如 編號 姓名 0001 張三 0002 李四 0003 王五 ---------------- 3

select dinstinct 姓名銀賀手 from 你的表
union
select '累計數拍伍量:'+cast(count(distinct 姓名鋒嫌) as varchar) from 你的表

⑦ sql統計連續相同數值的次數

給個思路你吧。先給表排列序號(類似ID來使用),然後寫個循環,記錄Failed次數,並且對比UUT_Staus值是否Failed,如果不為Failed,並且Failed大於1的,則把上一條記錄插入到臨時表中,最後再查找臨時表。

⑧ SQL資料庫中查詢連續編號的的數據。


DECLARE@T1table(UserIDint,[name]nvarchar(50),numint);
insertinto@T1(UserID,[name],num)values(1001,'a',8)
insertinto@T1(UserID,[name],num)values(1002,'b',6)
insertinto@T1(UserID,[name],num)values(1003,'c',8)
insertinto@T1(UserID,[name],num)values(1004,'a',8)
insertinto@T1(UserID,[name],num)values(1005,'b',8)

select*from@t1
selecta.*from
(selectUserID,[name],[num]from@t1)ajoin
(selectUserID,[name],[num]from@t1)bona.UserID<b.UserIDanda.UserID+1=b.UserID
anda.num=b.num
groupbya.userid,a.[name],a.[num]


得到結果:1003c8
1004a8

⑨ sql怎樣輸出連續相同個數

--方法一

--1.創建表

Create Table T

(

ip varchar(10),

賬號 Varchar(10),

序列 int,

結果 int

)


--2.測試數據

insert into T values('001','張三', 0)

insert into T values('001','張三', 0)

insert into T values('001','張三', 1)

insert into T values('001','張三', 0)

insert into T values('001','張三', 0)

insert into T values('001','李四', 0)

insert into T values('001','李四', 1)

insert into T values('002','王五', 1)

insert into T values('002','王五', 0)

insert into T values('002','王五', 0)

insert into T values('002','王五', 0)


--3.計算連續的號碼

Declare @ip varchar(10)

Declare @賬號 Varchar(10)

Declare @序列 int

Declare @I int=1

update T Set

結果=@I

,@I=Case When @ip=ip and @賬號=賬號 And @序列=序列 And 序列=0 Then @I+1 Else 1 End

,@ip=ip,@賬號=賬號 ,@序列=序列

--4.查詢結果(相同ip中相同賬號的序列中連續為0的個數是計算出來了)

--5.你要的那種效果還得繼續處理

select * from T


⑩ SQL怎麼統計個數

不同資料庫的系統表可能不一樣,比如informix就是systables

tabname。
informix資料庫:
統計個數:
select
count(*)
from
systables
where
tabname
like
'%abc%'
查看錶:
select
tabname
from
systables
where
tabname
like
'%abc%'
其他資料庫的話,系統表可能是sysobjects,對應的列可能也有不同,看你的情況改吧。