SQL語句備忘

2021-07-26 17:19:42 字數 3426 閱讀 3047



--rank 排序(每個分組進行單獨排名,相同名次的會佔位,比如兩個第二名,就沒有第三名了,直接到第四名)

select fenxiaoid,price,

rank() over( --使用rank() 進行排名

partition by fenxiaoid --使用fenxiaoid進行分組

order by price desc    --使用price進行排序

) as paiming

from paymentorder

--dense_rank排序(名次不佔位,其他和rank一樣)

select fenxiaoid,price,

dense_rank() over( --使用dense_rank() 進行排名

partition by fenxiaoid --使用fenxiaoid進行分組

order by price desc    --使用price進行排序

) as paiming

from paymentorder

--ntile()函式進行分組,指定範圍

select fenxiaoid,price,

ntile(5) over( --使用ntile()只能排序直到5

order by price desc    --使用price進行排序

) as paiming

from paymentorder

----ntile()使用場景

select fenxiaoid,price,

case ntile(4) over (order by price desc)  

when 1 then '25%' 

when 2 then '25%-50%' 

when 3 then '50%-75%' 

when 4 then '75%-100%' end as level 

from paymentorder

--row_number() 函式  分頁示例

----建立分頁儲存過程

create proc pro_userinfo_getpageuserinfos

@pagesize int,

@pageindex int,

@totalcount int  output

asselect * from (

select *,row_number() over(order by userid asc) as rownum from userinfo

) as t

where t.rownum between ((@pageindex-1)*@pagesize+1)  and (@pagesize*@pageindex)

select @totalcount=count(1) from userinfo;

--捕獲異常

begin try

select price/0 from paymentorder

end try

begin catch

select error_line(),error_number(),error_severity(),error_message()

end catch

--使用pivot 進行行轉列 [值],行記錄裡有多少值,方括號裡就要寫多少值,注意下

select fenxiaoid,

[1] as "一月份",

[2] as "二月份",

[3] as "三月份",

[4] as "四月份",

[5] as "五月份",

[6] as "六月份",

[7] as "七月份",

[8] as "八月份",

[9] as "九月份",

[10] as "十月份",

[11] as "十一月份",

[12] as "十二月份"

from

(select c.fenxiaoid,sum(c.price) sumprice,month(c.confirmdate) monthnumber from paymentorder c where c.confirmdate>'2016-1-1' group by month(c.confirmdate),c.fenxiaoid) aa

pivot --進行轉列操作

(sum(sumprice)

for monthnumber in

([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])

) as months;

--使用unpivot 進行列轉行

select tableid,ziduan,jiage from (

select top 3 a.tableid,a.payprice,a.price,a.youhui from paymentorder a) aa

unpivot (

jiage

for ziduan in

([price],[payprice],[youhui])

) as unpvt

--將資料刪除,然後將刪除的資料輸出,類似的還有 inserted updated

delete from paymentorder

output deleted.*

where tableid=1

--使用變數的top 查詢

declare @p int

set @p=100

select top(@p) * from paymentorder;

--使用tablesample 示例資料

select * from paymentorder tablesample(10 percent);--獲取10%的資料

select * from paymentorder tablesample(200 rows);--獲取大約200行的資料

--使用累加運算子

declare @i int=2

set @i*=2 --累加運算

print @i;

--一次插入多條資料

create table #t (c1 int,c2 nvarchar(10));

insert #t values (1,'a'),(2,'b')

select c1,c2,c3 from #t

inner join

(values(1,'aa'),(3,'cc') --內連線多條資料

) t (c3,c4) on #t.c1=t.c3

--grouping sets 的使用,不同字段分別分組,然後合併(不同於不同字段同時分組的情況)

select fenxiaoid,xingshiid,count(1) from paymentorder

group by grouping sets(fenxiaoid,xingshiid)

sql語句備忘

1.對一張表分類查詢,同時還需要另一張表的字段 select a.kindid,a.scoreall,b.kindname from select kindid,cast cast sum abs score as decimal 100 as decimal 18,2 as scoreall fr...

sql語句備忘(dba)

user tab comments 表注釋 user col comments 表字段注釋 以上兩個只能獲取自己使用者的表的注釋資訊,如果要訪問自己能夠訪問的其他使用者的表,則需要使用 all tab comments 表注釋 all col comments 表字段注釋 當然,如果有dba許可權,...

幾個SQL語句 備忘

1.三漲停 select biao1.biao1.名稱 from biao1,biao2,biao3 where biao1.漲幅 biao2.漲幅 biao3.漲幅 0.27 and biao1.biao2.and biao2.biao3.and not exists select from bi...