(原) Oracle按连续号对票据分组
数据内容:
开始 结束
1 61001 61011
2 61012 61262
3 61263 61395
4 61396 61660
5 61661 61975
6 61976 62000
7 89001 89164
8 89165 89306
9 89307 89573
10 89574 89775
11 89776 90000
12 102001 102044
13 102045 102255
14 102256 102437
15 102438 102603
16 102604 102850
17 102851 103000
18 105001 105073
19 105074 105322
20 105323 105420
21 105421 105595
22 105596 105707
23 105708 105842
24 105843 105988
需要得到的结果:
61263~62000;
89001~90000;
102001~103000;
105001~105988;
Function:
1CREATE OR REPLACE FUNCTION FUNC_GETRECKON(P_SELLID IN VARCHAR2,
2 p_DateStart IN VARCHAR2,
3 p_DateEnd IN VARCHAR2)
4 RETURN VARCHAR2 IS
5 RESULT VARCHAR2(256);
6
7 /*
8 名称:FUNC_GETRECKON
9 创建时间:2007.12.21
10 创建人:yugx
11
12 作用:按连续号对票据分组。
13 */
14
15 MinNumber INT;
16 MaxNumber INT;
17
18 M_SELLID VARCHAR2(256);
19
20 CURSOR CUR_GETRECKON IS
21 SELECT TO_NUMBER(TM_PEOPLERECKON.STARTTICKETNUMBER) STARTTICKETNUMBER,
22 TO_NUMBER(TM_PEOPLERECKON.ENDTICKETNUMBER) ENDTICKETNUMBER
23 FROM TM_PEOPLERECKON
24 WHERE TM_PEOPLERECKON.SALESID = M_SELLID
25 AND TM_PEOPLERECKON.STARTTICKETNUMBER IS NOT NULL
26 AND TM_PEOPLERECKON.Onlinetime> to_date(p_DateStart,'yyyy-MM-dd hh24:mi:ss')
27 AND TM_PEOPLERECKON.Accounttime <= to_date(p_DateEnd,'yyyy-MM-dd hh24:mi:ss')
28 ORDER BY TO_NUMBER(TM_PEOPLERECKON.STARTTICKETNUMBER);
29 CURROW CUR_GETRECKON%ROWTYPE;
30
31BEGIN
32
33 MinNumber:=0;
34 M_SELLID :=P_SELLID;
35 RESULT :='';
36
37 SELECT MAX(TM_PEOPLERECKON.ENDTICKETNUMBER) INTO MaxNumber
38 FROM TM_PEOPLERECKON
39 WHERE TM_PEOPLERECKON.SALESID = M_SELLID
40 AND TM_PEOPLERECKON.Onlinetime> to_date(p_DateStart,'yyyy-MM-dd hh24:mi:ss')
41 AND TM_PEOPLERECKON.Accounttime <= to_date(p_DateEnd,'yyyy-MM-dd hh24:mi:ss');
42
43 OPEN CUR_GETRECKON;
44 FETCH CUR_GETRECKON INTO CURROW;
45WHILE (CUR_GETRECKON%FOUND) LOOP
46 IF MinNumber <> CURROW.STARTTICKETNUMBER-1
47 THEN
48 IF MinNumber <>0
49 THEN
50 RESULT := RESULT|| '~' || to_char(MinNumber) || '; ' || to_char(CURROW.STARTTICKETNUMBER);
51 ELSE
52 RESULT := RESULT|| to_char(CURROW.STARTTICKETNUMBER);
53 END IF;
54 MinNumber:=CURROW.ENDTICKETNUMBER;
55 ELSE
56 MinNumber:=CURROW.ENDTICKETNUMBER;
57 IF MinNumber = MaxNumber
58 THEN
59 RESULT := RESULT|| '~' || to_char(MaxNumber);
60 END IF;
61 END IF;
62 FETCH CUR_GETRECKON INTO CURROW;
63END LOOP;
64
65RETURN(RESULT);
66
67EXCEPTION
68WHEN OTHERS THEN
69 LFGUSER.PROC_LOG_ORA_EXCEPTION(P_ERR_NUM => SQLCODE, P_ERR_MSG => SQLERRM, P_ERR_SOURCE => 'FUNC_GETRECKON');
70 RETURN '空';
71END FUNC_GETRECKON;
72
2 p_DateStart IN VARCHAR2,
3 p_DateEnd IN VARCHAR2)
4 RETURN VARCHAR2 IS
5 RESULT VARCHAR2(256);
6
7 /*
8 名称:FUNC_GETRECKON
9 创建时间:2007.12.21
10 创建人:yugx
11
12 作用:按连续号对票据分组。
13 */
14
15 MinNumber INT;
16 MaxNumber INT;
17
18 M_SELLID VARCHAR2(256);
19
20 CURSOR CUR_GETRECKON IS
21 SELECT TO_NUMBER(TM_PEOPLERECKON.STARTTICKETNUMBER) STARTTICKETNUMBER,
22 TO_NUMBER(TM_PEOPLERECKON.ENDTICKETNUMBER) ENDTICKETNUMBER
23 FROM TM_PEOPLERECKON
24 WHERE TM_PEOPLERECKON.SALESID = M_SELLID
25 AND TM_PEOPLERECKON.STARTTICKETNUMBER IS NOT NULL
26 AND TM_PEOPLERECKON.Onlinetime> to_date(p_DateStart,'yyyy-MM-dd hh24:mi:ss')
27 AND TM_PEOPLERECKON.Accounttime <= to_date(p_DateEnd,'yyyy-MM-dd hh24:mi:ss')
28 ORDER BY TO_NUMBER(TM_PEOPLERECKON.STARTTICKETNUMBER);
29 CURROW CUR_GETRECKON%ROWTYPE;
30
31BEGIN
32
33 MinNumber:=0;
34 M_SELLID :=P_SELLID;
35 RESULT :='';
36
37 SELECT MAX(TM_PEOPLERECKON.ENDTICKETNUMBER) INTO MaxNumber
38 FROM TM_PEOPLERECKON
39 WHERE TM_PEOPLERECKON.SALESID = M_SELLID
40 AND TM_PEOPLERECKON.Onlinetime> to_date(p_DateStart,'yyyy-MM-dd hh24:mi:ss')
41 AND TM_PEOPLERECKON.Accounttime <= to_date(p_DateEnd,'yyyy-MM-dd hh24:mi:ss');
42
43 OPEN CUR_GETRECKON;
44 FETCH CUR_GETRECKON INTO CURROW;
45WHILE (CUR_GETRECKON%FOUND) LOOP
46 IF MinNumber <> CURROW.STARTTICKETNUMBER-1
47 THEN
48 IF MinNumber <>0
49 THEN
50 RESULT := RESULT|| '~' || to_char(MinNumber) || '; ' || to_char(CURROW.STARTTICKETNUMBER);
51 ELSE
52 RESULT := RESULT|| to_char(CURROW.STARTTICKETNUMBER);
53 END IF;
54 MinNumber:=CURROW.ENDTICKETNUMBER;
55 ELSE
56 MinNumber:=CURROW.ENDTICKETNUMBER;
57 IF MinNumber = MaxNumber
58 THEN
59 RESULT := RESULT|| '~' || to_char(MaxNumber);
60 END IF;
61 END IF;
62 FETCH CUR_GETRECKON INTO CURROW;
63END LOOP;
64
65RETURN(RESULT);
66
67EXCEPTION
68WHEN OTHERS THEN
69 LFGUSER.PROC_LOG_ORA_EXCEPTION(P_ERR_NUM => SQLCODE, P_ERR_MSG => SQLERRM, P_ERR_SOURCE => 'FUNC_GETRECKON');
70 RETURN '空';
71END FUNC_GETRECKON;
72