--  billHead 表单的头部的部分效果--

   [billId] [int] IDENTITY(1,1) NOT NULL,

[billCode] [char](10) NOT NULL,

[billDate] [date] NOT NULL,

[billType] [int] NOT NULL,

[custId] [int] NOT NULL,

[opr] [int] NOT NULL,

[loginId] [int] NOT NULL,

[opDate] [smalldatetime] NOT NULL,

[remark] [varchar](500) NULL,



--表单的billItems 项目 --

这个是典型的1 对多的模式,为啥子我们做的那么多的丑

[billId] [int] NOT NULL,

[sNo] [int] NOT NULL,

[proId] [int] NOT NULL,

[Num] [float] NOT NULL,

[price] [money] NOT NULL,

[amt] [money] NOT NULL,

[remark] [varchar](200) NULL,

--在题二的基础上设计一个仅查看当月销售(包括销售出库与销售退货)单据的视图(列名包括:单据Id、单据编码、单据类别、单据日期、往来单位、备注)。--

--

create view  Loook

as



select billHead.billId,billHead.billCode,billHead.billType,billHead.billDate,

billHead.custId,billHead.remark from billHead

where billType & 3>0  and billhead.billDate>(DATEADD(mm, DATEDIFF(mm,0,getdate()), 0) ) and billHead.billDate<GETDATE();



--2015-12-01 20:46:30.140--

select   dateadd(dd,-day(getdate())+1,getdate()) 



--2015-12-01 00:00:00.000--

SELECT DATEADD(mm, DATEDIFF(mm,0,getdate()), 0)    





--在题二的基础上设计存储过程,要求参数为商品 @proId,开始日期 @startDate,结束日期 @endDate,返回从开始日期到结束日期该商品的进货数量与出库数量--



--  1 2 4 8 分别表示销售  退货  进库  出库     4 &12 >0  8 &12 >0   1 &12=0

create procedure looking

@startDate date,

@endDate date,

@proId int,

@proin int output,

@proout int output

as


 begin

select @proin=sum(   case billHead.billType 

                         when 4 Then billItems.Num

                         else -billItems.Num

     end)

    from billItems 

inner join billHead

on  billHead.billId=billItems.billId



where billHead.billType & 12 >0 and billHead.billDate>@startDate and billHead.billDate<@endDate

 end





 ---在题二的基础上设计存储过程,参数为开始日期 @startDate,结束日期 @endDate,返回从开始日期到结束日期所以单据中的商品的进出货统计列表(列名包括: 商品Id、合计数量变化)。--





 ---总结 我们能在使用这个的时候啊,有的时候想的不清楚  ,比如商品可能来至于不同的表单这时候应该想到我们的使用函数 group  by

 create procedure loo

     @startDate date,

@endDate date

as

begin

select billItems.proId,sum(case billHead.billType 

when 1 then -billItems.Num

when 2 then  billItems.Num

when 4 then -billItems.Num

when 8 then billItems.Num

else 0

                            end) as 合计数量的变化表

from billItems inner join  billHead

  on billHead.billId=billItems.billId

   and billHead.billDate>@startDate and billHead.billDate<@endDate

group by proId


end