22,209
社区成员
发帖
与我相关
我的任务
分享
SO_processID LOB获取时间 LOB完成时间 LOB处理人 LOB备注 LOB限制周期 LOB完成周期 LOB确认获取时间 LOB确认完成时间 LOB确认处理人 LOB确认备注 LOB确认限制周期 LOB确认完成周期
create table tb1 (sname varchar(10),classname varchar(10),oldcores int,nowcores int,note varchar(20))
go
insert into tb1 values('张三','语文',74,90,'及格')
insert into tb1 values('张三','数学',83,20,'更差')
insert into tb1 values('张三','物理',93,100,'不错')
insert into tb1 values('李四','语文',74,74,'随便')
insert into tb1 values('李四','数学',84,90,'还行')
insert into tb1 values('李四','物理',94,40,'差劲')
go
select sname,
语文上次分数=max(case when classname='语文' then oldcores end),
语文最新分数=max(case when classname='语文' then nowcores end),
语文老师评语=max(case when classname='语文' then note end),
语文限制分数=5,
语文超出分数=MAX(case when classname='语文' then nowcores-oldcores-5 end),
数学上次分数=max(case when classname='数学' then oldcores end),
数学最新分数=max(case when classname='数学' then nowcores end),
数学老师评语=max(case when classname='数学' then note end),
数学限制分数=3,
数学超出分数=MAX(case when classname='数学' then nowcores-oldcores-3 end),
物理上次分数=max(case when classname='物理' then oldcores end),
物理最新分数=max(case when classname='物理' then nowcores end),
物理老师评语=max(case when classname='物理' then note end),
物理限制分数=4,
物理超出分数=MAX(case when classname='物理' then nowcores-oldcores-4 end)
from tb1 group by sname
/*
sname 语文上次分数 语文最新分数 语文老师评语 语文限制分数 语文超出分数 数学上次分数 数学最新分数 数学老师评语 数学限制分数 数学超出分数 物理上次分数 物理最新分数 物理老师评语 物理限制分数 物理超出分数
---------- ----------- ----------- -------------------- ----------- ----------- ----------- ----------- -------------------- ----------- ----------- ----------- ----------- -------------------- ----------- -----------
李四 74 74 随便 5 -5 84 90 还行 3 3 94 40 差劲 4 -58
张三 74 90 及格 5 11 83 20 更差 3 -66 93 100 不错 4 3
警告: 聚合或其他 SET 操作消除了 Null 值。
(2 行受影响)
create table tb1 (sname varchar(10),classname varchar(10),oldcores int,nowcores int,note varchar(20))
go
insert into tb1 values('张三','语文',74,90,'及格')
insert into tb1 values('张三','数学',83,20,'更差')
insert into tb1 values('张三','物理',93,100,'不错')
insert into tb1 values('李四','语文',74,74,'随便')
insert into tb1 values('李四','数学',84,90,'还行')
insert into tb1 values('李四','物理',94,40,'差劲')
go
create table tb2 (classname varchar(10),score int)
insert into tb2
select '语文',5 union all
select '数学',6 union all
select '物理',8
go
declare @sql varchar(8000)
set @sql = 'select a.sname'
select @sql = @sql+',max(case a.classname when '''+classname+''' then a.oldcores else null end) ['+classname+'上次分数]'
+',max(case a.classname when '''+classname+''' then a.nowcores else null end) ['+classname+'最新分数]'
+',max(case a.classname when '''+classname+''' then a.note else null end) ['+classname+'老师评语]'
+',max(case a.classname when '''+classname+''' then b.score else null end) ['+classname+'限制分数]'
+',max(case a.classname when '''+classname+''' then a.nowcores-a.oldcores-b.score else null end) ['+classname+'超出分数]'
from tb1
group by classname
select @sql = @sql + ' from tb1 a join tb2 b on a.classname = b.classname group by a.sname'
exec(@sql)
drop table tb1,tb2
/************
sname 数学上次分数 数学最新分数 数学老师评语 数学限制分数 数学超出分数 物理上次分数 物理最新分数 物理老师评语 物理限制分数 物理超出分数 语文上次分数 语文最新分数 语文老师评语 语文限制分数 语文超出分数
---------- ----------- ----------- -------------------- ----------- ----------- ----------- ----------- -------------------- ----------- ----------- ----------- ----------- -------------------- ----------- -----------
李四 84 90 还行 6 0 94 40 差劲 8 -62 74 74 随便 5 -5
张三 83 20 更差 6 -69 93 100 不错 8 -1 74 90 及格 5 11
警告: 聚合或其他 SET 操作消除了空值。
(2 行受影响)
---想要的列名
学生姓名 语文上次分数 语文最新分数 语文老师评语 语文限制分数 语文超出分数 数学上次分数 数学最新分数 数学老师评语 数学限制分数 数学超出分数 物理上次分数 物理最新分数 物理老师评语 物理限制分数 物理超出分数
张三 74 90 及格 5 1
create table tb1 (sname varchar(10),classname varchar(10),oldcores int,nowcores int,note varchar(20))
go
insert into tb1 values('张三','语文',74,90,'及格')
insert into tb1 values('张三','数学',83,20,'更差')
insert into tb1 values('张三','物理',93,100,'不错')
insert into tb1 values('李四','语文',74,74,'随便')
insert into tb1 values('李四','数学',84,90,'还行')
insert into tb1 values('李四','物理',94,40,'差劲')
go
select * from tb1
--筛选信息
select
b.SO_SI_ID,a.SO_ItemNo,a.SO_ProcessID,a.SO_ProcessName,b.SO_SI_StepName,b.SO_SI_GetTime,b.SO_SI_CompTime,b.SO_SI_Notes,b.SO_SI_Optiontor into #so_stepList
from
SO_ProcessInfo a inner join SO_StepsInfo b on a.SO_ProcessID = b.SO_SI_ProcessID and a.SO_ProcessName = b.SO_SI_ProcessName
if exists (select 1
from sysobjects
where id = object_id('SO_ProcessInfo')
and type = 'U')
drop table SO_ProcessInfo
go
/*==============================================================*/
/* Table: SO_ProcessInfo */
/*==============================================================*/
create table SO_ProcessInfo (
SO_PID int identity,
SO_ProcessID int null,
SO_ProcessName nvarchar(50) null,
SO_userName nvarchar(50) null,
SO_Optiontors nvarchar(50) null,
SO_ProcessSites int null,
SO_GUID nvarchar(50) null,
SO_ItemNo nvarchar(50) null,
SO_IsCompeted nvarchar(20) null,
constraint PK_SO_PROCESSINFO primary key (SO_PID)
)
go
execute sp_addextendedproperty 'MS_Description', '流程主表', 'user', 'dbo', 'table', 'SO_ProcessInfo', NULL, NULL
go
execute sp_addextendedproperty
'MS_Description', '主编号', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_PID'
go
execute sp_addextendedproperty
'MS_Description', '流程实例号', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_ProcessID'
go
execute sp_addextendedproperty
'MS_Description', '流程名', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_ProcessName'
go
execute sp_addextendedproperty
'MS_Description', '发起人帐号', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_userName'
go
execute sp_addextendedproperty
'MS_Description', '发起人', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_Optiontors'
go
execute sp_addextendedproperty
'MS_Description', '流程状态', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_ProcessSites'
go
execute sp_addextendedproperty
'MS_Description', 'GUID', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_GUID'
go
execute sp_addextendedproperty
'MS_Description', 'ItemNo', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_ItemNo'
go
execute sp_addextendedproperty
'MS_Description', '是否已完成', 'user', 'dbo', 'table', 'SO_ProcessInfo', 'column', 'SO_IsCompeted'
go
if exists (select 1
from sysobjects
where id = object_id('SO_StepsInfo')
and type = 'U')
drop table SO_StepsInfo
go
/*==============================================================*/
/* Table: SO_StepsInfo */
/*==============================================================*/
create table SO_StepsInfo (
SO_SI_ID int identity,
SO_SI_ProcessName nvarchar(50) null,
SO_SI_ProcessID int null,
SO_SI_StepName nvarchar(50) null,
SO_SI_GetTime nvarchar(50) null,
SO_SI_CompTime nvarchar(50) null,
SO_SI_ISPlan nvarchar(50) null,
SO_SI_ISReview nvarchar(50) null,
SO_SI_Optiontor nvarchar(50) null,
SO_SI_LoginName nvarchar(50) null,
SO_SI_Notes nvarchar(1000) null,
SO_PID int null,
constraint PK_SO_STEPSINFO primary key (SO_SI_ID)
)
go
execute sp_addextendedproperty 'MS_Description', 'SO_StepsInfo', 'user', 'dbo', 'table', 'SO_StepsInfo', NULL, NULL
go
execute sp_addextendedproperty
'MS_Description', '步骤信息表主编号', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_ID'
go
execute sp_addextendedproperty
'MS_Description', '流程名', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_ProcessName'
go
execute sp_addextendedproperty
'MS_Description', '流程实例号', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_ProcessID'
go
execute sp_addextendedproperty
'MS_Description', '步骤名', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_StepName'
go
execute sp_addextendedproperty
'MS_Description', '接受时间', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_GetTime'
go
execute sp_addextendedproperty
'MS_Description', '更新时间', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_CompTime'
go
execute sp_addextendedproperty
'MS_Description', '是否需要策划', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_ISPlan'
go
execute sp_addextendedproperty
'MS_Description', '是否需要实物评审', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_ISReview'
go
execute sp_addextendedproperty
'MS_Description', '操作人', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_Optiontor'
go
execute sp_addextendedproperty
'MS_Description', '登录名', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_LoginName'
go
execute sp_addextendedproperty
'MS_Description', '备注', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_SI_Notes'
go
execute sp_addextendedproperty
'MS_Description', '流程主表ID', 'user', 'dbo', 'table', 'SO_StepsInfo', 'column', 'SO_PID'
go
--筛选信息
select
b.SO_SI_ID,a.SO_ItemNo,a.SO_ProcessID,a.SO_ProcessName,b.SO_SI_StepName,b.SO_SI_GetTime,b.SO_SI_CompTime,b.SO_SI_Notes,b.SO_SI_Optiontor into #so_stepList
from
SO_ProcessInfo a inner join SO_StepsInfo b on a.SO_ProcessID = b.SO_SI_ProcessID and a.SO_ProcessName = b.SO_SI_ProcessName
-----查询步骤信息
select SO_SI_StepName INTO #STEPNAME from #so_stepList
declare @sql varchar(max)
set @sql = 'select SO_ProcessID,SO_ItemNo'
---获取时间
select @sql = @sql+',max(case SO_SI_StepName when '''+ SO_SI_StepName +''' then SO_SI_GetTime else '''' end) ['+SO_SI_StepName+'获取时间]'
from #STEPNAME
---完成时间
select @sql = @sql+',max(case SO_SI_StepName when '''+ SO_SI_StepName +''' then SO_SI_CompTime else '''' end) ['+SO_SI_StepName+'完成时间]'
from #STEPNAME
--处理人
select @sql = @sql+',max(case SO_SI_StepName when '''+ SO_SI_StepName +''' then SO_SI_Optiontor else '''' end) ['+SO_SI_StepName+'处理人]'
from #STEPNAME
--备注
select @sql = @sql+',max(case SO_SI_StepName when '''+ SO_SI_StepName +''' then SO_SI_Notes else '''' end) ['+SO_SI_StepName+'备注]'
from #STEPNAME
--延迟时间
select @sql = @sql+',max(case SO_SI_StepName when '''+ SO_SI_StepName +''' then cast(datediff(hour,SO_SI_GetTime,SO_SI_CompTime) as nvarchar(50)) else ''0'' end) ['+SO_SI_StepName+'延迟时间]'
from #STEPNAME
set @sql = @sql+' from #so_stepList group by SO_ProcessID,SO_ItemNo'
exec(@sql)
DROP TABLE #STEPNAME
DROP TABLE #so_stepList