Rank() over(partition ... 分组统计的实例
USE [NanFeng]
GO
/****** Object: StoredProcedure [dbo].[st_MES_RptMaterilSum] Script Date: 04/18/2016 14:21:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[st_MES_RptMaterilSum]
(
@StaCode varchar(50) = ''--工作中心
,@MachineNO varchar(50) = ''--机器编号
,@BeginTime VARCHAR(19)='' --开始时间
,@EndTime VARCHAR(19)='' --结束时间
,@in_pagesize int = 20 --每页数量
,@in_page int = 1 --页数
,@out_total_rows int = 1 output --总数量
)
/*************
exec st_MES_RptMaterilSum '','','','',20,1,1
creator: Lindh
create date: 2014.05.15
Remark: 查询投料信息报表
**************/
as
begin
if object_ID('tempdb..#MaterialSUM') is not null drop table #MaterialSUM
--查询投料信息主表
select Id=IDENTITY(int),ProductDate,MachineNO,StaCode,MaterialNO,MaterialName,BeforeBalance,NeedQty,RealDemandQty,RealUseQty,Balance,1 Flag
into #MaterialSUM
from MES_MaterialSUM ml
where (@MachineNO='' or ml.MachineNO LIKE '%' + @MachineNO+'%')
and (@StaCode = '' or ml.StaCode in(select StaCode from dbo.FN_GetStaCode(@StaCode)))
and (@BeginTime='' or ml.ProductDate>=@BeginTime)
and (@EndTime='' or ml.ProductDate<=@EndTime)
order by ml.ProductDate,MachineNo,MaterialNO
select '1.0'
select * from #MaterialSUM
select '1.01'
select id,ProductDate, MachineNO,MaterialNO,NeedQty,RealDemandQty,RealUseQty,Balance,
Rank() over(partition by MachineNO+MaterialNO order by id) Groups
from #MaterialSUM
select '2.0 小计数据'
select max(ProductDate),MachineNO,MaterialNO,sum(isnull(NeedQty,0)),sum(isnull(RealDemandQty,0)),sum(isnull(RealUseQty,0)),max(id),2
from
(select id,ProductDate, MachineNO,MaterialNO,NeedQty,RealDemandQty,RealUseQty,Balance,
Rank() over(partition by MachineNO+MaterialNO order by id) Groups from #MaterialSUM
) a
group by MachineNO,MaterialNO,id-Groups
insert into #MaterialSUM(ProductDate,MachineNO,MaterialNO,NeedQty,RealDemandQty,RealUseQty,Balance,flag)
select max(ProductDate),MachineNO,MaterialNO,sum(isnull(NeedQty,0)),sum(isnull(RealDemandQty,0)),sum(isnull(RealUseQty,0)),max(id),2
from
(select id,ProductDate, MachineNO,MaterialNO,NeedQty,RealDemandQty,RealUseQty,Balance,
Rank() over(partition by MachineNO+MaterialNO order by id) Groups from #MaterialSUM
) a
group by MachineNO,MaterialNO,id-Groups
--更新小计的结余数量为最后一条记录的结余数
update b set b.Balance=isnull(a.Balance,0)
from #MaterialSUM a
join (select Balance from #MaterialSUM where flag=2)b
on a.id=Convert(int,b.Balance)
select '3.0小计'
select * from #MaterialSUM a
insert into #MaterialSUM(ProductDate,MaterialNO,MachineNO,NeedQty,RealDemandQty,RealUseQty,Balance,Flag)
select max(ProductDate),max(MaterialNO),MachineNO,sum(isnull(NeedQty,0)),sum(isnull(RealDemandQty,0)),sum(isnull(RealUseQty,0)),max(id),3
from #MaterialSUM where flag=2
group by MachineNO
--更新合计的结余数量为最后一条记录的结余数
update b set b.Balance=isnull(a.Balance,0) from #MaterialSUM a join (select Balance from #MaterialSUM where flag=3)b on a.id=Convert(int,b.Balance)
select '4.0 合计'
select * from #MaterialSUM a
insert into #MaterialSUM(ProductDate,MaterialNO,MachineNO,NeedQty,RealDemandQty,RealUseQty,Balance,Flag)
select max(ProductDate),max(MaterialNO),max(MachineNO),sum(isnull(NeedQty,0)),sum(isnull(RealDemandQty,0)),sum(isnull(RealUseQty,0)),max(id),4
from #MaterialSUM where flag=3
--更新总计的结余数量为最后一条记录的结余数
update b set b.Balance=isnull(a.Balance,0) from #MaterialSUM a join (select Balance from #MaterialSUM where flag=4)b on a.id=Convert(int,b.Balance)
select '5.0总计'
select * from #MaterialSUM a
--/* pages */
declare @row_from int, @row_to int
select @out_total_rows = count(*) from #MaterialSUM
execute st_MES_RS_Pages @out_total_rows, @in_pagesize, @in_page, @row_from output, @row_to output
----ouput
select Id,ProductDate,MachineNO,StaCode,MaterialNO,MaterialName,BeforeBalance,NeedQty,RealDemandQty,RealUseQty,Balance,Flag into #MaterialSUM2
from #MaterialSUM
where Id between @row_from and @row_to
order by MachineNO,ProductDate,MaterialNO,flag
select '结果'
select * from #MaterialSUM2
select Id,case when Flag=2 then '小计' when Flag=3 then '合计' when Flag=4 then '总计' else ProductDate end ProductDate,
case when Flag=3 or Flag=2 or Flag=4 then '' else MachineNO end MachineNO,StaCode,
case when Flag=3 or Flag=2 or Flag=4 then '' else MaterialNO end MaterialNO,MaterialName,
BeforeBalance,NeedQty,RealDemandQty,RealUseQty,Balance
from #MaterialSUM2
select @out_total_rows
end
---------------------------------------------------------测试的数据表生成:
if exists (select * from sysobjects where id = OBJECT_ID('[MES_MaterialSUM]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [MES_MaterialSUM]
CREATE TABLE [MES_MaterialSUM] (
[ID] [int] IDENTITY (1, 1) NOT NULL,
[ProductDate] [varchar] (10) NULL,
[MachineNO] [varchar] (50) NULL,
[MaterialNO] [varchar] (50) NULL,
[BeforeBalance] [decimal] (10,2) NULL,
[NeedQty] [decimal] (10,2) NULL,
[DemandQty] [decimal] (10,2) NULL,
[RealDemandQty] [decimal] (10,2) NULL,
[RealUseQty] [decimal] (10,2) NULL,
[Balance] [decimal] (10,2) NULL,
[StaCode] [varchar] (50) NULL,
[ItemNO] [varchar] (1000) NULL,
[IsEidtRdQty] [bit] NULL DEFAULT (0),
[CreateTime] [datetime] NULL DEFAULT (getdate()),
[UpdateMan] [varchar] (20) NULL,
[UpdateTime] [datetime] NULL,
[ProductFrom] [varchar] (19) NULL,
[ProductTo] [varchar] (19) NULL,
[ItemNameList] [varchar] (8000) NULL,
[MaterialName] [varchar] (8000) NULL)
ALTER TABLE [MES_MaterialSUM] WITH NOCHECK ADD CONSTRAINT [PK_MES_MaterialSUM] PRIMARY KEY NONCLUSTERED ( [ID] )
SET IDENTITY_INSERT [MES_MaterialSUM] ON
INSERT [MES_MaterialSUM] ([ID],[ProductDate],[MachineNO],[MaterialNO],[BeforeBalance],[RealDemandQty],[RealUseQty],[Balance],[StaCode],[IsEidtRdQty],[CreateTime],[ProductFrom],[ProductTo],[MaterialName]) VALUES ( 25,N'2014-12-22',N'NF8-205#',N'110000006',0.00,300.00,1968.33,-1668.33,N'1F10-18',0,N'2014-12-22 17:34:10',N'2014-12-22 08:00:00',N'2014-12-23 07:59:59',N'JTS01004-WH-FC')
INSERT [MES_MaterialSUM] ([ID],[ProductDate],[MachineNO],[MaterialNO],[BeforeBalance],[NeedQty],[RealDemandQty],[RealUseQty],[Balance],[StaCode],[IsEidtRdQty],[CreateTime],[ProductFrom],[ProductTo],[MaterialName]) VALUES ( 26,N'2014-12-22',N'NF8-205#',N'110190004',0.00,300.00,40.00,273.16,-233.16,N'1F10-18',0,N'2014-12-22 17:34:10',N'2014-12-22 08:00:00',N'2014-12-23 07:59:59',N'JTS01004-WH-FC')
INSERT [MES_MaterialSUM] ([ID],[ProductDate],[MachineNO],[MaterialNO],[BeforeBalance],[RealUseQty],[StaCode],[IsEidtRdQty],[CreateTime],[ProductFrom],[ProductTo],[MaterialName]) VALUES ( 31,N'2014-12-23',N'NF8-205#',N'110000006',0.00,1720.88,N'1F10-18',0,N'2014-12-24 10:02:24',N'2014-12-23 08:00:00',N'2014-12-24 07:59:59',N'JTS01004-WH-FC')
INSERT [MES_MaterialSUM] ([ID],[ProductDate],[MachineNO],[MaterialNO],[BeforeBalance],[RealUseQty],[StaCode],[IsEidtRdQty],[CreateTime],[ProductFrom],[ProductTo],[MaterialName]) VALUES ( 32,N'2014-12-24',N'NF8-205#',N'110000006',0.00,1368.99,N'1F10-18',0,N'2014-12-26 10:33:27',N'2014-12-24 08:00:00',N'2014-12-25 07:59:59',N'JTS01004-WH-FC')
INSERT [MES_MaterialSUM] ([ID],[ProductDate],[MachineNO],[MaterialNO],[BeforeBalance],[RealDemandQty],[RealUseQty],[Balance],[StaCode],[IsEidtRdQty],[CreateTime],[ProductFrom],[ProductTo],[MaterialName]) VALUES ( 33,N'2014-12-24',N'TS-160',N'110010001',0.00,1300.00,327.79,972.21,N'1F01-09',0,N'2014-12-26 10:33:27',N'2014-12-24 08:00:00',N'2014-12-25 07:59:59',N'yuliao')
INSERT [MES_MaterialSUM] ([ID],[ProductDate],[MachineNO],[MaterialNO],[BeforeBalance],[RealUseQty],[StaCode],[IsEidtRdQty],[CreateTime],[ProductFrom],[ProductTo],[MaterialName]) VALUES ( 34,N'2014-12-25',N'TS-160',N'110010001',972.21,126.94,N'1F01-09',0,N'2014-12-26 10:33:33',N'2014-12-25 08:00:00',N'2014-12-26 07:59:59',N'yuliao')
SET IDENTITY_INSERT [MES_MaterialSUM] OFF
---------------------------------------------------结果显示在MES_MaterialSUM_02.jpg与MES_MaterialSUM_01.jpg:在百度云MES里面的
posted on 2016-04-18 18:25 chengjunde 阅读(545) 评论(0) 编辑 收藏 举报