DATE ANSWER COUNT
2018/01/01 〇 1
2018/01/01 × 1
2018/01/01 △ 2
2018/01/02 〇 1
2018/01/02 × 1
2018/01/02 △ 2
2018/01/03 × 1
2018/01/03 △ 1
2018/01/03 〇 2
2018/01/04 × 2
2018/01/04 〇 2
上記のようなレコードをDBから取得した後、
DATE 〇 × △
2018/01/01 1 1 2
2018/01/02 1 1 2
2018/01/03 2 1 2
2018/01/04 2 2
のような形で画面に表示させたいと考えていますが、
やり方がわかりません。
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答2件
0
SQL的にはこんな感じだね・・
SELECT DATE, MAX(IF(ANSWER='〇', COUNT, 0)) AS ANSWER_1, MAX(IF(ANSWER='△', COUNT, 0)) AS ANSWER_2, MAX(IF(ANSWER='×', COUNT, 0)) AS ANSWER_3 FROM TEST GROUP BY DATE
まあ DB連携になにつかってるかしらんしコードもあげてないから連携部分は割愛する
投稿2018/02/18 05:26
退会済みユーザー
総合スコア0
0
書いてみました。
・DBの代わりに配列を読んでいます。
・素のThymeleafを使ってtemplate.htmlからresult.htmlを生成しています。
[template.html]
HTML
1<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-3.dtd"> 2<html xmlns="http://www.w3.org/1999/xhtml" 3 xmlns:th="http://www.thymeleaf.org"> 4<head> 5<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6<title>thymeleafでの表作成</title> 7</head> 8<body> 9 <table border> 10 <tr> 11 <th>DATE</th> 12 <th>〇</th> 13 <th>×</th> 14 <th>△</th> 15 </tr> 16 <tr th:each="row: ${rows}" th:object="${row}"> 17 <td th:text="*{key}"/> 18 <td th:text="*{value.maru}"/> 19 <td th:text="*{value.batsu}"/> 20 <td th:text="*{value.sankaku}"/> 21 </tr> 22 </table> 23</body> 24</html> 25
[result.html]
HTML
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 2<html xmlns="http://www.w3.org/1999/xhtml"> 3<head> 4<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 5<title>thymeleafでの表作成</title> 6</head> 7<body> 8 <table border> 9 <tr> 10 <th>DATE</th> 11 <th>〇</th> 12 <th>×</th> 13 <th>△</th> 14 </tr> 15 <tr> 16 <td>2018/01/01</td> 17 <td>1</td> 18 <td>1</td> 19 <td>2</td> 20 </tr> 21 <tr> 22 <td>2018/01/02</td> 23 <td>1</td> 24 <td>1</td> 25 <td>2</td> 26 </tr> 27 <tr> 28 <td>2018/01/03</td> 29 <td>2</td> 30 <td>1</td> 31 <td>1</td> 32 </tr> 33 <tr> 34 <td>2018/01/04</td> 35 <td>2</td> 36 <td>2</td> 37 <td>0</td> 38 </tr> 39 </table> 40</body> 41</html>
java
1import java.io.FileWriter; 2import java.io.IOException; 3import java.util.HashMap; 4import java.util.Map; 5 6import org.thymeleaf.TemplateEngine; 7import org.thymeleaf.context.Context; 8import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver; 9 10public class Q113979 { 11 12 //データベースのかわり 13 static final Object[][] DB = { 14 {"2018/01/01", "〇", 1}, 15 {"2018/01/01", "×", 1}, 16 {"2018/01/01", "△", 2}, 17 {"2018/01/02", "〇", 1}, 18 {"2018/01/02", "×", 1}, 19 {"2018/01/02", "△", 2}, 20 {"2018/01/03", "×", 1}, 21 {"2018/01/03", "△", 1}, 22 {"2018/01/03", "〇", 2}, 23 {"2018/01/04", "×", 2}, 24 {"2018/01/04", "〇", 2}, 25 }; 26 27 public static void main(String[] args) { 28 //クラスパスにあるテンプレートを使用 29 ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver(); 30 //テンプレートはXHTML形式 31 resolver.setTemplateMode("XHTML"); 32 //テンプレートの拡張子は.html 33 resolver.setSuffix(".html"); 34 TemplateEngine engine = new TemplateEngine(); 35 engine.setTemplateResolver(resolver); 36 //行を集計するためのクラス 37 class Row { 38 public int maru; 39 public int batsu; 40 public int sankaku; 41 void count(String mark, int n) { 42 switch(mark) { 43 case "〇": maru += n; break; 44 case "×": batsu += n; break; 45 case "△": sankaku += n; break; 46 } 47 } 48 } 49 //DBから1行ずつ取得し、同じ日付のデータをまとめる 50 Map<String, Row> map = new HashMap<>(); 51 for(Object[] i : DB) { 52 map.computeIfAbsent((String)i[0], k -> new Row()).count((String)i[1], (int)i[2]); 53 } 54 //まとめたデータを日付順にしてthymeleafで使用できるようコンテキストにセット 55 Context context = new Context(); 56 context.setVariable("rows", map.entrySet().stream().sorted(Map.Entry.comparingByKey()).toArray()); 57 //template.htmlからresult.htmlを生成 58 try(FileWriter writer = new FileWriter("result.html")) { 59 engine.process("template", context, writer); 60 } catch (IOException e) { 61 e.printStackTrace(); 62 } 63 } 64}
投稿2018/02/18 02:22
総合スコア94
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。