【紧急求助】求一存储过程!!!!!!!!!!!!!

时间:2022-02-03 11:56:58
RT。oracle下用的。
PS:从来木进过oracle版块。所以发在介。相信大家都会弄哇。
先说下表情况
[code=SQL]

1:Test_Sample检测样品表
test_sample_id  sample_name  pointer_result  pointer_result2 producenumber   sampling_date
 1                 SG3        优等品                           100            2012-05-02 13:00:00
 2                 SG3        合格品          医用级            80             2012-05-03 13:00:00
 3                 SG3        一等品          食品级            80             2012-05-03 13:00:00
 4                 氢气       一等品                            50             2012-05-04 13:00:00
 5                 SG3        一等品          食品级            80             2012-06-03 13:00:00
 6                 次氯酸钠                                     40             2012-05-02 13:00:00

................更多数据

2:Test_Analyse检测分析表
test_analyse_id  analyse_name     test_sample_id  analyse_method_name 
1                   水分析            1                 gb/12
2                   油分析            1                 gb/34
3                   水分析            2                 gb/12
4                   油分析            2                 gb/34
5                   水分析            3                 gb/12
6                   油分析            3                 gb/34
7                   水分析            5                 gb/12
8                   油分析            5                 gb/34
9                   固体分析          4                 gb/890
10                  次氯酸钠的测定    6                gb/4389398
.........................更多数据

75 个解决方案

#1


挖靠,内容很多,放不下。

#2


1:Test_Sample检测样品表
test_sample_id  sample_name  pointer_result  pointer_result2 producenumber   sampling_date
 1                 SG3        优等品                           100            2012-05-02 13:00:00
 2                 SG3        合格品          医用级            80             2012-05-03 13:00:00
 3                 SG3        一等品          食品级            80             2012-05-03 13:00:00
 4                 氢气       一等品                            50             2012-05-04 13:00:00
 5                 SG3        一等品          食品级            80             2012-06-03 13:00:00
 6                 次氯酸钠                                     40             2012-05-02 13:00:00

................更多数据

#3



2:Test_Analyse检测分析表
test_analyse_id  analyse_name     test_sample_id  analyse_method_name 
1                   水分析            1                 gb/12
2                   油分析            1                 gb/34
3                   水分析            2                 gb/12
4                   油分析            2                 gb/34
5                   水分析            3                 gb/12
6                   油分析            3                 gb/34
7                   水分析            5                 gb/12
8                   油分析            5                 gb/34
9                   固体分析          4                 gb/890
10                  次氯酸钠的测定    6                gb/4389398
.........................更多数据

3:Test_Item检测项目表
test_item_id test_analyse_id  test_item_name analyse_value pointer_condition_text pointer_result       pointer_condition_text2               pointer_result2  data_unit_name 
1             1                 黏数            140          143-136                  优等品                                                                      ml/g
2             1                 表观密度        0.5          >=0.40                   优等品                                                                      g/ml
3             1                 吸油量          26           >=23                     优等品                                                                      g
4             1                 挥发物          0.5          <=0.50                   优等品                                                                      %
5             1                 250um筛孔筛余物 68           >=23                     优等品                                                                      %
6             2                 60um筛孔筛余物  1.2          <=30                     优等品                                                                      %
7             2                 杂质粒子数      1.2          <=30                     优等品                                                                     个
8             2                 白度            1.2          <=30                     优等品                                                                      %
9             2                 鱼眼            1.2          <=30                     优等品                                                                     个/400cm
10             2                 电导率          1.2          <=30                    优等品                                                                      uS/(cm.g
11            2                 VCM残留量       1.2          <=30                     优等品                                                                      ug/g
12             2                 1,1-二氯乙烷    0.5                                                                                                                mg/Kg 


13             3                 黏数            136          143-136                  合格品                                                                    ml/g
14             3                 表观密度        0.5          >=0.40                   优等品                                                                    g/ml
15             3                 吸油量          70           >=23                     一等品                                                                    g
16             3                 挥发物          0.5          <=0.50                   合格品                                                                    %
17             3                 250um筛孔筛余物 78           >=23                     优等品                                                                    %
18             4                 60um筛孔筛余物  1.2          <=30                     一等品                                                                    %
19             4                 杂质粒子数      1.2          <=30                     一等品                                                                   个
20             4                 白度            1.2          <=30                     一等品                                                                    %
21             4                 鱼眼            1.2          <=30                     一等品                                                                    个/400cm
22             4                 电导率          1.2          <=30                     一等品                                                                    uS/(cm.g
23             4                 VCM残留量       1.2          <=30                     一等品           VCM残留量<=5,1,1-二氯乙烷<=150          医用级           ug/g
24             4                 1,1-二氯乙烷    0.5                                                                                                             mg/Kg 


25             5                 黏数            136          143-136                  合格品                                                                    ml/g
26             5                 表观密度        0.4          >=0.40                   合格品                                                                    g/ml
27             5                 吸油量          23           >=23                     合格品                                                                   g
28             5                 挥发物          0.5          <=0.50                   合格品                                                                    %
29             5                 250um筛孔筛余物 1.2          >=23                     合格品                                                                     %
30             6                 60um筛孔筛余物  1.2          <=30                     一等品                                                                     %
31             6                 杂质粒子数      1.2          <=30                     一等品                                                                    个
32             6                 白度            1.2          <=30                     一等品                                                                     %
33             6                 鱼眼            1.2          <=30                     一等品                                                                 个/400cm
34             6                 电导率          1.2          <=30                     一等品                                                                 uS/(cm.g
35             6                 VCM残留量       1.2          <=30                     一等品          VCM残留量<=1,1,1-二氯乙烷<=150           食品级         ug/g
36             6                 1,1-二氯乙烷    0.5                                                                                                           mg/Kg 


37             7                 黏数            136          143-136                  合格品                                                                    ml/g
38             7                 表观密度        0.4          >=0.40                   合格品                                                                    g/ml
39             7                 吸油量          23           >=23                     合格品                                                                   g
40             7                 挥发物          0.5          <=0.50                   合格品                                                                    %
41             7                 250um筛孔筛余物 1.2          >=23                     合格品                                                                     %
42             8                 60um筛孔筛余物  1.2          <=30                     一等品                                                                     %
43             8                 杂质粒子数      1.2          <=30                     一等品                                                                    个
44             8                 白度            1.2          <=30                     一等品                                                                     %
45             8                 鱼眼            1.2          <=30                     一等品                                                                 个/400cm
46             8                 电导率          1.2          <=30                     一等品                                                                 uS/(cm.g
47             8                 VCM残留量       1.2          <=30                     一等品          VCM残留量<=1,1,1-二氯乙烷<=150           食品级         ug/g
48             8                 1,1-二氯乙烷    0.5                                                                                                           mg/Kg 

49             9                 含量            789          >=99                    一等品                                                                       %


50             10                 外观           0.4          >=0.40                   合格品                                                                    
51             10                 次氯酸钠含量   23           >=23                     合格品                                                                      %
52             10                 游离碱含量     0.5          <=0.50                   合格品                                                                      %
53             10                 铁             1.2         >=23                      合格品                                                                      %
..................................更多数据

#4



想要的查询结果为(括号里代表字段名称),没有名称代表的是需要通过其他字段计算出来。
分析标准(analyse_method_name)        样品名称                   指标名称(test_item_name)    单位(data_unit_name)     指标值        本月最高    本月最低    本月平均   累计平均
    gb/12                            SG3优等品                  黏数                       ml/g                    143-136          56            55         455       45                                                                     
     ,,                                  ,,                   表观密度                    g/ml                     >=0.40           56            55         455       45                                                                            
     ,,                                  ,,                   吸油量                        g                      >=23            56            55         455       45                                                                           
     ,,                                  ,,                    挥发物                       %                      <=0.50          56            55         455       45                                                                            
     ,,                                  ,,                 250um筛孔筛余物                 %                     >=23             56            55         455       45                                                                            
    gb/34                                ,,                 60um筛孔筛余物                  %                      <=30           56            55         455       45                                                                              
     ,,                                  ,,                  杂质粒子数                     个                     <=30            56            55         455       45                                                                           
     ,,                                  ,,                    白度                         %                     <=30            56            55         455       45                                                                             
     ,,                                  ,,                   鱼眼                          个/400cm              <=30             56            55         455       45                                                                     
     ,,                                  ,,                 电导率                         uS/(cm.g                <=30           56            55         455       45                                                                           
     ,,                                  ,,                  VCM残留量                     ug/g                    <=30           56            55         455       45    
 
  gb/12                          SG3合格品                  黏数                       ml/g                    143-136          56            55         455       45                                                                     
     ,,                                  ,,                   表观密度                    g/ml                     >=0.40           56            55         455       45                                                                            
     ,,                                  ,,                   吸油量                        g                      >=23            56            55         455       45                                                                           
     ,,                                  ,,                    挥发物                       %                      <=0.50          56            55         455       45                                                                            
     ,,                                  ,,                 250um筛孔筛余物                 %                     >=23             56            55         455       45                                                                            
    gb/34                                ,,                 60um筛孔筛余物                  %                      <=30           56            55         455       45                                                                              
     ,,                                  ,,                  杂质粒子数                     个                     <=30            56            55         455       45                                                                           
     ,,                                  ,,                    白度                         %                     <=30            56            55         455       45                                                                             
     ,,                                  ,,                   鱼眼                          个/400cm              <=30             56            55         455       45                                                                     
     ,,                                  ,,                 电导率                         uS/(cm.g                <=30           56            55         455       45                                                                           
     ,,                                  ,,                  VCM残留量                     ug/g                    <=30           56            55         455       45      
 
  gb/12                          SG3一等品                  黏数                       ml/g                    143-136          56            55         455       45                                                                     
     ,,                                  ,,                   表观密度                    g/ml                     >=0.40           56            55         455       45                                                                            
     ,,                                  ,,                   吸油量                        g                      >=23            56            55         455       45                                                                           
     ,,                                  ,,                    挥发物                       %                      <=0.50          56            55         455       45                                                                            
     ,,                                  ,,                 250um筛孔筛余物                 %                     >=23             56            55         455       45                                                                            
    gb/34                                ,,                 60um筛孔筛余物                  %                      <=30           56            55         455       45                                                                              
     ,,                                  ,,                  杂质粒子数                     个                     <=30            56            55         455       45                                                                           
     ,,                                  ,,                    白度                         %                     <=30            56            55         455       45                                                                             
     ,,                                  ,,                   鱼眼                          个/400cm              <=30             56            55         455       45                                                                     
     ,,                                  ,,                 电导率                         uS/(cm.g                <=30           56            55         455       45                                                                           
     ,,                                  ,,                  VCM残留量                     ug/g                    <=30           56            55         455       45                                       

 gb/34                          SG3食品级                  VCM残留量                   ml/g                    <=1            56            55         455       45                                                                     
     ,,                                  ,,                   1,1-二氯乙烷                 g/ml                    <=150          56            55         455       45                                                                                                                                                                

 gb/890                          氢气一等品                含量                         ml/g                    143-136          56            55        455       45 

 gb/890                          次氯酸钠                外观                                                   143-136          56            55        455       45 
  ,,                              ,,                  次氯酸钠含量                    ml/g                      143-136          56            55        455       45 
  ,,                              ,,                  游离碱含量                      ml/g                     143-136         56            55        455       45 
  ,,                              ,,                     铁                           ml/g                     143-136          56            55        455       45 
  .................................更多数据

#5



  说明:
    以上数据都是我乱造的,但是仔细看还是能看懂,看出端倪的。
1、检测样品表的test_sample_id是检测分析表的外键,检测分析表的test_analyse_id是检测项目表的外键
2。样品名称通过test_sample表里的test_sample_nam+pointer_result或者为test_sample_name+pointer_result2组合而成。
3.分析标准、样品名称下行记录如果与上行记录相同,则用两个逗号",,"表示。
4.本月最高取test_item表里analyse_value最大值。最低取analyse_value最小值。
5.本月平均计算公式为:
第一个结果analyse_value*producenumber(test_sample表里)+第二个结果analyse_value*producenumber+第N个结果analyse_value*producenumber/第一个producenumber+第二个producenumber+第n个producenumber
6.累计平均计算公式为:
根据输入参数samplingStartTime和sapmlingEndTime  test_sample表的  samplingStartTime<=sampling_date<=samplingEndtime过滤数据得到当前月份的前面几个月的累计。
    如:输入参数samplingStartTime值为:2012-05-01 00:00:00,samplingEndTime值为:2012-05-31 23:59:59
累计平均 1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber         
7。以上结果集是先按照test_sample的pointer_result分组出,然后再按pointer_result2分组出。
按照pointer_result2分组出的数据有点小规则,如:SG3食品级,这个它只出了两个项目,VCM残留量和1,1-二氯已烷,按照如下规则
VCM残留量的pointer_condition2为 VCM残留量<=1,1,1-二氯乙烷<=150
所以指标值这一列,VCM残留量为:<=1,1,1二氯已烷为:<=150,是根据pointer_condition2以逗号为分隔取出VCM残留量<=1和1-二氯乙烷<=150,所以把<=1赋值到VCM残留量,把<=150赋值到1-二氯乙烷。
如果是其他项目的pointer_condition2也是按照这种逻辑取取项目名称和指标值的。


    麻烦大家弄个oracle下的存储过程,输入参数就是samplingStartTime和samplingEndTime.

#6


oracle不熟,oracle板块还是有好几个大牛,建议LZ把帖子移过去!

#7


写法应该差不多的吧,就是可能一些小细节要注意下。。

#8


sql版比较旺点,所以发介了。
8过最近人好像也变挺少了

#9


擦,这么多数据,看得头晕 【紧急求助】求一存储过程!!!!!!!!!!!!!

#10


引用 9 楼  的回复:
擦,这么多数据,看得头晕
木办法啊,
这些逻辑还是想了2、3天才理通。。
写这个帖子也不容易啊。
麻烦大家看看了。

#11


Oracle的我也无能为力了,帮顶一下!! 【紧急求助】求一存储过程!!!!!!!!!!!!!

#12



1、检测样品表的test_sample_id是检测分析表的外键,检测分析表的test_analyse_id是检测项目表的外键.
--这就是3表连接查询即可。

2、样品名称通过test_sample表里的test_sample_name+pointer_result或test_sample_name +pointer_result2组合而成。
--什么时候用test_sample_name + pointer_result  ?
--什么时候用test_sample_name + pointer_result2 ?

3.分析标准、样品名称下行记录如果与上行记录相同,则用两个逗号",,"表示。
--oracle中的序号和sql server 中的序号不一样。

4.本月最高取test_item表里analyse_value最大值。最低取analyse_value最小值。
--这个我估计是个嵌套子查询。

5.本月平均计算公式为:
第一个结果analyse_value*producenumber(test_sample表里)+
第二个结果analyse_value*producenumber+
第N个结果analyse_value*producenumber/第一个producenumber+第二个producenumber+第n个producenumber

--这个是不是少了括号呀?

6.累计平均计算公式为:
根据输入参数samplingStartTime和sapmlingEndTime  
test_sample表的samplingStartTime<=sampling_date<=samplingEndtime
过滤数据得到当前月份的前面几个月的累计。

如:输入参数
samplingStartTime值为:2012-05-01 00:00:00,
samplingEndTime值为:2012-05-31 23:59:59

累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber  

--这个是不是也少了括号?
              
7。以上结果集是先按照test_sample的pointer_result分组出,然后再按pointer_result2分组出。

--最后把两个结果 union all在一起?

按照pointer_result2分组出的数据有点小规则,如:SG3食品级,这个它只出了两个项目,VCM残留量和1,1-二氯已烷,按照如下规则 
VCM残留量的pointer_condition2为 VCM 残留量<=1,1,1-二氯乙烷<=150
所以指标值这一列,VCM残留量为:<=1,1,1二氯已烷为:<=150,
是根据pointer_condition2以逗号为分隔取出VCM残留量<=1和1-二氯乙烷<=150,
所以把<=1赋值到VCM残留量,把<=150赋值到1-二氯乙烷。

--这句我看了半天,貌似是没看懂...

如果是其他项目的pointer_condition2也是按照这种逻辑取取项目名称和指标值的。



懂Oracle的兄弟帮忙解决一下吧

#13


我这以上7条是为最后想要出的那个结果描述一下是怎么出那个数据的。
并不是按照1、2、3、4、56、7。根据这7个条件出数据。

#14


什么时候用test_sample_name + pointer_result  ?
--什么时候用test_sample_name + pointer_result2 ?
这个就是先按test_sample表里的pointer_result分组,
这样的话就是test_sample_name + pointer_result.
然后再按test_sample表里的pointer_result2分组。
这样的话就是test_sample_name+pointer_result2

#15


回复提示 此论坛木有这个帖子?  test下 【紧急求助】求一存储过程!!!!!!!!!!!!!

#16


引用 15 楼  的回复:
回复提示 此论坛木有这个帖子?  test下
现在应该可以鸟,,,
刚刚移动到水区加了点分上去。。

#17


看这仗势  貌似建表也要花个几十分钟咯  【紧急求助】求一存储过程!!!!!!!!!!!!!

#18


还是期待明天来更多人吧,
呵呵。
好困,介些天都超困。
不等了。希望明天有好结果。!

#19


引用 17 楼  的回复:
看这仗势  貌似建表也要花个几十分钟咯

数据里面有逗号,个别字段没有数据,数据脚本我折腾了好半天....


declare @Test_Sample table 
(
test_sample_id int,
sample_name varchar(8),
pointer_result varchar(6),
pointer_result2 varchar(6),
producenumber int,
sampling_date datetime
)
insert into @Test_Sample
select 1,'SG3','优等品',null,100,'2012-05-02 13:00:00' union all
select 2,'SG3','合格品','医用级',80,'2012-05-03 13:00:00' union all
select 3,'SG3','一等品','食品级',80,'2012-05-03 13:00:00' union all
select 4,'氢气','一等品',null,50,'2012-05-04 13:00:00' union all
select 5,'SG3','一等品','食品级',80,'2012-06-03 13:00:00' union all
select 6,'次氯酸钠',null,null,40,'2012-05-02 13:00:00'

declare @Test_Analyse table 
(
test_analyse_id int,
analyse_name varchar(14),
test_sample_id int,
analyse_method_name varchar(10)
)
insert into @Test_Analyse
select 1,'水分析',1,'gb/12' union all
select 2,'油分析',1,'gb/34' union all
select 3,'水分析',2,'gb/12' union all
select 4,'油分析',2,'gb/34' union all
select 5,'水分析',3,'gb/12' union all
select 6,'油分析',3,'gb/34' union all
select 7,'水分析',5,'gb/12' union all
select 8,'油分析',5,'gb/34' union all
select 9,'固体分析',4,'gb/890' union all
select 10,'次氯酸钠的测定',6,'gb/4389398'

declare @Test_Item table 
(
test_item_id int,
test_analyse_id int,
test_item_name varchar(15),
analyse_value varchar(10),
pointer_condition_text varchar(7),
pointer_result varchar(6),
pointer_condition_text2 varchar(32),
pointer_result2 varchar(10),
data_unit_name varchar(15)
)
insert into @Test_Item
select 1,1,'黏数','140','143-136','优等品',null,null,'ml/g' union all
select 2,1,'表观密度','0.5','>=0.40','优等品',null,null,'g/ml' union all
select 3,1,'吸油量','26','>=23','优等品',null,null,'g' union all
select 4,1,'挥发物','0.5','<=0.50','优等品',null,null,'%' union all
select 5,1,'250um筛孔筛余物','68','>=23','优等品',null,null,'%' union all
select 6,2,'60um筛孔筛余物','1.2','<=30','优等品',null,null,'%' union all
select 7,2,'杂质粒子数','1.2','<=30','优等品',null,null,'个' union all
select 8,2,'白度','1.2','<=30','优等品',null,null,'%' union all
select 9,2,'鱼眼','1.2','<=30','优等品',null,null,'个/400cm' union all
select 10,2,'电导率','1.2','<=30','优等品',null,null,'uS/(cm.g' union all
select 11,2,'VCM残留量','1.2','<=30','优等品',null,null,'ug/g' union all
select 12,2,'1','1-二氯乙烷','0.5','mg/Kg',null,null,null union all
select 13,3,'黏数','136','143-136','合格品',null,null,'ml/g' union all
select 14,3,'表观密度','0.5','>=0.40','优等品',null,null,'g/ml' union all
select 15,3,'吸油量','70','>=23','一等品',null,null,'g' union all
select 16,3,'挥发物','0.5','<=0.50','合格品',null,null,'%' union all
select 17,3,'250um筛孔筛余物','78','>=23','优等品',null,null,'%' union all
select 18,4,'60um筛孔筛余物','1.2','<=30','一等品',null,null,'%' union all
select 19,4,'杂质粒子数','1.2','<=30','一等品',null,null,'个' union all
select 20,4,'白度','1.2','<=30','一等品',null,null,'%' union all
select 21,4,'鱼眼','1.2','<=30','一等品',null,null,'个/400cm' union all
select 22,4,'电导率','1.2','<=30','一等品',null,null,'uS/(cm.g' union all
select 23,4,'VCM残留量','1.2','<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','医用级','ug/g' union all
select 24,4,'1','1-二氯乙烷','0.5',null,null,null,'mg/Kg' union all
select 25,5,'黏数','136','143-136','合格品',null,null,'ml/g' union all
select 26,5,'表观密度','0.4','>=0.40','合格品',null,null,'g/ml' union all
select 27,5,'吸油量','23','>=23','合格品',null,null,'g' union all
select 28,5,'挥发物','0.5','<=0.50','合格品',null,null,'%' union all
select 29,5,'250um筛孔筛余物','1.2','>=23','合格品',null,null,'%' union all
select 30,6,'60um筛孔筛余物','1.2','<=30','一等品',null,null,'%' union all
select 31,6,'杂质粒子数','1.2','<=30','一等品',null,null,'个' union all
select 32,6,'白度','1.2','<=30','一等品',null,null,'%' union all
select 33,6,'鱼眼','1.2','<=30','一等品',null,null,'个/400cm' union all
select 34,6,'电导率','1.2','<=30','一等品',null,null,'uS/(cm.g' union all
select 35,6,'VCM残留量','1.2','<=30','一等品','VCM残留量<=1,1,1-二氯乙烷<=150','食品级','ug/g' union all
select 36,6,'1','1-二氯乙烷','0.5',null,null,null,'mg/Kg' union all
select 37,7,'黏数','136','143-136','合格品',null,null,'ml/g' union all
select 38,7,'表观密度','0.4','>=0.40','合格品',null,null,'g/ml' union all
select 39,7,'吸油量','23','>=23','合格品',null,null,'g' union all
select 40,7,'挥发物','0.5','<=0.50','合格品',null,null,'%' union all
select 41,7,'250um筛孔筛余物','1.2','>=23','合格品',null,null,'%' union all
select 42,8,'60um筛孔筛余物','1.2','<=30','一等品',null,null,'%' union all
select 43,8,'杂质粒子数','1.2','<=30','一等品',null,null,'个' union all
select 44,8,'白度','1.2','<=30','一等品',null,null,'%' union all
select 45,8,'鱼眼','1.2','<=30','一等品',null,null,'个/400cm' union all
select 46,8,'电导率','1.2','<=30','一等品',null,null,'uS/(cm.g' union all
select 47,8,'VCM残留量','1.2','<=30','一等品','VCM残留量<=1,1,1-二氯乙烷<=150','食品级','ug/g' union all
select 48,8,'1','1-二氯乙烷','0.5',null,null,null,'mg/Kg' union all
select 49,9,'含量','789','>=99','一等品',null,null,'%' union all
select 50,10,'外观','0.4','>=0.40','合格品',null,null,null union all
select 51,10,'次氯酸钠含量','23','>=23','合格品',null,null,'%' union all
select 52,10,'游离碱含量','0.5','<=0.50','合格品',null,null,'%' union all
select 53,10,'铁','1.2','>=23','合格品',null,null,null

select * from @Test_Sample
select * from @Test_Analyse
select * from @Test_Item

#20


用sql server 写了个大概...

;with maco as
(
select 
b.analyse_method_name as 分析标准,
        c.pointer_condition_text,
c.pointer_result as 样品名称,
        c.test_item_name as 指标名称,
        c.data_unit_name as 单位,
        c.analyse_value as 指标值,
        a.sampling_date as 时间
from @Test_Item c 
left join @Test_Analyse b on c.test_analyse_id=b.test_analyse_id
left join @Test_Sample a on b.test_sample_id=a.test_sample_id
),
m1 as 
(
select *,row_number() over(order by (select 1)) as id from maco
)

select 
case when a.分析标准=b.分析标准 then ',,' else a.分析标准 end as 分析标准,
    case when a.样品名称=b.样品名称 then ',,' else a.样品名称 end as 样品名称,
a.指标名称,a.单位,
a.指标值,a.时间 ,
    (select max(指标值) from m1 
    where pointer_condition_text=a.pointer_condition_text and
    样品名称=a.样品名称
    ) as 最大值,
    (select min(指标值) from m1 
    where pointer_condition_text=a.pointer_condition_text and
    样品名称=a.样品名称
    ) as 最小值
from m1 a 
left join m1 b on a.id=b.id+1
/*
分析标准       样品名称   指标名称            单位              指标值        时间                      最大值        最小值
---------- ------ --------------- --------------- ---------- ----------------------- ---------- ----------
gb/12      优等品    黏数              ml/g            140        2012-05-02 13:00:00.000 140        140
,,         ,,     表观密度            g/ml            0.5        2012-05-02 13:00:00.000 0.5        0.5
,,         ,,     吸油量             g               26         2012-05-02 13:00:00.000 78         26
,,         ,,     挥发物             %               0.5        2012-05-02 13:00:00.000 0.5        0.5
,,         ,,     250um筛孔筛余物      %               68         2012-05-02 13:00:00.000 78         26
gb/34      ,,     60um筛孔筛余物       %               1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-05-02 13:00:00.000 NULL       NULL
gb/12      合格品    黏数              ml/g            136        2012-05-03 13:00:00.000 136        136
,,         优等品    表观密度            g/ml            0.5        2012-05-03 13:00:00.000 0.5        0.5
,,         一等品    吸油量             g               70         2012-05-03 13:00:00.000 70         70
,,         合格品    挥发物             %               0.5        2012-05-03 13:00:00.000 0.5        0.5
,,         优等品    250um筛孔筛余物      %               78         2012-05-03 13:00:00.000 78         26
gb/34      一等品    60um筛孔筛余物       %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-05-03 13:00:00.000 NULL       NULL
gb/12      合格品    黏数              ml/g            136        2012-05-03 13:00:00.000 136        136
,,         ,,     表观密度            g/ml            0.4        2012-05-03 13:00:00.000 0.4        0.4
,,         ,,     吸油量             g               23         2012-05-03 13:00:00.000 23         1.2
,,         ,,     挥发物             %               0.5        2012-05-03 13:00:00.000 0.5        0.5
,,         ,,     250um筛孔筛余物      %               1.2        2012-05-03 13:00:00.000 23         1.2
gb/34      一等品    60um筛孔筛余物       %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-05-03 13:00:00.000 NULL       NULL
gb/12      合格品    黏数              ml/g            136        2012-06-03 13:00:00.000 136        136
,,         ,,     表观密度            g/ml            0.4        2012-06-03 13:00:00.000 0.4        0.4
,,         ,,     吸油量             g               23         2012-06-03 13:00:00.000 23         1.2
,,         ,,     挥发物             %               0.5        2012-06-03 13:00:00.000 0.5        0.5
,,         ,,     250um筛孔筛余物      %               1.2        2012-06-03 13:00:00.000 23         1.2
gb/34      一等品    60um筛孔筛余物       %               1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-06-03 13:00:00.000 NULL       NULL
gb/890     一等品    含量              %               789        2012-05-04 13:00:00.000 789        789
gb/4389398 合格品    外观              NULL            0.4        2012-05-02 13:00:00.000 0.4        0.4
,,         ,,     次氯酸钠含量          %               23         2012-05-02 13:00:00.000 23         1.2
,,         ,,     游离碱含量           %               0.5        2012-05-02 13:00:00.000 0.5        0.5
,,         ,,     铁               NULL            1.2        2012-05-02 13:00:00.000 23         1.2

(53 row(s) affected)

*/

#21


拼命了  表现建好了  给想做的人省些时间 数据应该对应了  花了30多分钟 不行了 夏眠 【紧急求助】求一存储过程!!!!!!!!!!!!!



--1、检测样品表
create table Test_Sample (test_sample_id number(5),sample_name varchar2(20),pointer_result varchar2(10),
                          pointer_result2 varchar2(10),producenumber number(3),sampling_date date);
insert into Test_Sample values (1,'SG3','优等品',null,100,to_date('2012-05-02 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (2,'SG3','合格品','医用级',80,to_date('2012-05-03 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (3,'SG3','一等品','食品级',80,to_date('2012-05-04 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (4,'氢气','一等品',null,50,to_date('2012-05-05 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (5,'SG3','一等品','食品级',80,to_date('2012-06-03 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (6,'次氯酸钠',null,null,40,to_date('2012-05-02 13:00:00','yyyy-mm-dd hh24:mi:ss'));

--2、检测分析表
create table Test_Analyse (test_analyse_id number(5),analyse_name varchar2(20),test_sample_id number(5),analyse_method_name varchar2(20));
insert into Test_Analyse values (1,'水分析',1,'gb/12');
insert into Test_Analyse values (2,'油分析',1,'gb/34');
insert into Test_Analyse values (3,'水分析',2,'gb/12');
insert into Test_Analyse values (4,'油分析',2,'gb/34');
insert into Test_Analyse values (5,'水分析',3,'gb/12');
insert into Test_Analyse values (6,'油分析',3,'gb/34');
insert into Test_Analyse values (7,'水分析',5,'gb/12');
insert into Test_Analyse values (8,'油分析',5,'gb/34');
insert into Test_Analyse values (9,'固体分析',4,'gb/890');
insert into Test_Analyse values (10,'次氯酸钠的测定',6,'gb/4389398');

--3、检测项目表
create table Test_Item (test_item_id number(5),test_analyse_id number(5),test_item_name varchar2(20),analyse_value number(5,1),
                       pointer_condition_text varchar2(20),pointer_result varchar2(10),pointer_condition_text2 varchar2(100),
                       pointer_result2 varchar2(100),data_unit_name varchar2(20));
                       
insert into Test_Item values (1,1,'黏数',140,'143-136','优等品',null,null,'ml/g');
insert into Test_Item values (2,1,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (3,1,'吸油量',26,'>=23','优等品',null,null,'g');
insert into Test_Item values (4,1,'挥发物',0.5,'<=0.50','优等品',null,null,'%');
insert into Test_Item values (5,1,'250um筛孔筛余物',68,'>=23','优等品',null,null,'%');
insert into Test_Item values (6,2,'60um筛孔筛余物',1.2,'<=30','优等品',null,null,'%');
insert into Test_Item values (7,2,'杂质粒子数',1.2,'<=30','优等品',null,null,'个');
insert into Test_Item values (8,2,'白度',1.2,'<=30','优等品',null,null,'%');
insert into Test_Item values (9,2,'鱼眼',1.2,'<=30','优等品',null,null,'个/400cm');
insert into Test_Item values (10,2,'电导率',1.2,'<=30','优等品',null,null,'uS/(cm.g)');
insert into Test_Item values (11,2,'VCM残留量',1.2,'<=30','优等品',null,null,'ug/g');
insert into Test_Item values (12,2,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (13,3,'黏数',136,'143-136','合格品',null,null,'ml/g');
insert into Test_Item values (14,3,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (15,3,'吸油量',70,'>=23','一等品',null,null,'g');
insert into Test_Item values (16,3,'挥发物',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (17,3,'250um筛孔筛余物',78,'>=23','优等品',null,null,'%');
insert into Test_Item values (18,4,'60um筛孔筛余物',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (19,4,'杂质粒子数',1.2,'<=30','一等品',null,null,'个');
insert into Test_Item values (20,4,'白度烷',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (22,4,'鱼眼',1.2,'<=30','一等品',null,null,'个/400cm');
insert into Test_Item values (23,4,'电导率',1.2,'<=30','一等品',null,null,'uS/(cm.g)');
insert into Test_Item values (24,4,'VCM残留量',1.2,'<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','医用级','ug/g');
insert into Test_Item values (24,4,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (25,5,'黏数',136,'143-136','合格品',null,null,'ml/g');
insert into Test_Item values (26,5,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (27,5,'吸油量',70,'>=23','一等品',null,null,'g');
insert into Test_Item values (28,5,'挥发物',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (29,5,'250um筛孔筛余物',78,'>=23','优等品',null,null,'%');
insert into Test_Item values (30,6,'60um筛孔筛余物',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (31,6,'杂质粒子数',1.2,'<=30','一等品',null,null,'个');
insert into Test_Item values (32,6,'白度烷',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (33,6,'鱼眼',1.2,'<=30','一等品',null,null,'个/400cm');
insert into Test_Item values (34,6,'电导率',1.2,'<=30','一等品',null,null,'uS/(cm.g)');
insert into Test_Item values (35,6,'VCM残留量',1.2,'<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','食品级','ug/g');
insert into Test_Item values (36,6,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (37,7,'黏数',136,'143-136','合格品',null,null,'ml/g');
insert into Test_Item values (38,7,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (39,7,'吸油量',70,'>=23','一等品',null,null,'g');
insert into Test_Item values (40,7,'挥发物',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (41,7,'250um筛孔筛余物',78,'>=23','优等品',null,null,'%');
insert into Test_Item values (42,8,'60um筛孔筛余物',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (43,8,'杂质粒子数',1.2,'<=30','一等品',null,null,'个');
insert into Test_Item values (44,8,'白度烷',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (45,8,'鱼眼',1.2,'<=30','一等品',null,null,'个/400cm');
insert into Test_Item values (46,8,'电导率',1.2,'<=30','一等品',null,null,'uS/(cm.g)');
insert into Test_Item values (47,8,'VCM残留量',1.2,'<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','食品级','ug/g');
insert into Test_Item values (48,8,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (49,97,'含量',789,'>=99','一等品',null,null,'%');

insert into Test_Item values (50,10,'外观',0.4,'>=0.40','合格品',null,null,null);
insert into Test_Item values (51,10,'次氯酸钠含量',23,'>=23','合格品',null,null,'%');
insert into Test_Item values (52,10,'游离碱含量',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (53,10,'铁',1.2,'>=23','合格品',null,null,null);
commit; 

#22


表里的ID全是guid,没有规则的。

#23


纠结,继续等待。

#24


把帖子移到Oracle版块吧,与其在这不专一的地方等待,不如移到对口的版块,白天肯定有人愿意写的

#25


guid和数字序列差别大了,容易误导哦!是这样的吧:C25238FF54FF510EE040010A1AD751F9
引用 22 楼  的回复:
表里的ID全是guid,没有规则的。

#26


引用 25 楼  的回复:
guid和数字序列差别大了,容易误导哦!是这样的吧:C25238FF54FF510EE040010A1AD751F9
引用 22 楼  的回复:
表里的ID全是guid,没有规则的。
类似这样吧,反正是没有规则的。

#27


引用 23 楼  的回复:
纠结,继续等待。



哪有这么效率搞定的哇  【紧急求助】求一存储过程!!!!!!!!!!!!!

貌似后面的 平均只能单独列出来才行哇 

#28


引用 27 楼  的回复:
引用 23 楼  的回复:

纠结,继续等待。



哪有这么效率搞定的哇 

貌似后面的 平均只能单独列出来才行哇
,不管sql运行几次,只要能出这种结果就行了。
我直接和头说不会弄。他竟然还让我弄弄看。
无语。什么都往我这里丢。

#29


更正下  上面脚本 表3的49行  test_analyse_id为9  写成97了 

我说怎么查了半天 木看到gb/890后面有对应数据呢  【紧急求助】求一存储过程!!!!!!!!!!!!!

#30


累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber 


这里的producenumber 怎么取? 是1月份平均的producenumber还是?

#31


==哈,我问到了就回。

#32


累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber  

这里的producenumber是每个月份所有producenumber上加。

#33



select decode(lag(分析标准) over(order by 样品名称,分析标准),分析标准,'''''',分析标准) 分析标准,
       decode(lag(样品名称) over(order by 样品名称,分析标准),样品名称,'''''',样品名称) 样品名称,
       指标名称,
       单位,
       指标值,
       本月最高,
       本月最低,
       本月平均,
       (select round(sum(p_val*a_pro)/sum(s_pro),2) 
        from
        (
          select round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val,
                 round(avg(t1.producenumber),2) a_pro,
                 sum(t1.producenumber) s_pro
          from  test_sample t1,
                test_analyse t2,
                test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 t1.sampling_date < sysdate
          group by to_char(t1.sampling_date,'yyyy-mm')
         )
         ) 累计平均
from
(
    select b.analyse_method_name 分析标准,
           a.sample_name||a.pointer_result 样品名称,
           c.test_item_name 指标名称,
           c.data_unit_name 单位,
           c.pointer_condition_text 指标值,
           d.m_val 本月最高,
           d.n_val 本月最低,
           p_val 本月平均
    from test_analyse b 
         left join test_sample a on a.test_sample_id=b.test_sample_id
         left join test_item c on b.test_analyse_id=c.test_analyse_id,
         (select max(t3.analyse_value) m_val,
                 min(t3.analyse_value) n_val,
                 round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val
          from   test_sample t1,
                 test_analyse t2,
                 test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 to_char(t1.sampling_date,'yyyy-mm') = '2012-05'
          ) d
    where to_char(a.sampling_date,'yyyy-mm') = '2012-05'  --to_char(sysdate,'yyyy-mm')



    分析标准  样品名称   指标名称    单位    指标值  本月最高 本月最低 本月平均 累计平均
-----------------------------------------------------------------------------------------------
1 gb/12 SG3合格品 250um筛孔筛余物 % >=23 789 0.4 33.5 1.09
2 '' '' 吸油量 g >=23 789 0.4 33.5 1.09
3 '' '' 挥发物 % <=0.50 789 0.4 33.5 1.09
4 '' '' 黏数 ml/g 143-136 789 0.4 33.5 1.09
5 '' '' 表观密度 g/ml >=0.40 789 0.4 33.5 1.09
6 gb/34 '' 60um筛孔筛余物 % <=30 789 0.4 33.5 1.09
7 '' '' 杂质粒子数 个 <=30 789 0.4 33.5 1.09
8 '' '' 白度烷 % <=30 789 0.4 33.5 1.09
9 '' '' 鱼眼 个/400cm <=30 789 0.4 33.5 1.09
10 '' '' 1,1-二氯乙烷 mg/Kg 789 0.4 33.5 1.09
11 '' '' VCM残留量 ug/g <=30 789 0.4 33.5 1.09
12 '' '' 电导率 uS/(cm.g) <=30 789 0.4 33.5 1.09
13 gb/12 SG3一等品 250um筛孔筛余物 % >=23 789 0.4 33.5 1.09
14 '' '' 黏数 ml/g 143-136 789 0.4 33.5 1.09
15 '' '' 表观密度 g/ml >=0.40 789 0.4 33.5 1.09
16 '' '' 挥发物 % <=0.50 789 0.4 33.5 1.09
17 '' '' 吸油量 g >=23 789 0.4 33.5 1.09
18 gb/34 '' 杂质粒子数 个 <=30 789 0.4 33.5 1.09
19 '' '' 白度烷 % <=30 789 0.4 33.5 1.09
20 '' '' 鱼眼 个/400cm <=30 789 0.4 33.5 1.09
21 '' '' 电导率 uS/(cm.g) <=30 789 0.4 33.5 1.09
22 '' '' VCM残留量 ug/g <=30 789 0.4 33.5 1.09
23 '' '' 1,1-二氯乙烷 mg/Kg 789 0.4 33.5 1.09
24 '' '' 60um筛孔筛余物 % <=30 789 0.4 33.5 1.09
25 gb/12 SG3优等品 250um筛孔筛余物 % >=23 789 0.4 33.5 1.09
26 '' '' 挥发物 % <=0.50 789 0.4 33.5 1.09
27 '' '' 吸油量 g >=23 789 0.4 33.5 1.09
28 '' '' 黏数 ml/g 143-136 789 0.4 33.5 1.09
29 '' '' 表观密度 g/ml >=0.40 789 0.4 33.5 1.09
30 gb/34 '' VCM残留量 ug/g <=30 789 0.4 33.5 1.09
31 '' '' 电导率 uS/(cm.g) <=30 789 0.4 33.5 1.09
32 '' '' 鱼眼 个/400cm <=30 789 0.4 33.5 1.09
33 '' '' 白度 % <=30 789 0.4 33.5 1.09
34 '' '' 杂质粒子数 个 <=30 789 0.4 33.5 1.09
35 '' '' 1,1-二氯乙烷 mg/Kg 789 0.4 33.5 1.09
36 '' '' 60um筛孔筛余物 % <=30 789 0.4 33.5 1.09
37 gb/4389398 次氯酸钠 外观 >=0.40 789 0.4 33.5 1.09
38 '' '' 次氯酸钠含量 % >=23 789 0.4 33.5 1.09
39 '' '' 游离碱含量 % <=0.50 789 0.4 33.5 1.09
40 '' '' 铁 >=23 789 0.4 33.5 1.09
41 gb/890 氢气一等品 含量 % >=99 789 0.4 33.5 1.09

#34


引用 32 楼  的回复:
累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber  

这里的producenumber是每个月份所有producenumber上加。


那累计平均要改下咯


       (select round(sum(p_val*s_pro)/sum(s_pro),2) 
        from
        (
          select round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val,
                 sum(t1.producenumber) s_pro
          from  test_sample t1,
                test_analyse t2,
                test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 t1.sampling_date < sysdate and
                 to_char(t1.sampling_date,'yyyy-mm') = to_char(sysdate,'yyyy-mm')
          group by to_char(t1.sampling_date,'yyyy-mm')
         )
         ) 累计平均


 吃饭去咯 下午来整吧 

#35


看到好多关键字都不懂,囧,这样都不晓得正确不正确。
验证还要花大把时间。
sample_name + pointre_result2这个数据还木出来呢?

#36


添加了sample_name + pointre_result2   又花了十多分钟排版 不知道格式可以没?

select decode(lag(分析标准) over(order by 样品名称,分析标准),分析标准,'''''',分析标准) 分析标准,
       decode(lag(样品名称) over(order by 样品名称,分析标准),样品名称,'''''',样品名称) 样品名称,
       指标名称,
       单位,
       指标值,
       本月最高,
       本月最低,
       本月平均,
       (select round(sum(p_val*s_pro)/sum(s_pro),2) 
        from
        (
          select round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val,
                 sum(t1.producenumber) s_pro
          from  test_sample t1,
                test_analyse t2,
                test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 t1.sampling_date < sysdate and
                 to_char(t1.sampling_date,'yyyy-mm') = to_char(sysdate,'yyyy-mm')
          group by to_char(t1.sampling_date,'yyyy-mm')
         )
         ) 累计平均
from
(
    select b.analyse_method_name 分析标准,
           e.sp 样品名称,
           c.test_item_name 指标名称,
           c.data_unit_name 单位,
           c.pointer_condition_text 指标值,
           d.m_val 本月最高,
           d.n_val 本月最低,
           p_val 本月平均
    from test_analyse b 
         left join test_sample a on a.test_sample_id=b.test_sample_id
         left join test_item c on b.test_analyse_id=c.test_analyse_id,
         (select test_sample_id,sample_name||pointer_result sp
           from  test_sample 
           union all
           select  test_sample_id,sample_name||pointer_result2
           from  test_sample
           where pointer_result2 is not null) e,
         (select max(t3.analyse_value) m_val,
                 min(t3.analyse_value) n_val,
                 round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val
          from   test_sample t1,
                 test_analyse t2,
                 test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 to_char(t1.sampling_date,'yyyy-mm') = '2012-05'
          ) d
    where a.test_sample_id=e.test_sample_id and 
          to_char(a.sampling_date,'yyyy-mm') = '2012-05'  --to_char(sysdate,'yyyy-mm')



分析标准  样品名称      指标名称        单位   指标值  本月最高 本月最低 本月平均 累计平均
-----------------------------------------------------------------------------------------------
gb/12 SG3合格品 250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
gb/34 ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         鱼眼         个/400cm <=30 789 0.4 33.5 24.39
'' ''         白度烷         % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
gb/12 SG3食品级 表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
gb/34 ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         白度烷         % <=30 789 0.4 33.5 24.39
'' ''         电导率        uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         鱼眼        个/400cm <=30 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
gb/12 SG3一等品 吸油量         g >=23 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
gb/34 ''         白度烷         % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         鱼眼        个/400cm <=30 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
gb/12 SG3医用级 挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
gb/34 ''         鱼眼 个/400cm <=30 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         白度烷         % <=30 789 0.4 33.5 24.39
gb/12 SG3优等品 黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
gb/34 ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         白度         % <=30 789 0.4 33.5 24.39
'' ''         鱼眼         个/400cm<=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
gb/4389398         次氯酸钠 外观 >=0.40 789 0.4 33.5 24.39
'' ''         次氯酸钠含量 % >=23 789 0.4 33.5 24.39
'' ''         游离碱含量 % <=0.50 789 0.4 33.5 24.39
'' ''         铁         >=23 789 0.4 33.5 24.39
gb/890 氢气一等品 含量         % >=99 789 0.4 33.5 24.39

#37


【紧急求助】求一存储过程!!!!!!!!!!!!!还素没排好 第一排有值的 第二排没后推    怎么看着像W型呢 【紧急求助】求一存储过程!!!!!!!!!!!!! 

#38


发现有些地方逻辑需要改的。
sample_name + pointer_result2这个取得数据是有规则的。
1:只取pointer_result2不为空的值,并且pointer_result2="食品级"的数据。然后唯一例外的就是根据test_item表里的pointer_condition2值不空,并且包含有test__item_name,例如VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150",它包含两个test_item_name
1个是VCM残留量,第2个是1,1-二氯乙烷
所以这里它只取两个test_item_name
就是我上面给出的

分析标准(analyse_method_name)        样品名称                   指标名称(test_item_name)    单位(data_unit_name)     指标值        本月最高    本月最低    本月平均   累计平均
 gb/34                          SG3食品级                  VCM残留量                   ml/g                    <=1            56            55         455       45                                                                     
     ,,                                  ,,                   1,1-二氯乙烷                 g/ml                    <=150          56            55         455       45                                                                                          


然后指标值就是根据VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150"
得到VCM残留量为“<=1”,1,1-二氯乙烷为"<=150"
2:然后它的本月最高、本月最低、本月平均、累计平均
需要加上SG3这个test_sample_name的pointer_result为“一等品”“优等品”“食品级”“医用级”这些数据来求最高、最低、平均、和累计平均。。

#39


介么多数据,表示会看的头晕,帮顶 【紧急求助】求一存储过程!!!!!!!!!!!!!

#40


么多数据,表示会看的头晕,帮顶

#41


其他都可以了  就这个

然后唯一例外的就是根据test_item表里的pointer_condition2值不空,并且包含有test__item_name,例如VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150",它包含两个test_item_name
1个是VCM残留量,第2个是1,1-二氯乙烷


test__item_name里面也存在逗号  pointer_condition2也都是逗号分开 ,我怎么来判断  刚好包涵了几个test__item_name呢 ,用ondition2匹配所有的name 看包涵几个name? 即使完成了 貌似提取字段也是个问题哇?

#42


引用 41 楼  的回复:
其他都可以了  就这个

然后唯一例外的就是根据test_item表里的pointer_condition2值不空,并且包含有test__item_name,例如VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150",它包含两个test_item_name
1个是VCM残留量,第2个是1,1-二氯乙烷


test__it……
只能通过test_item表里的pointer_condition2里面包含几个test_item字段来了,,

#43


这样吧,我让pointer_condition2里面不用逗号分隔,而是用两个"$$",例如这样
VCM 残留量<=1$$1,1-二氯乙烷<=150.
这样的话,好处理不

#44


然后这样的话应该好提取了,
分隔完之后就是
两个test_item_name
VCM 残留量<=1、1,1-二氯乙烷<=150
然后再把test_item_name清空赋值到字段 指标值里。
这样的话就出这个结果了。

分析标准(analyse_method_name)        样品名称                   指标名称(test_item_name)    单位(data_unit_name)     指标值        本月最高    本月最低    本月平均   累计平均
 gb/34                          SG3食品级                  VCM残留量                   ml/g                    <=1            56            55         455       45                                                                     
     ,,                                  ,,                   1,1-二氯乙烷                 g/ml                    <=150          56            55         455       45   

#45


这个计算还不得把人算哭了啊?

#46


引用 45 楼  的回复:
这个计算还不得把人算哭了啊?
婶婶,木办法啊,
我都要哭了。

#47


我在想  为什么  不直接分开写在对应的name 里面呢   要几个联合起来写在ondition2里面 


这个程序 插入值的时候 是这吗插入的 那时候就可以判断  分割字符在对应name的ondition2里面呢

也可以为统计节省一点时间了

#48


引用 46 楼  的回复:
引用 45 楼  的回复:

这个计算还不得把人算哭了啊?
婶婶,木办法啊,
我都要哭了。


  好吧、偶也要哭了   【紧急求助】求一存储过程!!!!!!!!!!!!!

#49


引用 47 楼  的回复:
我在想  为什么  不直接分开写在对应的name 里面呢   要几个联合起来写在ondition2里面 


这个程序 插入值的时候 是这吗插入的 那时候就可以判断  分割字符在对应name的ondition2里面呢

也可以为统计节省一点时间了
最高值,最低值的数据不对,
你取的是test_item表里的最高,最低,而不是每个test_item_name的最高最低,,
不知道平均和累计平均是不是也算错了,,

#50


引用 47 楼  的回复:
我在想  为什么  不直接分开写在对应的name 里面呢   要几个联合起来写在ondition2里面 


这个程序 插入值的时候 是这吗插入的 那时候就可以判断  分割字符在对应name的ondition2里面呢

也可以为统计节省一点时间了
,特么的,说起这个就来气。
程序是别人写的,
他们让我来写这个统计。
我肏。
说了我不会,还硬让我先写写。

#1


挖靠,内容很多,放不下。

#2


1:Test_Sample检测样品表
test_sample_id  sample_name  pointer_result  pointer_result2 producenumber   sampling_date
 1                 SG3        优等品                           100            2012-05-02 13:00:00
 2                 SG3        合格品          医用级            80             2012-05-03 13:00:00
 3                 SG3        一等品          食品级            80             2012-05-03 13:00:00
 4                 氢气       一等品                            50             2012-05-04 13:00:00
 5                 SG3        一等品          食品级            80             2012-06-03 13:00:00
 6                 次氯酸钠                                     40             2012-05-02 13:00:00

................更多数据

#3



2:Test_Analyse检测分析表
test_analyse_id  analyse_name     test_sample_id  analyse_method_name 
1                   水分析            1                 gb/12
2                   油分析            1                 gb/34
3                   水分析            2                 gb/12
4                   油分析            2                 gb/34
5                   水分析            3                 gb/12
6                   油分析            3                 gb/34
7                   水分析            5                 gb/12
8                   油分析            5                 gb/34
9                   固体分析          4                 gb/890
10                  次氯酸钠的测定    6                gb/4389398
.........................更多数据

3:Test_Item检测项目表
test_item_id test_analyse_id  test_item_name analyse_value pointer_condition_text pointer_result       pointer_condition_text2               pointer_result2  data_unit_name 
1             1                 黏数            140          143-136                  优等品                                                                      ml/g
2             1                 表观密度        0.5          >=0.40                   优等品                                                                      g/ml
3             1                 吸油量          26           >=23                     优等品                                                                      g
4             1                 挥发物          0.5          <=0.50                   优等品                                                                      %
5             1                 250um筛孔筛余物 68           >=23                     优等品                                                                      %
6             2                 60um筛孔筛余物  1.2          <=30                     优等品                                                                      %
7             2                 杂质粒子数      1.2          <=30                     优等品                                                                     个
8             2                 白度            1.2          <=30                     优等品                                                                      %
9             2                 鱼眼            1.2          <=30                     优等品                                                                     个/400cm
10             2                 电导率          1.2          <=30                    优等品                                                                      uS/(cm.g
11            2                 VCM残留量       1.2          <=30                     优等品                                                                      ug/g
12             2                 1,1-二氯乙烷    0.5                                                                                                                mg/Kg 


13             3                 黏数            136          143-136                  合格品                                                                    ml/g
14             3                 表观密度        0.5          >=0.40                   优等品                                                                    g/ml
15             3                 吸油量          70           >=23                     一等品                                                                    g
16             3                 挥发物          0.5          <=0.50                   合格品                                                                    %
17             3                 250um筛孔筛余物 78           >=23                     优等品                                                                    %
18             4                 60um筛孔筛余物  1.2          <=30                     一等品                                                                    %
19             4                 杂质粒子数      1.2          <=30                     一等品                                                                   个
20             4                 白度            1.2          <=30                     一等品                                                                    %
21             4                 鱼眼            1.2          <=30                     一等品                                                                    个/400cm
22             4                 电导率          1.2          <=30                     一等品                                                                    uS/(cm.g
23             4                 VCM残留量       1.2          <=30                     一等品           VCM残留量<=5,1,1-二氯乙烷<=150          医用级           ug/g
24             4                 1,1-二氯乙烷    0.5                                                                                                             mg/Kg 


25             5                 黏数            136          143-136                  合格品                                                                    ml/g
26             5                 表观密度        0.4          >=0.40                   合格品                                                                    g/ml
27             5                 吸油量          23           >=23                     合格品                                                                   g
28             5                 挥发物          0.5          <=0.50                   合格品                                                                    %
29             5                 250um筛孔筛余物 1.2          >=23                     合格品                                                                     %
30             6                 60um筛孔筛余物  1.2          <=30                     一等品                                                                     %
31             6                 杂质粒子数      1.2          <=30                     一等品                                                                    个
32             6                 白度            1.2          <=30                     一等品                                                                     %
33             6                 鱼眼            1.2          <=30                     一等品                                                                 个/400cm
34             6                 电导率          1.2          <=30                     一等品                                                                 uS/(cm.g
35             6                 VCM残留量       1.2          <=30                     一等品          VCM残留量<=1,1,1-二氯乙烷<=150           食品级         ug/g
36             6                 1,1-二氯乙烷    0.5                                                                                                           mg/Kg 


37             7                 黏数            136          143-136                  合格品                                                                    ml/g
38             7                 表观密度        0.4          >=0.40                   合格品                                                                    g/ml
39             7                 吸油量          23           >=23                     合格品                                                                   g
40             7                 挥发物          0.5          <=0.50                   合格品                                                                    %
41             7                 250um筛孔筛余物 1.2          >=23                     合格品                                                                     %
42             8                 60um筛孔筛余物  1.2          <=30                     一等品                                                                     %
43             8                 杂质粒子数      1.2          <=30                     一等品                                                                    个
44             8                 白度            1.2          <=30                     一等品                                                                     %
45             8                 鱼眼            1.2          <=30                     一等品                                                                 个/400cm
46             8                 电导率          1.2          <=30                     一等品                                                                 uS/(cm.g
47             8                 VCM残留量       1.2          <=30                     一等品          VCM残留量<=1,1,1-二氯乙烷<=150           食品级         ug/g
48             8                 1,1-二氯乙烷    0.5                                                                                                           mg/Kg 

49             9                 含量            789          >=99                    一等品                                                                       %


50             10                 外观           0.4          >=0.40                   合格品                                                                    
51             10                 次氯酸钠含量   23           >=23                     合格品                                                                      %
52             10                 游离碱含量     0.5          <=0.50                   合格品                                                                      %
53             10                 铁             1.2         >=23                      合格品                                                                      %
..................................更多数据

#4



想要的查询结果为(括号里代表字段名称),没有名称代表的是需要通过其他字段计算出来。
分析标准(analyse_method_name)        样品名称                   指标名称(test_item_name)    单位(data_unit_name)     指标值        本月最高    本月最低    本月平均   累计平均
    gb/12                            SG3优等品                  黏数                       ml/g                    143-136          56            55         455       45                                                                     
     ,,                                  ,,                   表观密度                    g/ml                     >=0.40           56            55         455       45                                                                            
     ,,                                  ,,                   吸油量                        g                      >=23            56            55         455       45                                                                           
     ,,                                  ,,                    挥发物                       %                      <=0.50          56            55         455       45                                                                            
     ,,                                  ,,                 250um筛孔筛余物                 %                     >=23             56            55         455       45                                                                            
    gb/34                                ,,                 60um筛孔筛余物                  %                      <=30           56            55         455       45                                                                              
     ,,                                  ,,                  杂质粒子数                     个                     <=30            56            55         455       45                                                                           
     ,,                                  ,,                    白度                         %                     <=30            56            55         455       45                                                                             
     ,,                                  ,,                   鱼眼                          个/400cm              <=30             56            55         455       45                                                                     
     ,,                                  ,,                 电导率                         uS/(cm.g                <=30           56            55         455       45                                                                           
     ,,                                  ,,                  VCM残留量                     ug/g                    <=30           56            55         455       45    
 
  gb/12                          SG3合格品                  黏数                       ml/g                    143-136          56            55         455       45                                                                     
     ,,                                  ,,                   表观密度                    g/ml                     >=0.40           56            55         455       45                                                                            
     ,,                                  ,,                   吸油量                        g                      >=23            56            55         455       45                                                                           
     ,,                                  ,,                    挥发物                       %                      <=0.50          56            55         455       45                                                                            
     ,,                                  ,,                 250um筛孔筛余物                 %                     >=23             56            55         455       45                                                                            
    gb/34                                ,,                 60um筛孔筛余物                  %                      <=30           56            55         455       45                                                                              
     ,,                                  ,,                  杂质粒子数                     个                     <=30            56            55         455       45                                                                           
     ,,                                  ,,                    白度                         %                     <=30            56            55         455       45                                                                             
     ,,                                  ,,                   鱼眼                          个/400cm              <=30             56            55         455       45                                                                     
     ,,                                  ,,                 电导率                         uS/(cm.g                <=30           56            55         455       45                                                                           
     ,,                                  ,,                  VCM残留量                     ug/g                    <=30           56            55         455       45      
 
  gb/12                          SG3一等品                  黏数                       ml/g                    143-136          56            55         455       45                                                                     
     ,,                                  ,,                   表观密度                    g/ml                     >=0.40           56            55         455       45                                                                            
     ,,                                  ,,                   吸油量                        g                      >=23            56            55         455       45                                                                           
     ,,                                  ,,                    挥发物                       %                      <=0.50          56            55         455       45                                                                            
     ,,                                  ,,                 250um筛孔筛余物                 %                     >=23             56            55         455       45                                                                            
    gb/34                                ,,                 60um筛孔筛余物                  %                      <=30           56            55         455       45                                                                              
     ,,                                  ,,                  杂质粒子数                     个                     <=30            56            55         455       45                                                                           
     ,,                                  ,,                    白度                         %                     <=30            56            55         455       45                                                                             
     ,,                                  ,,                   鱼眼                          个/400cm              <=30             56            55         455       45                                                                     
     ,,                                  ,,                 电导率                         uS/(cm.g                <=30           56            55         455       45                                                                           
     ,,                                  ,,                  VCM残留量                     ug/g                    <=30           56            55         455       45                                       

 gb/34                          SG3食品级                  VCM残留量                   ml/g                    <=1            56            55         455       45                                                                     
     ,,                                  ,,                   1,1-二氯乙烷                 g/ml                    <=150          56            55         455       45                                                                                                                                                                

 gb/890                          氢气一等品                含量                         ml/g                    143-136          56            55        455       45 

 gb/890                          次氯酸钠                外观                                                   143-136          56            55        455       45 
  ,,                              ,,                  次氯酸钠含量                    ml/g                      143-136          56            55        455       45 
  ,,                              ,,                  游离碱含量                      ml/g                     143-136         56            55        455       45 
  ,,                              ,,                     铁                           ml/g                     143-136          56            55        455       45 
  .................................更多数据

#5



  说明:
    以上数据都是我乱造的,但是仔细看还是能看懂,看出端倪的。
1、检测样品表的test_sample_id是检测分析表的外键,检测分析表的test_analyse_id是检测项目表的外键
2。样品名称通过test_sample表里的test_sample_nam+pointer_result或者为test_sample_name+pointer_result2组合而成。
3.分析标准、样品名称下行记录如果与上行记录相同,则用两个逗号",,"表示。
4.本月最高取test_item表里analyse_value最大值。最低取analyse_value最小值。
5.本月平均计算公式为:
第一个结果analyse_value*producenumber(test_sample表里)+第二个结果analyse_value*producenumber+第N个结果analyse_value*producenumber/第一个producenumber+第二个producenumber+第n个producenumber
6.累计平均计算公式为:
根据输入参数samplingStartTime和sapmlingEndTime  test_sample表的  samplingStartTime<=sampling_date<=samplingEndtime过滤数据得到当前月份的前面几个月的累计。
    如:输入参数samplingStartTime值为:2012-05-01 00:00:00,samplingEndTime值为:2012-05-31 23:59:59
累计平均 1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber         
7。以上结果集是先按照test_sample的pointer_result分组出,然后再按pointer_result2分组出。
按照pointer_result2分组出的数据有点小规则,如:SG3食品级,这个它只出了两个项目,VCM残留量和1,1-二氯已烷,按照如下规则
VCM残留量的pointer_condition2为 VCM残留量<=1,1,1-二氯乙烷<=150
所以指标值这一列,VCM残留量为:<=1,1,1二氯已烷为:<=150,是根据pointer_condition2以逗号为分隔取出VCM残留量<=1和1-二氯乙烷<=150,所以把<=1赋值到VCM残留量,把<=150赋值到1-二氯乙烷。
如果是其他项目的pointer_condition2也是按照这种逻辑取取项目名称和指标值的。


    麻烦大家弄个oracle下的存储过程,输入参数就是samplingStartTime和samplingEndTime.

#6


oracle不熟,oracle板块还是有好几个大牛,建议LZ把帖子移过去!

#7


写法应该差不多的吧,就是可能一些小细节要注意下。。

#8


sql版比较旺点,所以发介了。
8过最近人好像也变挺少了

#9


擦,这么多数据,看得头晕 【紧急求助】求一存储过程!!!!!!!!!!!!!

#10


引用 9 楼  的回复:
擦,这么多数据,看得头晕
木办法啊,
这些逻辑还是想了2、3天才理通。。
写这个帖子也不容易啊。
麻烦大家看看了。

#11


Oracle的我也无能为力了,帮顶一下!! 【紧急求助】求一存储过程!!!!!!!!!!!!!

#12



1、检测样品表的test_sample_id是检测分析表的外键,检测分析表的test_analyse_id是检测项目表的外键.
--这就是3表连接查询即可。

2、样品名称通过test_sample表里的test_sample_name+pointer_result或test_sample_name +pointer_result2组合而成。
--什么时候用test_sample_name + pointer_result  ?
--什么时候用test_sample_name + pointer_result2 ?

3.分析标准、样品名称下行记录如果与上行记录相同,则用两个逗号",,"表示。
--oracle中的序号和sql server 中的序号不一样。

4.本月最高取test_item表里analyse_value最大值。最低取analyse_value最小值。
--这个我估计是个嵌套子查询。

5.本月平均计算公式为:
第一个结果analyse_value*producenumber(test_sample表里)+
第二个结果analyse_value*producenumber+
第N个结果analyse_value*producenumber/第一个producenumber+第二个producenumber+第n个producenumber

--这个是不是少了括号呀?

6.累计平均计算公式为:
根据输入参数samplingStartTime和sapmlingEndTime  
test_sample表的samplingStartTime<=sampling_date<=samplingEndtime
过滤数据得到当前月份的前面几个月的累计。

如:输入参数
samplingStartTime值为:2012-05-01 00:00:00,
samplingEndTime值为:2012-05-31 23:59:59

累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber  

--这个是不是也少了括号?
              
7。以上结果集是先按照test_sample的pointer_result分组出,然后再按pointer_result2分组出。

--最后把两个结果 union all在一起?

按照pointer_result2分组出的数据有点小规则,如:SG3食品级,这个它只出了两个项目,VCM残留量和1,1-二氯已烷,按照如下规则 
VCM残留量的pointer_condition2为 VCM 残留量<=1,1,1-二氯乙烷<=150
所以指标值这一列,VCM残留量为:<=1,1,1二氯已烷为:<=150,
是根据pointer_condition2以逗号为分隔取出VCM残留量<=1和1-二氯乙烷<=150,
所以把<=1赋值到VCM残留量,把<=150赋值到1-二氯乙烷。

--这句我看了半天,貌似是没看懂...

如果是其他项目的pointer_condition2也是按照这种逻辑取取项目名称和指标值的。



懂Oracle的兄弟帮忙解决一下吧

#13


我这以上7条是为最后想要出的那个结果描述一下是怎么出那个数据的。
并不是按照1、2、3、4、56、7。根据这7个条件出数据。

#14


什么时候用test_sample_name + pointer_result  ?
--什么时候用test_sample_name + pointer_result2 ?
这个就是先按test_sample表里的pointer_result分组,
这样的话就是test_sample_name + pointer_result.
然后再按test_sample表里的pointer_result2分组。
这样的话就是test_sample_name+pointer_result2

#15


回复提示 此论坛木有这个帖子?  test下 【紧急求助】求一存储过程!!!!!!!!!!!!!

#16


引用 15 楼  的回复:
回复提示 此论坛木有这个帖子?  test下
现在应该可以鸟,,,
刚刚移动到水区加了点分上去。。

#17


看这仗势  貌似建表也要花个几十分钟咯  【紧急求助】求一存储过程!!!!!!!!!!!!!

#18


还是期待明天来更多人吧,
呵呵。
好困,介些天都超困。
不等了。希望明天有好结果。!

#19


引用 17 楼  的回复:
看这仗势  貌似建表也要花个几十分钟咯

数据里面有逗号,个别字段没有数据,数据脚本我折腾了好半天....


declare @Test_Sample table 
(
test_sample_id int,
sample_name varchar(8),
pointer_result varchar(6),
pointer_result2 varchar(6),
producenumber int,
sampling_date datetime
)
insert into @Test_Sample
select 1,'SG3','优等品',null,100,'2012-05-02 13:00:00' union all
select 2,'SG3','合格品','医用级',80,'2012-05-03 13:00:00' union all
select 3,'SG3','一等品','食品级',80,'2012-05-03 13:00:00' union all
select 4,'氢气','一等品',null,50,'2012-05-04 13:00:00' union all
select 5,'SG3','一等品','食品级',80,'2012-06-03 13:00:00' union all
select 6,'次氯酸钠',null,null,40,'2012-05-02 13:00:00'

declare @Test_Analyse table 
(
test_analyse_id int,
analyse_name varchar(14),
test_sample_id int,
analyse_method_name varchar(10)
)
insert into @Test_Analyse
select 1,'水分析',1,'gb/12' union all
select 2,'油分析',1,'gb/34' union all
select 3,'水分析',2,'gb/12' union all
select 4,'油分析',2,'gb/34' union all
select 5,'水分析',3,'gb/12' union all
select 6,'油分析',3,'gb/34' union all
select 7,'水分析',5,'gb/12' union all
select 8,'油分析',5,'gb/34' union all
select 9,'固体分析',4,'gb/890' union all
select 10,'次氯酸钠的测定',6,'gb/4389398'

declare @Test_Item table 
(
test_item_id int,
test_analyse_id int,
test_item_name varchar(15),
analyse_value varchar(10),
pointer_condition_text varchar(7),
pointer_result varchar(6),
pointer_condition_text2 varchar(32),
pointer_result2 varchar(10),
data_unit_name varchar(15)
)
insert into @Test_Item
select 1,1,'黏数','140','143-136','优等品',null,null,'ml/g' union all
select 2,1,'表观密度','0.5','>=0.40','优等品',null,null,'g/ml' union all
select 3,1,'吸油量','26','>=23','优等品',null,null,'g' union all
select 4,1,'挥发物','0.5','<=0.50','优等品',null,null,'%' union all
select 5,1,'250um筛孔筛余物','68','>=23','优等品',null,null,'%' union all
select 6,2,'60um筛孔筛余物','1.2','<=30','优等品',null,null,'%' union all
select 7,2,'杂质粒子数','1.2','<=30','优等品',null,null,'个' union all
select 8,2,'白度','1.2','<=30','优等品',null,null,'%' union all
select 9,2,'鱼眼','1.2','<=30','优等品',null,null,'个/400cm' union all
select 10,2,'电导率','1.2','<=30','优等品',null,null,'uS/(cm.g' union all
select 11,2,'VCM残留量','1.2','<=30','优等品',null,null,'ug/g' union all
select 12,2,'1','1-二氯乙烷','0.5','mg/Kg',null,null,null union all
select 13,3,'黏数','136','143-136','合格品',null,null,'ml/g' union all
select 14,3,'表观密度','0.5','>=0.40','优等品',null,null,'g/ml' union all
select 15,3,'吸油量','70','>=23','一等品',null,null,'g' union all
select 16,3,'挥发物','0.5','<=0.50','合格品',null,null,'%' union all
select 17,3,'250um筛孔筛余物','78','>=23','优等品',null,null,'%' union all
select 18,4,'60um筛孔筛余物','1.2','<=30','一等品',null,null,'%' union all
select 19,4,'杂质粒子数','1.2','<=30','一等品',null,null,'个' union all
select 20,4,'白度','1.2','<=30','一等品',null,null,'%' union all
select 21,4,'鱼眼','1.2','<=30','一等品',null,null,'个/400cm' union all
select 22,4,'电导率','1.2','<=30','一等品',null,null,'uS/(cm.g' union all
select 23,4,'VCM残留量','1.2','<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','医用级','ug/g' union all
select 24,4,'1','1-二氯乙烷','0.5',null,null,null,'mg/Kg' union all
select 25,5,'黏数','136','143-136','合格品',null,null,'ml/g' union all
select 26,5,'表观密度','0.4','>=0.40','合格品',null,null,'g/ml' union all
select 27,5,'吸油量','23','>=23','合格品',null,null,'g' union all
select 28,5,'挥发物','0.5','<=0.50','合格品',null,null,'%' union all
select 29,5,'250um筛孔筛余物','1.2','>=23','合格品',null,null,'%' union all
select 30,6,'60um筛孔筛余物','1.2','<=30','一等品',null,null,'%' union all
select 31,6,'杂质粒子数','1.2','<=30','一等品',null,null,'个' union all
select 32,6,'白度','1.2','<=30','一等品',null,null,'%' union all
select 33,6,'鱼眼','1.2','<=30','一等品',null,null,'个/400cm' union all
select 34,6,'电导率','1.2','<=30','一等品',null,null,'uS/(cm.g' union all
select 35,6,'VCM残留量','1.2','<=30','一等品','VCM残留量<=1,1,1-二氯乙烷<=150','食品级','ug/g' union all
select 36,6,'1','1-二氯乙烷','0.5',null,null,null,'mg/Kg' union all
select 37,7,'黏数','136','143-136','合格品',null,null,'ml/g' union all
select 38,7,'表观密度','0.4','>=0.40','合格品',null,null,'g/ml' union all
select 39,7,'吸油量','23','>=23','合格品',null,null,'g' union all
select 40,7,'挥发物','0.5','<=0.50','合格品',null,null,'%' union all
select 41,7,'250um筛孔筛余物','1.2','>=23','合格品',null,null,'%' union all
select 42,8,'60um筛孔筛余物','1.2','<=30','一等品',null,null,'%' union all
select 43,8,'杂质粒子数','1.2','<=30','一等品',null,null,'个' union all
select 44,8,'白度','1.2','<=30','一等品',null,null,'%' union all
select 45,8,'鱼眼','1.2','<=30','一等品',null,null,'个/400cm' union all
select 46,8,'电导率','1.2','<=30','一等品',null,null,'uS/(cm.g' union all
select 47,8,'VCM残留量','1.2','<=30','一等品','VCM残留量<=1,1,1-二氯乙烷<=150','食品级','ug/g' union all
select 48,8,'1','1-二氯乙烷','0.5',null,null,null,'mg/Kg' union all
select 49,9,'含量','789','>=99','一等品',null,null,'%' union all
select 50,10,'外观','0.4','>=0.40','合格品',null,null,null union all
select 51,10,'次氯酸钠含量','23','>=23','合格品',null,null,'%' union all
select 52,10,'游离碱含量','0.5','<=0.50','合格品',null,null,'%' union all
select 53,10,'铁','1.2','>=23','合格品',null,null,null

select * from @Test_Sample
select * from @Test_Analyse
select * from @Test_Item

#20


用sql server 写了个大概...

;with maco as
(
select 
b.analyse_method_name as 分析标准,
        c.pointer_condition_text,
c.pointer_result as 样品名称,
        c.test_item_name as 指标名称,
        c.data_unit_name as 单位,
        c.analyse_value as 指标值,
        a.sampling_date as 时间
from @Test_Item c 
left join @Test_Analyse b on c.test_analyse_id=b.test_analyse_id
left join @Test_Sample a on b.test_sample_id=a.test_sample_id
),
m1 as 
(
select *,row_number() over(order by (select 1)) as id from maco
)

select 
case when a.分析标准=b.分析标准 then ',,' else a.分析标准 end as 分析标准,
    case when a.样品名称=b.样品名称 then ',,' else a.样品名称 end as 样品名称,
a.指标名称,a.单位,
a.指标值,a.时间 ,
    (select max(指标值) from m1 
    where pointer_condition_text=a.pointer_condition_text and
    样品名称=a.样品名称
    ) as 最大值,
    (select min(指标值) from m1 
    where pointer_condition_text=a.pointer_condition_text and
    样品名称=a.样品名称
    ) as 最小值
from m1 a 
left join m1 b on a.id=b.id+1
/*
分析标准       样品名称   指标名称            单位              指标值        时间                      最大值        最小值
---------- ------ --------------- --------------- ---------- ----------------------- ---------- ----------
gb/12      优等品    黏数              ml/g            140        2012-05-02 13:00:00.000 140        140
,,         ,,     表观密度            g/ml            0.5        2012-05-02 13:00:00.000 0.5        0.5
,,         ,,     吸油量             g               26         2012-05-02 13:00:00.000 78         26
,,         ,,     挥发物             %               0.5        2012-05-02 13:00:00.000 0.5        0.5
,,         ,,     250um筛孔筛余物      %               68         2012-05-02 13:00:00.000 78         26
gb/34      ,,     60um筛孔筛余物       %               1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-05-02 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-05-02 13:00:00.000 NULL       NULL
gb/12      合格品    黏数              ml/g            136        2012-05-03 13:00:00.000 136        136
,,         优等品    表观密度            g/ml            0.5        2012-05-03 13:00:00.000 0.5        0.5
,,         一等品    吸油量             g               70         2012-05-03 13:00:00.000 70         70
,,         合格品    挥发物             %               0.5        2012-05-03 13:00:00.000 0.5        0.5
,,         优等品    250um筛孔筛余物      %               78         2012-05-03 13:00:00.000 78         26
gb/34      一等品    60um筛孔筛余物       %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-05-03 13:00:00.000 NULL       NULL
gb/12      合格品    黏数              ml/g            136        2012-05-03 13:00:00.000 136        136
,,         ,,     表观密度            g/ml            0.4        2012-05-03 13:00:00.000 0.4        0.4
,,         ,,     吸油量             g               23         2012-05-03 13:00:00.000 23         1.2
,,         ,,     挥发物             %               0.5        2012-05-03 13:00:00.000 0.5        0.5
,,         ,,     250um筛孔筛余物      %               1.2        2012-05-03 13:00:00.000 23         1.2
gb/34      一等品    60um筛孔筛余物       %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-05-03 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-05-03 13:00:00.000 NULL       NULL
gb/12      合格品    黏数              ml/g            136        2012-06-03 13:00:00.000 136        136
,,         ,,     表观密度            g/ml            0.4        2012-06-03 13:00:00.000 0.4        0.4
,,         ,,     吸油量             g               23         2012-06-03 13:00:00.000 23         1.2
,,         ,,     挥发物             %               0.5        2012-06-03 13:00:00.000 0.5        0.5
,,         ,,     250um筛孔筛余物      %               1.2        2012-06-03 13:00:00.000 23         1.2
gb/34      一等品    60um筛孔筛余物       %               1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     杂质粒子数           个               1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     白度              %               1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     鱼眼              个/400cm         1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     电导率             uS/(cm.g        1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         ,,     VCM残留量          ug/g            1.2        2012-06-03 13:00:00.000 1.2        1.2
,,         NULL   1               mg/Kg           1-二氯乙烷     2012-06-03 13:00:00.000 NULL       NULL
gb/890     一等品    含量              %               789        2012-05-04 13:00:00.000 789        789
gb/4389398 合格品    外观              NULL            0.4        2012-05-02 13:00:00.000 0.4        0.4
,,         ,,     次氯酸钠含量          %               23         2012-05-02 13:00:00.000 23         1.2
,,         ,,     游离碱含量           %               0.5        2012-05-02 13:00:00.000 0.5        0.5
,,         ,,     铁               NULL            1.2        2012-05-02 13:00:00.000 23         1.2

(53 row(s) affected)

*/

#21


拼命了  表现建好了  给想做的人省些时间 数据应该对应了  花了30多分钟 不行了 夏眠 【紧急求助】求一存储过程!!!!!!!!!!!!!



--1、检测样品表
create table Test_Sample (test_sample_id number(5),sample_name varchar2(20),pointer_result varchar2(10),
                          pointer_result2 varchar2(10),producenumber number(3),sampling_date date);
insert into Test_Sample values (1,'SG3','优等品',null,100,to_date('2012-05-02 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (2,'SG3','合格品','医用级',80,to_date('2012-05-03 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (3,'SG3','一等品','食品级',80,to_date('2012-05-04 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (4,'氢气','一等品',null,50,to_date('2012-05-05 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (5,'SG3','一等品','食品级',80,to_date('2012-06-03 13:00:00','yyyy-mm-dd hh24:mi:ss'));
insert into Test_Sample values (6,'次氯酸钠',null,null,40,to_date('2012-05-02 13:00:00','yyyy-mm-dd hh24:mi:ss'));

--2、检测分析表
create table Test_Analyse (test_analyse_id number(5),analyse_name varchar2(20),test_sample_id number(5),analyse_method_name varchar2(20));
insert into Test_Analyse values (1,'水分析',1,'gb/12');
insert into Test_Analyse values (2,'油分析',1,'gb/34');
insert into Test_Analyse values (3,'水分析',2,'gb/12');
insert into Test_Analyse values (4,'油分析',2,'gb/34');
insert into Test_Analyse values (5,'水分析',3,'gb/12');
insert into Test_Analyse values (6,'油分析',3,'gb/34');
insert into Test_Analyse values (7,'水分析',5,'gb/12');
insert into Test_Analyse values (8,'油分析',5,'gb/34');
insert into Test_Analyse values (9,'固体分析',4,'gb/890');
insert into Test_Analyse values (10,'次氯酸钠的测定',6,'gb/4389398');

--3、检测项目表
create table Test_Item (test_item_id number(5),test_analyse_id number(5),test_item_name varchar2(20),analyse_value number(5,1),
                       pointer_condition_text varchar2(20),pointer_result varchar2(10),pointer_condition_text2 varchar2(100),
                       pointer_result2 varchar2(100),data_unit_name varchar2(20));
                       
insert into Test_Item values (1,1,'黏数',140,'143-136','优等品',null,null,'ml/g');
insert into Test_Item values (2,1,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (3,1,'吸油量',26,'>=23','优等品',null,null,'g');
insert into Test_Item values (4,1,'挥发物',0.5,'<=0.50','优等品',null,null,'%');
insert into Test_Item values (5,1,'250um筛孔筛余物',68,'>=23','优等品',null,null,'%');
insert into Test_Item values (6,2,'60um筛孔筛余物',1.2,'<=30','优等品',null,null,'%');
insert into Test_Item values (7,2,'杂质粒子数',1.2,'<=30','优等品',null,null,'个');
insert into Test_Item values (8,2,'白度',1.2,'<=30','优等品',null,null,'%');
insert into Test_Item values (9,2,'鱼眼',1.2,'<=30','优等品',null,null,'个/400cm');
insert into Test_Item values (10,2,'电导率',1.2,'<=30','优等品',null,null,'uS/(cm.g)');
insert into Test_Item values (11,2,'VCM残留量',1.2,'<=30','优等品',null,null,'ug/g');
insert into Test_Item values (12,2,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (13,3,'黏数',136,'143-136','合格品',null,null,'ml/g');
insert into Test_Item values (14,3,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (15,3,'吸油量',70,'>=23','一等品',null,null,'g');
insert into Test_Item values (16,3,'挥发物',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (17,3,'250um筛孔筛余物',78,'>=23','优等品',null,null,'%');
insert into Test_Item values (18,4,'60um筛孔筛余物',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (19,4,'杂质粒子数',1.2,'<=30','一等品',null,null,'个');
insert into Test_Item values (20,4,'白度烷',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (22,4,'鱼眼',1.2,'<=30','一等品',null,null,'个/400cm');
insert into Test_Item values (23,4,'电导率',1.2,'<=30','一等品',null,null,'uS/(cm.g)');
insert into Test_Item values (24,4,'VCM残留量',1.2,'<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','医用级','ug/g');
insert into Test_Item values (24,4,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (25,5,'黏数',136,'143-136','合格品',null,null,'ml/g');
insert into Test_Item values (26,5,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (27,5,'吸油量',70,'>=23','一等品',null,null,'g');
insert into Test_Item values (28,5,'挥发物',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (29,5,'250um筛孔筛余物',78,'>=23','优等品',null,null,'%');
insert into Test_Item values (30,6,'60um筛孔筛余物',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (31,6,'杂质粒子数',1.2,'<=30','一等品',null,null,'个');
insert into Test_Item values (32,6,'白度烷',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (33,6,'鱼眼',1.2,'<=30','一等品',null,null,'个/400cm');
insert into Test_Item values (34,6,'电导率',1.2,'<=30','一等品',null,null,'uS/(cm.g)');
insert into Test_Item values (35,6,'VCM残留量',1.2,'<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','食品级','ug/g');
insert into Test_Item values (36,6,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (37,7,'黏数',136,'143-136','合格品',null,null,'ml/g');
insert into Test_Item values (38,7,'表观密度',0.5,'>=0.40','优等品',null,null,'g/ml');
insert into Test_Item values (39,7,'吸油量',70,'>=23','一等品',null,null,'g');
insert into Test_Item values (40,7,'挥发物',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (41,7,'250um筛孔筛余物',78,'>=23','优等品',null,null,'%');
insert into Test_Item values (42,8,'60um筛孔筛余物',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (43,8,'杂质粒子数',1.2,'<=30','一等品',null,null,'个');
insert into Test_Item values (44,8,'白度烷',1.2,'<=30','一等品',null,null,'%');
insert into Test_Item values (45,8,'鱼眼',1.2,'<=30','一等品',null,null,'个/400cm');
insert into Test_Item values (46,8,'电导率',1.2,'<=30','一等品',null,null,'uS/(cm.g)');
insert into Test_Item values (47,8,'VCM残留量',1.2,'<=30','一等品','VCM残留量<=5,1,1-二氯乙烷<=150','食品级','ug/g');
insert into Test_Item values (48,8,'1,1-二氯乙烷',0.5,null,null,null,null,'mg/Kg');

insert into Test_Item values (49,97,'含量',789,'>=99','一等品',null,null,'%');

insert into Test_Item values (50,10,'外观',0.4,'>=0.40','合格品',null,null,null);
insert into Test_Item values (51,10,'次氯酸钠含量',23,'>=23','合格品',null,null,'%');
insert into Test_Item values (52,10,'游离碱含量',0.5,'<=0.50','合格品',null,null,'%');
insert into Test_Item values (53,10,'铁',1.2,'>=23','合格品',null,null,null);
commit; 

#22


表里的ID全是guid,没有规则的。

#23


纠结,继续等待。

#24


把帖子移到Oracle版块吧,与其在这不专一的地方等待,不如移到对口的版块,白天肯定有人愿意写的

#25


guid和数字序列差别大了,容易误导哦!是这样的吧:C25238FF54FF510EE040010A1AD751F9
引用 22 楼  的回复:
表里的ID全是guid,没有规则的。

#26


引用 25 楼  的回复:
guid和数字序列差别大了,容易误导哦!是这样的吧:C25238FF54FF510EE040010A1AD751F9
引用 22 楼  的回复:
表里的ID全是guid,没有规则的。
类似这样吧,反正是没有规则的。

#27


引用 23 楼  的回复:
纠结,继续等待。



哪有这么效率搞定的哇  【紧急求助】求一存储过程!!!!!!!!!!!!!

貌似后面的 平均只能单独列出来才行哇 

#28


引用 27 楼  的回复:
引用 23 楼  的回复:

纠结,继续等待。



哪有这么效率搞定的哇 

貌似后面的 平均只能单独列出来才行哇
,不管sql运行几次,只要能出这种结果就行了。
我直接和头说不会弄。他竟然还让我弄弄看。
无语。什么都往我这里丢。

#29


更正下  上面脚本 表3的49行  test_analyse_id为9  写成97了 

我说怎么查了半天 木看到gb/890后面有对应数据呢  【紧急求助】求一存储过程!!!!!!!!!!!!!

#30


累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber 


这里的producenumber 怎么取? 是1月份平均的producenumber还是?

#31


==哈,我问到了就回。

#32


累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber  

这里的producenumber是每个月份所有producenumber上加。

#33



select decode(lag(分析标准) over(order by 样品名称,分析标准),分析标准,'''''',分析标准) 分析标准,
       decode(lag(样品名称) over(order by 样品名称,分析标准),样品名称,'''''',样品名称) 样品名称,
       指标名称,
       单位,
       指标值,
       本月最高,
       本月最低,
       本月平均,
       (select round(sum(p_val*a_pro)/sum(s_pro),2) 
        from
        (
          select round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val,
                 round(avg(t1.producenumber),2) a_pro,
                 sum(t1.producenumber) s_pro
          from  test_sample t1,
                test_analyse t2,
                test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 t1.sampling_date < sysdate
          group by to_char(t1.sampling_date,'yyyy-mm')
         )
         ) 累计平均
from
(
    select b.analyse_method_name 分析标准,
           a.sample_name||a.pointer_result 样品名称,
           c.test_item_name 指标名称,
           c.data_unit_name 单位,
           c.pointer_condition_text 指标值,
           d.m_val 本月最高,
           d.n_val 本月最低,
           p_val 本月平均
    from test_analyse b 
         left join test_sample a on a.test_sample_id=b.test_sample_id
         left join test_item c on b.test_analyse_id=c.test_analyse_id,
         (select max(t3.analyse_value) m_val,
                 min(t3.analyse_value) n_val,
                 round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val
          from   test_sample t1,
                 test_analyse t2,
                 test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 to_char(t1.sampling_date,'yyyy-mm') = '2012-05'
          ) d
    where to_char(a.sampling_date,'yyyy-mm') = '2012-05'  --to_char(sysdate,'yyyy-mm')



    分析标准  样品名称   指标名称    单位    指标值  本月最高 本月最低 本月平均 累计平均
-----------------------------------------------------------------------------------------------
1 gb/12 SG3合格品 250um筛孔筛余物 % >=23 789 0.4 33.5 1.09
2 '' '' 吸油量 g >=23 789 0.4 33.5 1.09
3 '' '' 挥发物 % <=0.50 789 0.4 33.5 1.09
4 '' '' 黏数 ml/g 143-136 789 0.4 33.5 1.09
5 '' '' 表观密度 g/ml >=0.40 789 0.4 33.5 1.09
6 gb/34 '' 60um筛孔筛余物 % <=30 789 0.4 33.5 1.09
7 '' '' 杂质粒子数 个 <=30 789 0.4 33.5 1.09
8 '' '' 白度烷 % <=30 789 0.4 33.5 1.09
9 '' '' 鱼眼 个/400cm <=30 789 0.4 33.5 1.09
10 '' '' 1,1-二氯乙烷 mg/Kg 789 0.4 33.5 1.09
11 '' '' VCM残留量 ug/g <=30 789 0.4 33.5 1.09
12 '' '' 电导率 uS/(cm.g) <=30 789 0.4 33.5 1.09
13 gb/12 SG3一等品 250um筛孔筛余物 % >=23 789 0.4 33.5 1.09
14 '' '' 黏数 ml/g 143-136 789 0.4 33.5 1.09
15 '' '' 表观密度 g/ml >=0.40 789 0.4 33.5 1.09
16 '' '' 挥发物 % <=0.50 789 0.4 33.5 1.09
17 '' '' 吸油量 g >=23 789 0.4 33.5 1.09
18 gb/34 '' 杂质粒子数 个 <=30 789 0.4 33.5 1.09
19 '' '' 白度烷 % <=30 789 0.4 33.5 1.09
20 '' '' 鱼眼 个/400cm <=30 789 0.4 33.5 1.09
21 '' '' 电导率 uS/(cm.g) <=30 789 0.4 33.5 1.09
22 '' '' VCM残留量 ug/g <=30 789 0.4 33.5 1.09
23 '' '' 1,1-二氯乙烷 mg/Kg 789 0.4 33.5 1.09
24 '' '' 60um筛孔筛余物 % <=30 789 0.4 33.5 1.09
25 gb/12 SG3优等品 250um筛孔筛余物 % >=23 789 0.4 33.5 1.09
26 '' '' 挥发物 % <=0.50 789 0.4 33.5 1.09
27 '' '' 吸油量 g >=23 789 0.4 33.5 1.09
28 '' '' 黏数 ml/g 143-136 789 0.4 33.5 1.09
29 '' '' 表观密度 g/ml >=0.40 789 0.4 33.5 1.09
30 gb/34 '' VCM残留量 ug/g <=30 789 0.4 33.5 1.09
31 '' '' 电导率 uS/(cm.g) <=30 789 0.4 33.5 1.09
32 '' '' 鱼眼 个/400cm <=30 789 0.4 33.5 1.09
33 '' '' 白度 % <=30 789 0.4 33.5 1.09
34 '' '' 杂质粒子数 个 <=30 789 0.4 33.5 1.09
35 '' '' 1,1-二氯乙烷 mg/Kg 789 0.4 33.5 1.09
36 '' '' 60um筛孔筛余物 % <=30 789 0.4 33.5 1.09
37 gb/4389398 次氯酸钠 外观 >=0.40 789 0.4 33.5 1.09
38 '' '' 次氯酸钠含量 % >=23 789 0.4 33.5 1.09
39 '' '' 游离碱含量 % <=0.50 789 0.4 33.5 1.09
40 '' '' 铁 >=23 789 0.4 33.5 1.09
41 gb/890 氢气一等品 含量 % >=99 789 0.4 33.5 1.09

#34


引用 32 楼  的回复:
累计平均:
1月本月平均*producenumber + 2月本月平均*producenumber .....+5月本月平均/
1月所有的producenumber+2月所有的producenumber...+5月所有的producenumber  

这里的producenumber是每个月份所有producenumber上加。


那累计平均要改下咯


       (select round(sum(p_val*s_pro)/sum(s_pro),2) 
        from
        (
          select round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val,
                 sum(t1.producenumber) s_pro
          from  test_sample t1,
                test_analyse t2,
                test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 t1.sampling_date < sysdate and
                 to_char(t1.sampling_date,'yyyy-mm') = to_char(sysdate,'yyyy-mm')
          group by to_char(t1.sampling_date,'yyyy-mm')
         )
         ) 累计平均


 吃饭去咯 下午来整吧 

#35


看到好多关键字都不懂,囧,这样都不晓得正确不正确。
验证还要花大把时间。
sample_name + pointre_result2这个数据还木出来呢?

#36


添加了sample_name + pointre_result2   又花了十多分钟排版 不知道格式可以没?

select decode(lag(分析标准) over(order by 样品名称,分析标准),分析标准,'''''',分析标准) 分析标准,
       decode(lag(样品名称) over(order by 样品名称,分析标准),样品名称,'''''',样品名称) 样品名称,
       指标名称,
       单位,
       指标值,
       本月最高,
       本月最低,
       本月平均,
       (select round(sum(p_val*s_pro)/sum(s_pro),2) 
        from
        (
          select round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val,
                 sum(t1.producenumber) s_pro
          from  test_sample t1,
                test_analyse t2,
                test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 t1.sampling_date < sysdate and
                 to_char(t1.sampling_date,'yyyy-mm') = to_char(sysdate,'yyyy-mm')
          group by to_char(t1.sampling_date,'yyyy-mm')
         )
         ) 累计平均
from
(
    select b.analyse_method_name 分析标准,
           e.sp 样品名称,
           c.test_item_name 指标名称,
           c.data_unit_name 单位,
           c.pointer_condition_text 指标值,
           d.m_val 本月最高,
           d.n_val 本月最低,
           p_val 本月平均
    from test_analyse b 
         left join test_sample a on a.test_sample_id=b.test_sample_id
         left join test_item c on b.test_analyse_id=c.test_analyse_id,
         (select test_sample_id,sample_name||pointer_result sp
           from  test_sample 
           union all
           select  test_sample_id,sample_name||pointer_result2
           from  test_sample
           where pointer_result2 is not null) e,
         (select max(t3.analyse_value) m_val,
                 min(t3.analyse_value) n_val,
                 round(sum(t1.producenumber*t3.analyse_value)/sum(t1.producenumber),2) p_val
          from   test_sample t1,
                 test_analyse t2,
                 test_item t3
          where  t1.test_sample_id=t2.test_sample_id and 
                 t2.test_analyse_id=t3.test_analyse_id and
                 to_char(t1.sampling_date,'yyyy-mm') = '2012-05'
          ) d
    where a.test_sample_id=e.test_sample_id and 
          to_char(a.sampling_date,'yyyy-mm') = '2012-05'  --to_char(sysdate,'yyyy-mm')



分析标准  样品名称      指标名称        单位   指标值  本月最高 本月最低 本月平均 累计平均
-----------------------------------------------------------------------------------------------
gb/12 SG3合格品 250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
gb/34 ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         鱼眼         个/400cm <=30 789 0.4 33.5 24.39
'' ''         白度烷         % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
gb/12 SG3食品级 表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
gb/34 ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         白度烷         % <=30 789 0.4 33.5 24.39
'' ''         电导率        uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         鱼眼        个/400cm <=30 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
gb/12 SG3一等品 吸油量         g >=23 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
gb/34 ''         白度烷         % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         鱼眼        个/400cm <=30 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
gb/12 SG3医用级 挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
'' ''         黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
gb/34 ''         鱼眼 个/400cm <=30 789 0.4 33.5 24.39
'' ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         白度烷         % <=30 789 0.4 33.5 24.39
gb/12 SG3优等品 黏数         ml/g 143-136 789 0.4 33.5 24.39
'' ''         表观密度 g/ml >=0.40 789 0.4 33.5 24.39
'' ''         吸油量         g >=23 789 0.4 33.5 24.39
'' ''         挥发物         % <=0.50 789 0.4 33.5 24.39
'' ''         250um筛孔筛余物 % >=23 789 0.4 33.5 24.39
gb/34 ''         60um筛孔筛余物 % <=30 789 0.4 33.5 24.39
'' ''         杂质粒子数 个 <=30 789 0.4 33.5 24.39
'' ''         白度         % <=30 789 0.4 33.5 24.39
'' ''         鱼眼         个/400cm<=30 789 0.4 33.5 24.39
'' ''         电导率         uS/(cm.g)<=30 789 0.4 33.5 24.39
'' ''         VCM残留量 ug/g <=30 789 0.4 33.5 24.39
'' ''         1,1-二氯乙烷 mg/Kg 789 0.4 33.5 24.39
gb/4389398         次氯酸钠 外观 >=0.40 789 0.4 33.5 24.39
'' ''         次氯酸钠含量 % >=23 789 0.4 33.5 24.39
'' ''         游离碱含量 % <=0.50 789 0.4 33.5 24.39
'' ''         铁         >=23 789 0.4 33.5 24.39
gb/890 氢气一等品 含量         % >=99 789 0.4 33.5 24.39

#37


【紧急求助】求一存储过程!!!!!!!!!!!!!还素没排好 第一排有值的 第二排没后推    怎么看着像W型呢 【紧急求助】求一存储过程!!!!!!!!!!!!! 

#38


发现有些地方逻辑需要改的。
sample_name + pointer_result2这个取得数据是有规则的。
1:只取pointer_result2不为空的值,并且pointer_result2="食品级"的数据。然后唯一例外的就是根据test_item表里的pointer_condition2值不空,并且包含有test__item_name,例如VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150",它包含两个test_item_name
1个是VCM残留量,第2个是1,1-二氯乙烷
所以这里它只取两个test_item_name
就是我上面给出的

分析标准(analyse_method_name)        样品名称                   指标名称(test_item_name)    单位(data_unit_name)     指标值        本月最高    本月最低    本月平均   累计平均
 gb/34                          SG3食品级                  VCM残留量                   ml/g                    <=1            56            55         455       45                                                                     
     ,,                                  ,,                   1,1-二氯乙烷                 g/ml                    <=150          56            55         455       45                                                                                          


然后指标值就是根据VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150"
得到VCM残留量为“<=1”,1,1-二氯乙烷为"<=150"
2:然后它的本月最高、本月最低、本月平均、累计平均
需要加上SG3这个test_sample_name的pointer_result为“一等品”“优等品”“食品级”“医用级”这些数据来求最高、最低、平均、和累计平均。。

#39


介么多数据,表示会看的头晕,帮顶 【紧急求助】求一存储过程!!!!!!!!!!!!!

#40


么多数据,表示会看的头晕,帮顶

#41


其他都可以了  就这个

然后唯一例外的就是根据test_item表里的pointer_condition2值不空,并且包含有test__item_name,例如VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150",它包含两个test_item_name
1个是VCM残留量,第2个是1,1-二氯乙烷


test__item_name里面也存在逗号  pointer_condition2也都是逗号分开 ,我怎么来判断  刚好包涵了几个test__item_name呢 ,用ondition2匹配所有的name 看包涵几个name? 即使完成了 貌似提取字段也是个问题哇?

#42


引用 41 楼  的回复:
其他都可以了  就这个

然后唯一例外的就是根据test_item表里的pointer_condition2值不空,并且包含有test__item_name,例如VCM残留量这个数据的pointer_condition2的值为"VCM 残留量<=1,1,1-二氯乙烷<=150",它包含两个test_item_name
1个是VCM残留量,第2个是1,1-二氯乙烷


test__it……
只能通过test_item表里的pointer_condition2里面包含几个test_item字段来了,,

#43


这样吧,我让pointer_condition2里面不用逗号分隔,而是用两个"$$",例如这样
VCM 残留量<=1$$1,1-二氯乙烷<=150.
这样的话,好处理不

#44


然后这样的话应该好提取了,
分隔完之后就是
两个test_item_name
VCM 残留量<=1、1,1-二氯乙烷<=150
然后再把test_item_name清空赋值到字段 指标值里。
这样的话就出这个结果了。

分析标准(analyse_method_name)        样品名称                   指标名称(test_item_name)    单位(data_unit_name)     指标值        本月最高    本月最低    本月平均   累计平均
 gb/34                          SG3食品级                  VCM残留量                   ml/g                    <=1            56            55         455       45                                                                     
     ,,                                  ,,                   1,1-二氯乙烷                 g/ml                    <=150          56            55         455       45   

#45


这个计算还不得把人算哭了啊?

#46


引用 45 楼  的回复:
这个计算还不得把人算哭了啊?
婶婶,木办法啊,
我都要哭了。

#47


我在想  为什么  不直接分开写在对应的name 里面呢   要几个联合起来写在ondition2里面 


这个程序 插入值的时候 是这吗插入的 那时候就可以判断  分割字符在对应name的ondition2里面呢

也可以为统计节省一点时间了

#48


引用 46 楼  的回复:
引用 45 楼  的回复:

这个计算还不得把人算哭了啊?
婶婶,木办法啊,
我都要哭了。


  好吧、偶也要哭了   【紧急求助】求一存储过程!!!!!!!!!!!!!

#49


引用 47 楼  的回复:
我在想  为什么  不直接分开写在对应的name 里面呢   要几个联合起来写在ondition2里面 


这个程序 插入值的时候 是这吗插入的 那时候就可以判断  分割字符在对应name的ondition2里面呢

也可以为统计节省一点时间了
最高值,最低值的数据不对,
你取的是test_item表里的最高,最低,而不是每个test_item_name的最高最低,,
不知道平均和累计平均是不是也算错了,,

#50


引用 47 楼  的回复:
我在想  为什么  不直接分开写在对应的name 里面呢   要几个联合起来写在ondition2里面 


这个程序 插入值的时候 是这吗插入的 那时候就可以判断  分割字符在对应name的ondition2里面呢

也可以为统计节省一点时间了
,特么的,说起这个就来气。
程序是别人写的,
他们让我来写这个统计。
我肏。
说了我不会,还硬让我先写写。