teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

3

書式を修正しました。

2021/01/08 04:46

投稿

Qingxuan
Qingxuan

スコア7

title CHANGED
File without changes
body CHANGED
@@ -45,23 +45,23 @@
45
45
  CREATE OR REPLACE FUNCTION sumfunction
46
46
  ( f_col1 IN VARCHAR2)
47
47
  RETURN NUMBER
48
- IS
48
+ IS
49
- total NUMBER;
49
+ total NUMBER;
50
- BEGIN
50
+ BEGIN
51
- SELECT SUM (col3) INTO total from test6 group by col1 having col1 =f_col1 ;
51
+ SELECT SUM (col3) INTO total from test6 group by col1 having col1 =f_col1 ;
52
- RETURN total;
52
+ RETURN total;
53
53
  END sumfunction;
54
54
  /
55
55
 
56
56
  --PL/SQL文
57
57
  SET SERVEROUTPUT ON
58
58
  BEGIN
59
- FOR v_table IN
59
+ FOR v_table IN
60
- (SELECT * from test6)
60
+ (SELECT * from test6)
61
61
  LOOP
62
- DBMS_OUTPUT.PUT_LINE(v_table.col1||' '|| v_table.col2||
62
+ DBMS_OUTPUT.PUT_LINE(v_table.col1||' '|| v_table.col2||
63
63
             ' '||v_table.col3);
64
- DBMS_OUTPUT.PUT_LINE('合計 '||sumfunction(v_table.col1));
64
+ DBMS_OUTPUT.PUT_LINE('合計 '||sumfunction(v_table.col1));
65
65
  END LOOP;
66
66
  END;
67
67
  /
@@ -72,18 +72,19 @@
72
72
 
73
73
  ### 試したこと
74
74
  試しにネストしてみたのですが、イメージと結構違っています。
75
+ ```ここに言語を入力
75
76
  FOR v_table IN
76
- (SELECT * from test6)
77
+ (SELECT * from test6)
77
78
  LOOP
78
- FOR v_cnt IN 1..2 LOOP
79
+ FOR v_cnt IN 1..2 LOOP
79
- DBMS_OUTPUT.PUT_LINE(v_table.col1||' '|| v_table.col2||
80
+ DBMS_OUTPUT.PUT_LINE(v_table.col1||' '|| v_table.col2||
80
- ' '||v_table.col3);
81
+ ' '||v_table.col3);
81
- END LOOP;
82
+ END LOOP;
82
- DBMS_OUTPUT.PUT_LINE('合計 '||sumfunction(v_table.col1));
83
+ DBMS_OUTPUT.PUT_LINE('合計 '||sumfunction(v_table.col1));
83
84
  END LOOP;
84
- END;
85
- /
86
85
 
86
+
87
+ ```
87
88
  ループをネストか、それともブロックをネストか、ぜひ助言をお願いします。
88
89
 
89
90
 

2

col3 をNUMBER型に修正しました

2021/01/08 04:45

投稿

Qingxuan
Qingxuan

スコア7

title CHANGED
File without changes
body CHANGED
@@ -31,15 +31,15 @@
31
31
 
32
32
  ```
33
33
  --create table
34
- CREATE TABLE test6 (col1 VARCHAR2(10),col2 VARCHAR2(10), col3 VARCHAR2(10));
34
+ CREATE TABLE test6 (col1 VARCHAR2(10),col2 VARCHAR2(10), col3 NUMBER);
35
35
 
36
- INSERT INTO test6 VALUES('AAAAA','ああああ','1000');
36
+ INSERT INTO test6 VALUES('AAAAA','ああああ',1000);
37
- INSERT INTO test6 VALUES('AAAAA','いいいい','2000');
37
+ INSERT INTO test6 VALUES('AAAAA','いいいい',2000);
38
- INSERT INTO test6 VALUES('BBBBB','ああああ','1500');
38
+ INSERT INTO test6 VALUES('BBBBB','ああああ',1500);
39
- INSERT INTO test6 VALUES('BBBBB','いいいい','4000');
39
+ INSERT INTO test6 VALUES('BBBBB','いいいい',4000);
40
- INSERT INTO test6 VALUES('BBBBB','うううう','3000');
40
+ INSERT INTO test6 VALUES('BBBBB','うううう',3000);
41
- INSERT INTO test6 VALUES('CCCCC','ああああ','1400');
41
+ INSERT INTO test6 VALUES('CCCCC','ああああ',1400);
42
- INSERT INTO test6 VALUES('CCCCC','うううう','1500');
42
+ INSERT INTO test6 VALUES('CCCCC','うううう',1500);
43
43
 
44
44
  --合計を求めるFUNCTION
45
45
  CREATE OR REPLACE FUNCTION sumfunction

1

create tableのソースを追加しました

2021/01/08 04:41

投稿

Qingxuan
Qingxuan

スコア7

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,6 @@
1
1
  ### 前提・実現したいこと
2
2
  テーブルは以下の感じです。
3
+
3
4
  test6
4
5
  col1 col2 col3
5
6
  AAAAA ああああ 1000
@@ -29,6 +30,17 @@
29
30
  ### 該当のソースコード
30
31
 
31
32
  ```
33
+ --create table
34
+ CREATE TABLE test6 (col1 VARCHAR2(10),col2 VARCHAR2(10), col3 VARCHAR2(10));
35
+
36
+ INSERT INTO test6 VALUES('AAAAA','ああああ','1000');
37
+ INSERT INTO test6 VALUES('AAAAA','いいいい','2000');
38
+ INSERT INTO test6 VALUES('BBBBB','ああああ','1500');
39
+ INSERT INTO test6 VALUES('BBBBB','いいいい','4000');
40
+ INSERT INTO test6 VALUES('BBBBB','うううう','3000');
41
+ INSERT INTO test6 VALUES('CCCCC','ああああ','1400');
42
+ INSERT INTO test6 VALUES('CCCCC','うううう','1500');
43
+
32
44
  --合計を求めるFUNCTION
33
45
  CREATE OR REPLACE FUNCTION sumfunction
34
46
  ( f_col1 IN VARCHAR2)