質問編集履歴

2

SQLの変更と、当該SQLの意図について編集・追記

2022/05/13 13:11

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -3,10 +3,12 @@
3
3
 
4
4
  下記はDBにアクセスするDAOクラスに記載したSQLで、
5
5
  ここでやりたいことは、元となるデータが記載されたテーブル「budget_tracker_table」からselectで抽出/集計した結果を、もう一つのテーブル「budget_tracker_table_alias」にinsertする事です。
6
+ 下記では、SQLの内容を少し変え、Group by product_typeとしました。
7
+ 意図としては、ユーザ入力値の日付(from)、日付(to)、店名から、指定された日付の間で、指定の店名で購入した商品のリストを取得し、さらにそのリストの中で、商品種類(product_type)を洗い出し、どういった商品の種類(電化製品、食品など)が購入されたかをパーセンテージで表示するSQLを組みたいと思っています。
6
8
 
7
9
  BudgetTrackerAliasDao.java
8
10
  ```ここに言語を入力
9
- @Query("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%') as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%' group by store_name;")
11
+ @Query("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(product_type) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%') as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%' group by product_type;")
10
12
  void insert(String date1, String date2, String storeName);
11
13
  ```
12
14
 
@@ -29,15 +31,14 @@
29
31
  ```
30
32
  上記状態で、下記SQLを実行します。
31
33
  ```ここに言語を入力
32
- insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%') as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%' group by store_name;
34
+ insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(product_type) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%') as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%' group by product_type;
33
35
  ```
34
36
  budget_tracker_table_aliasには下記のデータが入りました。
35
- ただ、SQLではLIKE検索でamazonとしているので、本来であればID「18」が入っているはずだったのですが、
37
+ ただ、SQLではLIKE検索でamazonとしているので、本来であればID「17「23」が入っているはずだったのですが、
36
- 当該レコードのみが入っていませんした。
38
+ 当該レコードのみが入っていませんした。
37
39
  ```ここに言語を入力
38
- 53 2022-4-26 Amazon echo show 2222 gadget 159999 25.0
39
- 54 2022-4-22 Amazon echo show 333 gadget 30000 25.0
40
- 55 2022-4-23 amazon renpho smart nawatobi gadget 3000 50.0
40
+ 146 2022-4-23 Amazon renpho smart nawatobi gadget 3000 75.0
41
+ 147 2022-4-22 amazon tiger mask toy 1500 25.0
41
42
  ```
42
43
 
43
44
  当該事象解決の為、ご助力頂けますと幸いです。

1

SQLを全体的に変更

2022/05/13 05:53

投稿

Yakusugi
Yakusugi

スコア123

test CHANGED
File without changes
test CHANGED
@@ -6,14 +6,14 @@
6
6
 
7
7
  BudgetTrackerAliasDao.java
8
8
  ```ここに言語を入力
9
- @Query("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2) as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 group by store_name LIKE '%' || :storeName || '%';")
9
+ @Query("insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%') as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%' group by store_name;")
10
10
  void insert(String date1, String date2, String storeName);
11
11
  ```
12
12
 
13
13
  想定では、上記SQLを実行すると、ユーザ入力値のdate1(始まりの日付)、date2(終わりの日付)、storeName(店名)を元に、
14
14
  budget_tracker_tableからデータを抽出/集計し、その結果をbudget_tracker_table_aliasにinsertするはずでした。
15
15
  また、日付の間隔が大きければ、複数のデータが入るはずだったのですが、実際に実行してみると、
16
- budget_tracker_table_aliasにinsertされた想定よりも少なく、またstoreNameで指定していないデータで入っていました。
16
+ budget_tracker_table_aliasにinsertされたレコード数は想定よりも少ない結果とました。
17
17
 
18
18
  抽出元となるbudget_tracker_tableのデータは下記のようになっています。
19
19
 
@@ -24,20 +24,23 @@
24
24
  19 2022-4-22 Google store pixelbook gadget 45099
25
25
  20 2022-4-22 Google store echo show gadget 1500
26
26
  23 2022-4-26 Amazon echo show 2222 gadget 159999
27
+ 24 2022-4-23 amazon renpho smart nawatobi gadget 3000
28
+ 25 2022-4-26 Google store pixel 6a gadget 54000
27
29
  ```
28
30
  上記状態で、下記SQLを実行します。
29
31
  ```ここに言語を入力
30
- insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= '2022-4-22' and date <= '2022-4-23') as 'Percentage' from budget_tracker_table where date >= '2022-4-22' and date <= '2022-4-23' group by store_name LIKE '%' || 'amazon' || '%';
32
+ insert into budget_tracker_table_alias (date_alias, store_name_alias, product_name_alias, product_type_alias, price_alias, product_type_percentage) select date, store_name, product_name, product_type, price, count(*) * 100.0 / (select count(*) from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%') as 'Percentage' from budget_tracker_table where date >= :date1 and date <= :date2 and store_name LIKE '%' || :storeName || '%' group by store_name;
31
33
  ```
32
34
  budget_tracker_table_aliasには下記のデータが入りました。
33
- ただ、SQLではLIKE検索でamazonとしているので、本来であればID「17」「18」が入っているはずだったのですが、
35
+ ただ、SQLではLIKE検索でamazonとしているので、本来であればID「18」が入っているはずだったのですが、
34
- 実際には店名が「Google store」もの含まれおり、原因が突き止められておりません。
36
+ 当該レコード入っませんdした
35
37
  ```ここに言語を入力
38
+ 53 2022-4-26 Amazon echo show 2222 gadget 159999 25.0
36
- 30 2022-4-22 Google store echo show gadget 1500 50.0
39
+ 54 2022-4-22 Amazon echo show 333 gadget 30000 25.0
37
- 31 2022-4-22 amazon tiger mask toy 1500 50.0
40
+ 55 2022-4-23 amazon renpho smart nawatobi gadget 3000 50.0
38
41
  ```
39
- 個人的には、SQL内の店名(store_name)の指定で間違っているのではないかと推測はしているのですが、
42
+
40
- ご助力頂けますと幸いです。
43
+ 当該事象解決の為、ご助力頂けますと幸いです。
41
44
 
42
45
 
43
46