乙個統計查詢寫的哥要哭了

2021-09-06 04:36:39 字數 4118 閱讀 3307

整了2個小時:

--列出存在的線束

with wiringharness(partno,cartype,partname,setting)as(

select tp.partno,tp.cartype,pp.p_setting setting,pp.p_partname partname from

( select p_partno partno,p_yw cartype from p_partno group by p_partno,p_yw

union

select partno partno, p_yw cartype from inticket where p_bs = '1' group by partno,p_yw

union

select partno partno,p_yw cartype from outticket where p_bs = '1' group by partno,p_yw

)as tp

left join p_partno pp on

tp.partno=pp.p_partno

andtp.cartype=pp.p_yw

),--計算期初庫存

whbeforeinventory(partno,cartype,partname,setting,beforeqty)as(

select *,

( isnull((

select sum(inqty) from inticket

where ticketstatus = '關閉' and p_bs = '1'

and wh.partno=inticket.partno and wh.cartype=inticket.p_yw

and indate < '2011-10-10'

),0)

- --這裡有個減號!!

isnull((

select sum(isnull(a.outqty,0)) from outticketlist a

left join outticket b

on a.partno = b.partno and a.subid = b.subid and a.outticketid = b.outticketid

where a.partno =wh.partno

and a.status = '完成'

and ltrim(rtrim(b.p_yw)) =wh.cartype and b.p_bs = '1' and b.outdate <'2011-10-10'

),0)

)as beforeqty

from wiringharness wh

), --指定時間段內的入庫統計

whin (partno,cartype,sumproductionin,sumrepairin,sumreturnin,sumotherin)as(

select

partno,p_yw cartype,

sum(productionin) sumproductionin,

sum(repairin) sumrepairin,

sum(returnin) sumreturnin,

sum(otherin) sumotherin

from

( select

case when tickettype='生產繳庫' then isnull(inqty,0)

else 0 end as productionin,

case when tickettype='返修入庫' then isnull(inqty,0)

else 0 end as repairin,

case when tickettype='退貨入庫' then isnull(inqty,0)

else 0 end as returnin ,

case when tickettype='其它入庫' then isnull(inqty,0)

else 0 end as otherin,

partno,p_yw

from inticket

where ticketstatus = '關閉' and p_bs = '1'

and indate >='2011-10-10' and indate <='2011-11-3 23:59:59'

) as t01

group by partno, p_yw

), --指定時間段內的出庫統計

whout(partno,cartype,sumrepairout,sumtransferout,sumoutplanout,sumotherout,sumretailout)as(

select

partno,cartype,

sum(repairout) sumrepairout,

sum(transferout) sumtransferout,

sum(outplanout) sumoutplanout,

sum(otherout) sumotherout,

sum(retailout) sumretailout

from

( select

b.partno,b.p_yw as cartype,

case when ticktype='返修出庫' then isnull(a.outqty,0)

else 0 end as repairout,

case when ticktype='轉移出庫' then isnull(a.outqty,0)

else 0 end as transferout,

case when ticktype='計畫外出庫' then isnull(a.outqty,0)

else 0 end as outplanout ,

case when ticktype='其它出庫' then isnull(a.outqty,0)

else 0 end as otherout,

case when ticktype='個體零售' then isnull(a.outqty,0)

else 0 end as retailout

from outticketlist a

left join outticket b

on a.partno = b.partno and a.subid = b.subid and a.outticketid = b.outticketid

where a.status = '完成' and b.p_bs = '1'

and outdate >='2011-10-10' and outdate <='2011-11-3 23:59:59'

) as t02

group by partno,cartype

)select

*,beforeqty+totalin-totalout as inventory

from(

select

whb.*,

sumproductionin,sumrepairin,sumreturnin,sumotherin,

sumrepairout,sumtransferout,sumoutplanout,sumotherout,sumretailout,

(isnull(sumproductionin,0)+isnull(sumrepairin,0)+isnull(sumreturnin,0)+isnull(sumotherin,0)) as totalin ,

(isnull(sumrepairout,0)+isnull(sumtransferout,0)+isnull(sumoutplanout,0)+isnull(sumotherout,0)+isnull(sumretailout,0)) as totalout

from

whbeforeinventory whb

left join whin on whb.partno=whin.partno and whb.cartype=whin.cartype

left join whout on whb.partno=whout.partno and whb.cartype=whout.cartype

) as t001

python寫乙個服務 Python寫乙個服務

coding utf 8 import json from urllib.parse import parse qs from wsgiref.server import make server 定義函式,引數是函式的兩個引數,都是python本身定義的,預設就行了。定義檔案請求的型別和當前請求成功...

sql查詢,nolock寫還是不寫,這是乙個問題

在做過的很多專案中,發現大家不管對什麼表,逢select必定加上nolock 或者with nolock 好像已經是制度化的一種東西。有領導高人解釋說加上nolock可以提高查詢速度,不影響對資料表的其他併發操作。但是真有必要每個查詢都加nolock嗎?個人認為加不加nolock還是值得我們根據實際...

sql查詢,nolock寫還是不寫,這是乙個問題

在做過的很多專案中,發現大家不管對什麼表,逢select必定加上nolock 或者with nolock 好像已經是制度化的一種東西。有領導高人解釋說加上nolock可以提高查詢速度,不影響對資料表的其他併發操作。但是真有必要每個查詢都加nolock嗎?個人認為加不加nolock還是值得我們根據實際...