函式編寫SQL如何給記錄加序號,函式編寫SQL時如何給記錄加序號?

2022-03-02 01:01:43 字數 5416 閱讀 9160

1樓:du瓶邪

給查詢出的sql記錄新增序號列,解決方法有以下兩種第一:select row_number() over (order by a.欄位 asc) as xuhao,a.

* from table a

(table 為表名,欄位為表a中的欄位名)第二:select rank() over (order by a.欄位 asc) as xuhao,a.* from table a

(table 為表名,欄位為表a中的欄位名)

2樓:匿名使用者

lect裡面的列的個數只能是固定的,所以我覺得,如果zm裡的資料是動態的話,你必須動態生成這個語句。

select fjname, name, hm,

max(decode(zm, 'aaa', zm, null )) zm1, max(decode(zm, 'aaa', charge, null )) zm1charge,

max(decode(zm, 'bbb', zm, null )) zm2, max(decode(zm, 'bbb', charge, null )) zm2charge,

max(decode(zm, 'ccc', zm, null )) zm3, max(decode(zm, 'ccc', charge, null )) zm3charge,

max(decode(zm, 'ddd', zm, null )) zm4, max(decode(zm, 'ddd', charge, null )) zm4charge

from owe

group by fjname, name, hm;

另外,團idc網上有許多產品**,便宜有口碑

3樓:鍾

新建一個屬性,在是否自動新增還是什麼來著,點選「是」,增值為1

4樓:匿名使用者

有五種方法:

一、需要用臨時表來實現

select identity(int, 1,1) as id_num,* into #temp from 表

select * from #temp

drop table #temp

二、不用臨時表,就必須有排序列,值唯一,做參考:

select (select count(*) from yourtable where col <= a.col) row, * from yourtable a order by col

三、在原表中增加一列來實現

alter table yourtable add id int identity

select * from yourtable

alter table yourtable drop column id

四、 使用sql server 2005 獨有的rank() over () 語法(測試 客戶編號 也應該值唯一才對)

select rank() over (order by 客戶編號 desc) as 序號, 客戶編號,公司名稱 from 客戶

五、select 序號= count(*), a.客戶編號, b.公司名稱

from 客戶 as a, 客戶as b where a.客戶編號》= b.客戶編號

group by a.客戶編號, a.公司名稱

order by 序號

5樓:匿名使用者

記錄是查詢出來的還是原始的資料記錄呢?

如果是查詢出來的或者增加記錄:

identity(函式)

只用在帶有 into table 子句的 select 語句中,以將標識列插入到新表中。

儘管類似,但是 identity 函式不是與 create table 和 alter table 一起使用的 identity 屬性。

語法identity ( data_type [ , seed , increment ] ) as column_name

引數data_type

標識列的資料型別。標識列的有效資料型別可以是任何整數資料型別分類的資料型別(bit 資料型別除外),也可以是 decimal 資料型別。

seed

要指派給表中第一行的值。給每一個後續行指派下一個標識值,該值等於上一個 identity 值加上 increment 值。如果既沒有指定 seed,也沒有指定 increment,那麼它們都預設為 1。

increment

用來新增到 seed 值以獲得表中連續行的增量。

column_name

將插入到新表中的列的名稱。

返回型別

返回與 data_type 相同的型別。

6樓:匿名使用者

序號不是insert時候就帶上的嗎

函式編寫sql時如何給記錄加序號?

7樓:匿名使用者

有五種方法:

一、需要用臨時表來實現

select identity(int, 1,1) as id_num,* into #temp from 表

select * from #temp

drop table #temp

二、不用臨時表,就必須有排序列,值唯一,做參考:

select (select count(*) from yourtable where col <= a.col) row, * from yourtable a order by col

三、在原表中增加一列來實現

alter table yourtable add id int identity

select * from yourtable

alter table yourtable drop column id

四、 使用sql server 2005 獨有的rank() over () 語法(測試 客戶編號 也應該值唯一才對)

select rank() over (order by 客戶編號 desc) as 序號, 客戶編號,公司名稱 from 客戶

五、select 序號= count(*), a.客戶編號, b.公司名稱

from 客戶 as a, 客戶as b where a.客戶編號》= b.客戶編號

group by a.客戶編號, a.公司名稱

order by 序號

如何寫sql,讓select出的資料帶著序號

8樓:匿名使用者

--用row_number() over(order by )

select row_number() over(order by f1) as 序號,* from t_test

sql 查詢語句自動增加序號

9樓:匿名使用者

.使用臨時表實現

sql的identity函式可以提供自增的序號,但只能用在帶有into table子句的select語句中,所以如果可以使用臨時表的情況下可以使用這種實現方法。

eg:select identity(int,1,1) as seq,field1,field2,...,fieldn into tmptablename from srctablename;

select * from tmptablename;

drop table tmptablename;

10樓:

自增序列的增刪改的問題,我覺得這個部落格總結的不錯:

網頁連結

sql語句裡如何實現給查詢記錄新增自然序號?

11樓:匿名使用者

通過rownum實現即可。

sql:select rownum , * from tablename where 條件語句。

解釋:rownum是隱藏的,查詢結果預設從1開始編號,所以肯定會是自然編號的,有多少條,編號就到多少。

12樓:匿名使用者

select row_number() over (order by a.id asc)as 序號,a.* from table a

或者:select rank() over (order by a.id asc )as 序號,a.* from table a

table寫表名。

13樓:匿名使用者

select (@rowno := @rowno+1) as rowno,username,cardnum from (select username,cardnum from t_consumer where 1=1) a,(select @rowno :=0) b

14樓:

select @rownum:=@rownum+1 as rownum, a.*

from (select @rownum:=0) r, a;

15樓:心翼

新增欄位,使用自動增長

如何用sql排序然後結果帶序號

16樓:育知同創教育

比如:select 學號,日期row_number() over(partition by 學號 order by  學號) from a表.

17樓:斷的刃

不太記得了,應該是下面這樣寫

select rownum , a,b from tablename order by a,b

rownum不知道有沒有拼錯

18樓:啊啊啊啊

row_number()over(order by name)as rn

問一個sql查詢,如何給查詢結果加上序號?

19樓:匿名使用者

sql server 下

1、select row_number()  over(order by id) rownu,a1 from ta1

解說:在這裡,ta1是一個表,a1是表中的一個欄位,表的另一個欄位為id本用於自增這兒用來排序。

sql server 中的 row_number() 得到一個查詢出的順序,但這個函式要求給出一個查的排序方案,因為sql server的儲存是無關順序的。

在oracle裡,本就有rownum。可直接用:

1、select rownum,a1 from ta1其它的資料庫可能有別的方案,不一而論。

參考資料

sql中如何給變數賦值,Sql中如何給變數賦值

declare n1 int,n2 varchar 10 set n1 select age from table where column set n2 select gender from table where column 或者一起賦值 就是樓上那個 declare n1 int,n2 va...

1編寫函式,計算兩整數和,然後編寫主函式呼叫並驗證

include int sum int a,int b int main 編寫函式,計算兩整數和,然後編寫主函式呼叫並驗證。inthcf intx,inty 定義最大公約數函式for i x i 定義最小公 倍數函式for i y 1 i returni voidmain c語言.定義一個函式,用於...

資料庫怎麼編寫儲存過程,用SQL怎樣編寫一個儲存過程?

sql server的語法 create procedure proc name para1 int assql statement mysql的語法 create procedure proc name para1 int sql statement 上面的para1是引數,如果不需要可以省略括號...