From 7eba64459cd68f7bf6a44a25d47bcecd94b0e453 Mon Sep 17 00:00:00 2001 From: Jim Martens Date: Wed, 5 Jul 2017 13:51:39 +0200 Subject: [PATCH] Extended cross table query to allow different entries for time dimension Signed-off-by: Jim Martens --- 07/src/de/dis2017/MainAnalysis.java | 47 ++++++++++++++++++++++++----- 07/src/de/dis2017/data/db/ORM.java | 10 +++--- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/07/src/de/dis2017/MainAnalysis.java b/07/src/de/dis2017/MainAnalysis.java index 9bb4ae9..a485923 100644 --- a/07/src/de/dis2017/MainAnalysis.java +++ b/07/src/de/dis2017/MainAnalysis.java @@ -6,24 +6,34 @@ import java.util.*; public class MainAnalysis { private static ORM _orm; + private static int _year; + private static String _productDimension; + private static String _locationDimension; + private static String _timeDimension; /** * Starts the application. */ public static void main(String[] args) { _orm = new ORM(); - Map>> sales = _orm.getSalesCrossTable(2017, - "PRODUCTFAMILY", - "NAME"); - printTable(sales); + _year = 2017; + _productDimension = "PRODUCTFAMILY"; + _locationDimension = "NAME"; + _timeDimension = "MONTH"; + + Map>> sales = _orm.getSalesCrossTable(_year, + _productDimension, + _locationDimension, + _timeDimension); + printTable(sales, _timeDimension); } - private static void printTable(Map>> crossTable) { + private static void printTable(Map>> crossTable, String timeDimension) { List articleNames = new ArrayList<>(crossTable.get("total").get("total").keySet()); Collections.sort(articleNames); StringBuilder columnHeaderEdge = new StringBuilder("+------------------------+-----------------+"); StringBuilder columnHeader = new StringBuilder("| Location | Time |"); - StringBuilder leftAlignFormat = new StringBuilder("| %-22s | %-15s |"); + StringBuilder leftAlignFormat = new StringBuilder("| %-22s | %15s |"); for (String article : articleNames) { if (article.equals("total")) continue; @@ -62,12 +72,35 @@ public class MainAnalysis { List times = new ArrayList<>(timeMap.keySet()); Collections.sort(times); + Map months = new HashMap<>(); + months.put(1, "January"); + months.put(2, "February"); + months.put(3, "March"); + months.put(4, "April"); + months.put(5, "May"); + months.put(6, "June"); + months.put(7, "July"); + months.put(8, "August"); + months.put(9, "September"); + months.put(10, "October"); + months.put(11, "November"); + months.put(12, "December"); for (String time : times) { if (time.equals("total")) continue; Map productMap = timeMap.get(time); List values = new ArrayList<>(); values.add(city); - values.add("quarter " + time + ", 2017"); + switch (timeDimension) { + case "QUARTER": + values.add("quarter " + time + ", 2017"); + break; + case "MONTH": + values.add(months.get(Integer.valueOf(time)) + ", 2017"); + break; + case "YEAR": + values.add(time); + break; + } for (String article : articleNames) { if (article.equals("total")) continue; values.add(productMap.get(article)); diff --git a/07/src/de/dis2017/data/db/ORM.java b/07/src/de/dis2017/data/db/ORM.java index b49d4dc..eace4a8 100644 --- a/07/src/de/dis2017/data/db/ORM.java +++ b/07/src/de/dis2017/data/db/ORM.java @@ -203,14 +203,16 @@ public class ORM { * @param productDimension the column of the productDimension to be used (name for article name, productgroup, * productfamily, productcategory) * @param locationDimension the column of the locationDimension to be used (name for shop name, city, region, country) + * @param timeDimension the column of the timeDimension to be used (month, quarter, year) * @return the cross table */ public Map>> getSalesCrossTable(int year, String productDimension, - String locationDimension) { + String locationDimension, + String timeDimension) { String querySQL = "SELECT SUM(s.SOLDUNITS) AS sales, a." + productDimension + " AS article, sh." + locationDimension + " AS city, " + - "d.QUARTER AS quarter " + + "d." + timeDimension + " AS quarter " + "FROM VSISP12.SALES AS s, " + "VSISP12.DATETABLE AS d, " + "VSISP12.SHOP AS sh, " + @@ -221,8 +223,8 @@ public class ORM { "AND d.YEAR = " + year + " " + "GROUP BY GROUPING SETS ( (), (sh." + locationDimension + "), (a." + productDimension + "), " + "(sh." + locationDimension + ", a." + productDimension + "), " + - "(d.QUARTER, sh." + locationDimension + "), " + - "(sh." + locationDimension + ", a." + productDimension + ", d.QUARTER) )"; + "(d." + timeDimension + ", sh." + locationDimension + "), " + + "(sh." + locationDimension + ", a." + productDimension + ", d." + timeDimension + ") )"; Map>> sales = new HashMap<>(); try {