一个工作中的Sql(Sql server)

 Code
Codeselect cast('2008'+'-'+'07'+'-01' as datetime),dateadd(mm,1,cast(('2008'+'-'+'7'+'-01') as datetime))

 Code
Codesql server 里面一个需要看看的sql
SELECT CODE_TS AS CODE, G_NAME, 'BASE_HSCODE' AS TYPE,
(SELECT NAME + '(' + CODE + ')' AS Expr1
FROM dbo.BASE_UNIT
WHERE (T.UNIT_1 = CODE)) AS UNIT_1,
(SELECT NAME + '(' + CODE + ')' AS Expr1
FROM dbo.BASE_UNIT AS BASE_UNIT_2
WHERE (T.UNIT_2 = CODE)) AS UNIT_2, LOW_RATE * 100 AS LOW_RATE, HIGH_RATE * 100 AS HIGH_RATE, TAX_RATE * 100 AS TAX_RATE
FROM dbo.BASE_HSCODE AS T

 Code
Codeset ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER procedure [dbo].[sp_statistics_depot]
@year nvarchar(4),
@month nvarchar(2)
as
begin
if object_id('tempdb..#input_deport') is not null
drop table #input_deport
if object_id('tempdb..#input_deport1') is not null
drop table #input_deport1
if object_id('tempdb..#output_deport') is not null
drop table #output_deport
if object_id('tempdb..#output_deport1') is not null
drop table #output_deport1
if object_id('tempdb..#temp1') is not null
drop table #temp1
if object_id('tempdb..#temp2') is not null
drop table #temp2
declare @tablename nvarchar(12)
set @tablename=@year+@month
--print @tablename
--本月入库明细
select
TSL.storebillNo,
TSI.itemid,
TSI.ItemName,
TSI.ItemModel,
TSI.QTY2,--常用数量
TSI.Unit1+'('+TU.Name+')' as Unit2,
TSI.Total,
ISNULL(TSI.Total_Usd,0) as Total_Usd
into #input_deport
from TU_StoreIn_Item TSI left join TU_StoreBillIn_Line TSL
on TSI.StoreBillNo=TSL.StoreBillNo
inner join TC_UNIT TU on TSI.Unit1=TU.Code
where TSL.checkflag='B08' and
((YEAR(TSL.checkTime))=@year and (right(100+MONTH(TSL.checkTime),2))=@month)
--MONTH(TSL.checkTime)=@month)
select * from #input_deport
--select * from #input_deport
--select * from #input_deport
--进库月报表itemid,品名,规格,常用数量<合计>,单价,总价
select itemid,ItemName,ItemModel,
sum(QTY2) as qty2,Unit2,
sum(Total_Usd)/sum(QTY2) as price,
sum(Total_Usd) as Total_Usd
into #input_deport1
from #input_deport
group by itemid , ItemName,ItemModel,Unit2
--select * from #input_deport1
--出库明细
select TSI.storeoutBillNo, TSI.itemid,TSI.ItemName,TSI.ItemModel,TSI.QTY2,
TSI.Unit1+'('+TU.Name+')' as Unit2,ISNULL(TSI.Total_Usd,0) as Total_Usd
into #output_deport
from TU_StoreBillOut_Item TSI
left join TU_StoreBillOut_Line TSL
on TSI.storeoutbillno=TSL.storeoutbillno
inner join TC_UNIT TU on TSI.Unit1=TU.Code
where TSL.checkflag='B08' and
(YEAR(TSL.checkTime) = @year) AND (MONTH(TSL.checkTime)=@month)
--(right((MONTH(TSL.checkTime)),2)= @month)
select * from #output_deport
--SELECT right(100+month(getdate()),2),MONTH('1998-02-02');
--case when zzc =0 then 1 else zzc end
--select * from #output_deport
--出库月报表itemid,品名,规格,常用数量<合计>,单价,总价
select itemid,ItemName,ItemModel,Unit2,
sum(QTY2) as qty2,
--sum(Total_Usd)/sum(QTY2) as price,
sum(Total_Usd)/(case when sum(QTY2)=0 then 1 else sum(QTY2) end) as price,
sum(Total_Usd) as Total_Usd
into #output_deport1
from #output_deport
group by itemid,ItemName,ItemModel,Unit2
--库存库存=入库+起初库存-出库
--select * from #output_deport1
--入库出库合并显示
select * from #output_deport1
select
--本月入库
ISNULL(in_1.itemid,Out_1.itemid) as in_itemid,
ISNULL(in_1.ItemName,Out_1.ItemName) as in_ItemName,
ISNULL(in_1.ItemModel,Out_1.ItemModel) as in_ItemModel,
ISNULL (in_1.qty2 ,0) as in_qty2,
ISNULL(in_1.Unit2,'') as in_Unit2,
ISNULL(in_1.price,0) as in_price,
ISNULL(in_1.Total_Usd,0) as in_Total_Usd,
--本月出库
ISNULL(Out_1.itemid,in_1.itemid) as out_itemid,
ISNULL(Out_1.ItemName,'') as out_ItemName,
ISNULL(Out_1.ItemModel,'') as out_ItemModel,
ISNULL (Out_1.qty2,0) as out_qty2,
ISNULL(Out_1.Unit2,'') as out_Unit2,
ISNULL(Out_1.price,0) as out_price,
ISNULL(Out_1.Total_Usd,0) as out_Total_Usd
into #temp1
from #input_deport1 in_1
FULL OUTER JOIN #output_deport1 Out_1 on in_1.itemid=Out_1.itemid
if ((@year='2008') and (@month='08'))
begin
--没有上月起初库存
delete from report_table where datetime_statistics=@tablename
-- select in_itemid,in_ItemName,in_ItemModel,in_bz,in_qty2,in_Unit2,in_price,in_QTY2_1,in_Total,
-- out_itemid,out_ItemName,out_ItemModel,out_bz,out_qty2,out_Unit2,out_price,out_QTY2_1,out_Total,
print 'OK111'
insert
into report_table(
in_itemid,in_ItemName,in_ItemModel,
in_qty2,in_Unit2,in_price,in_Total_Usd,
out_itemid,out_ItemName,out_ItemModel,
out_qty2,out_Unit2,out_price,out_Total_Usd,
storage_qty2,storage_Unit2,storage_price,storage_Total_Usd,
datetime_statistics
)
select in_itemid,in_ItemName,in_ItemModel,
in_qty2,in_Unit2,in_price,in_Total_Usd,
out_itemid,out_ItemName,out_ItemModel,
out_qty2,out_Unit2,out_price,out_Total_Usd,
-- --库存
(in_qty2-out_qty2) as storage_qty2,
case in_Unit2 when '' then out_Unit2 else in_Unit2 end as storage_Unit2,
--((in_Total_Usd-out_Total_Usd)/((in_qty2-out_qty2))) as storage_price,
((in_Total_Usd-out_Total_Usd)/(case when ((in_qty2-out_qty2))=0 then 1 else ((in_qty2-out_qty2)) end)) as storage_price,
(in_Total_Usd-in_Total_Usd) as storage_Total_Usd,
@tablename as datetime_statistics
from #temp1
--没有起初库存的月报表
select * from report_table where datetime_statistics=@tablename order by in_itemid desc
end
else
begin
--有起初库存的月报表库存数量=上月库存+本月进库-本月出库
--上月日期
declare @tablename1 nvarchar(12)
if (@month='01')
begin
--@tablename1=上年的月
set @tablename1=convert(nvarchar(4),(convert(int,@year)-1))+'12'
end
else
begin
--@tablename1=年份不变,月份-1
-- 上月月份10月月份前面补
declare @i_len int
set @i_len=LEN(convert(nvarchar(2),(convert(int,@month)-1)))
if (@i_len=1)
set @tablename1=@year+'0'+convert(nvarchar(2),(convert(int,@month)-1))
else
set @tablename1=@year+convert(nvarchar(2),(convert(int,@month)-1))
--set @tablename1=@year+convert(nvarchar(2),(convert(int,@month)-1))
end
delete from report_table where datetime_statistics=@tablename
----(case when sum(QTY2)=0 then 1 else sum(QTY2) end)
insert
into report_table
select in_itemid,in_ItemName,in_ItemModel,in_qty2,in_Unit2,in_price,in_Total_Usd,
out_itemid,out_ItemName,out_ItemModel,out_qty2,out_Unit2,out_price,out_Total_Usd,
--库存
(in_qty2-out_qty2) as storage_qty2,
case in_Unit2 when '' then out_Unit2 else in_Unit2 end as storage_Unit2,
--(case when sum(QTY2)=0 then 1 else sum(QTY2) end)
--((in_Total_Usd-out_Total_Usd)/((in_qty2-out_qty2))) as storage_price,
((in_Total_Usd-out_Total_Usd)/(case when ((in_qty2-out_qty2))=0 then 1 else ((in_qty2-out_qty2)) end)) as storage_price,
(in_Total_Usd-out_Total_Usd) as storage_Total_Usd,
@tablename as datetime_statistics
from #temp1
--查看上月库存
select ID,in_itemid,in_ItemName,in_ItemModel,
storage_qty2,storage_Unit2,storage_price,storage_Total_Usd
into #temp2
from report_table where datetime_statistics=@tablename1
--select * from #temp2
--生成本月库存
DECLARE @var_in_itemid VARCHAR(50) --宣告接受游标传回的值的变量
DECLARE @var_storage_qty2 VARCHAR(50),@storage_Unit2 VARCHAR(50)
declare @storage_Total_Usd VARCHAR(50)
declare declare_2 CURSOR
for
select in_itemid,storage_qty2,storage_Unit2,storage_Total_Usd
from #temp2
open declare_2
FETCH NEXT FROM declare_2 into @var_in_itemid,@var_storage_qty2,
@storage_Unit2,@storage_Total_Usd
WHILE @@FETCH_STATUS=0
begin
print @var_in_itemid+' '+' '+@var_storage_qty2
update report_table
set storage_qty2=@var_storage_qty2+in_qty2-out_qty2 ,
storage_Unit2=@storage_Unit2 ,
storage_Total_Usd=@storage_Total_Usd+in_Total_Usd-out_Total_Usd,
--本月库存单价
--(case when sum(QTY2)=0 then 1 else sum(QTY2) end)
--storage_price=(@storage_Total_Usd+in_Total_Usd-out_Total_Usd)/(@var_storage_qty2+in_qty2-out_qty2)
storage_price=(@storage_Total_Usd+in_Total_Usd-out_Total_Usd)/(case when (@var_storage_qty2+in_qty2-out_qty2)=0 then 1 else (@var_storage_qty2+in_qty2-out_qty2) end)
where in_itemid=@var_in_itemid
FETCH NEXT FROM declare_2
into @var_in_itemid,@var_storage_qty2,@storage_Unit2,@storage_Total_Usd
end
--释放游标
close declare_2
DEALLOCATE declare_2
select
'' as id,
ISNULL(TM2.in_itemid,RT.in_itemid) as in_itemid,
ISNULL(TM2.in_ItemName,RT.in_ItemName) as in_ItemName,
ISNULL(TM2.in_ItemModel,RT.in_ItemModel) as in_ItemModel,
ISNULL(TM2.storage_qty2,0) as last_storage_qty2,
TM2.storage_Unit2 as last_storage_Unit2,
ISNULL(TM2.storage_price,0) as last_storage_price,
ISNULL(TM2.storage_Total_Usd,0) as last_storage_Total_Usd,
ISNULL(RT.in_qty2,0) as in_qty2,
RT.in_Unit2 as in_Unit2,
ISNULL(RT.in_price,0) as in_price,
ISNULL(RT.in_Total_Usd,0) as in_Total_Usd,
ISNULL(RT.out_qty2,0) as out_qty2,
ISNULL(RT.out_Unit2,'') as out_Unit2,
ISNULL(RT.out_price,0) as out_price,
ISNULL(RT.out_Total_Usd,0) as out_Total_Usd,
--本月库存
ISNULL(RT.storage_qty2,0) as storage_qty2,
ISNULL(RT.storage_Unit2,'') as storage_Unit2,
ISNULL(RT.storage_Total_Usd,0) as storage_Total_Usd,
ISNULL(RT.storage_price,0) as storage_price,
--
TI.SN
from
#temp2 TM2 full join report_table RT on
(TM2.in_itemid=RT.in_itemid)
left join TS_Item TI
on RT.in_itemid=TI.itemid
where RT.datetime_statistics=@tablename
print @tablename1
drop table #temp2
end
drop table #temp1
drop table #output_deport1
drop table #output_deport
drop table #input_deport
drop table #input_deport1
end
 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号