sql臨時表 轉換字段 百分比(金橋總報表)

2022-08-30 14:36:16 字數 4555 閱讀 1308

--select *  from @pt_tball

create proc asset_querymainlistforreport

(@pi_action varchar(100),

@pi_propertycode varchar(100),

@pi_pageindex int,

@pi_pagesize int,

@po_recordcount bigint output)as

declare @pt_startrowindex int

declare @pt_endrowindex int

set @pt_startrowindex = (@pi_pageindex - 1) * @pi_pagesize + 1

set @pt_endrowindex = @pi_pageindex * @pi_pagesize

if @pi_action = 'getlist'

begin

--臨時表的使用

--宣告臨時表

declare  @pt_tball table(totalshop varchar(100) ,shoparea varchar(100),rendshop varchar(100),rendshoparea varchar(100),unrendshop varchar(100),unrendshoparea varchar(100),unrendpercent varchar(100),rendpercent varchar(100),rendshopmoney varchar(100))

--插入臨時表

insert into @pt_tball

--(查詢出的現實所需的表)

--檢視querymainlist

--select    dbo.asset_shopinfo.square,dbo.asset_shopinfo.rentstatus,dbo.asset_shopinfo.shopcode,dbo.asset_contract.rendstartdate,

dbo.asset_contract.totalamount,

dbo.asset_shopinfo.propertycode

--from         dbo.asset_contract inner join

dbo.asset_contractshopinfo on dbo.asset_contract.contractcode = dbo.asset_contractshopinfo.contractcode right outer join

dbo.asset_shopinfo on dbo.asset_contractshopinfo.shopcode = dbo.asset_shopinfo.shopcode

--總商鋪數量

select (select count() shopcodefrom querymainlist) as totalshop,

--總商鋪面積

轉換字段,兩種方法:

cast(num as int )

convert(int,num)

(select sum(cast (squarenew as float)) as aa

from

(select 'squarenew'=(

case

when isnumeric ([square])=0 then '0'

when isnumeric ([square])>0 then [square]

end) from querymainlist

)tb) as shoparea,

--總已租商鋪數量

(select count(shopcode) from querymainlist where rentstatus=2) as rendshop,

--總已租商鋪面積

(select sum(cast (squarenew as float)) as bb

from

(select 'squarenew'=(

case

when isnumeric ([square])=0 then '0'

when isnumeric ([square])>0 then [square]

end) from querymainlist

where rentstatus=2

)tb) as rendshoparea,

--總未租商鋪數量

(select count(shopcode) from querymainlist where rentstatus=0) as unrendshop,

--總未租商鋪面積

(select sum(convert(float,squarenew)) as cc  from

(select 'squarenew'=(

case

when isnumeric ([square])=0 then '0'

when isnumeric ([square])>0 then [square]

end) from querymainlist

where rentstatus=0

)tb) as unrendshoparea,

--已租商鋪數量百分比

--求百分比

--cast(cast(分子 *1.0*100/cast(cast(分母 as decimal(10,2)) as varchar(50)) +'%')

(select cast(cast((select count(shopcode) from querymainlist where rentstatus=2) *1.0*100/cast(cast((select count(shopcode(select count(shopcode) from querymainlist) as decimal(10,2)) as varchar(50)) +'%') as unrendpercent,

--已租商鋪面積百分比

(select cast(cast((select sum(cast (squarenew as float)) as bb

from

(select 'squarenew'=(

case

when isnumeric ([square])=0 then '0'

when isnumeric ([square])>0 then [square]

end) from querymainlist

where rentstatus=2

)tb) *1.0*100/(select sum(cast (squarenew as float)) as aa

from

(select 'squarenew'=(

case

when isnumeric ([square])=0 then '0'

when isnumeric ([square])>0 then [square]

end) from querymainlist

)tb) as decimal(10,2)) as varchar(50)) +'%') as rendpercent,

--檢視中總金額重複(因為獲取的是shopinfo表中的全部,所以對應的商鋪後totalamount欄位就有合同當中的總值)distinct重複列

sum (distinct totalamount) as rendshopmoney from querymainlist

--租賃開始日期條件

where rendstartdate  between 

--轉換得到的日期格式:111:2012/8/27

convert(varchar(100),

--獲取本年的第一天日期

dateadd(yy, datediff(yy,0,getdate()), 0) , 111

) and getdate()

select * from

(select

*,row_number() over (order by totalshop desc) as rowindex

from @pt_tball

) tba

where rowindex between @pt_startrowindex and @pt_endrowindex

--得到總數

select @po_recordcount = count(*)

from         @pt_tball

--where asset_shopinfo.propertycode = @pi_propertycode

end

SQL 總合百分比

算出累積總計是乙個常見的需求,可惜以 sql 並沒有乙個很直接的方式達到這個需求。要以 sql 算出累積總計,基本上的概念與列出排名類似 第一是先做個 自我鏈結 self join 然後將結果依序列出。在做列出排名時,我們算出每一行之前 包含那一行本身 有多少行數 而在做累積總計時,我們則是算出每一...

css百分比定位和百分比尺寸

只有設定了定位的 relative,absolute,fixed 的元素才有left,top等屬性。子元素relative定位 百分比定位和百分比尺寸都是相對于父元素,無論父元素有沒有定位 子元素absolute定位 百分比定位和百分比尺寸都是相對於最近的定位了的祖先元素,如果沒有則相對於視窗。可以...

sql操作歸類百分比

原資料 專案名稱 完成否 主閘 是 主閘 是 主閘 否 發動機 是 發動機 否 發動機 否 鏈條 是 鏈條 是 鏈條 否 想要得到的資料 專案名稱 完成百分比 主閘 2 3 發動機 1 3鏈條 2 3sql語句應該如何寫?select name,convert varchar 100 sum cas...