Extended cross table query to allow different entries for location dimension

Signed-off-by: Jim Martens <github@2martens.de>
This commit is contained in:
Jim Martens 2017-07-05 13:34:32 +02:00
parent 8e4b359762
commit e601db107e
2 changed files with 15 additions and 9 deletions

View File

@ -13,16 +13,17 @@ public class MainAnalysis {
public static void main(String[] args) { public static void main(String[] args) {
_orm = new ORM(); _orm = new ORM();
Map<String,Map<String, Map<String, Integer>>> sales = _orm.getSalesCrossTable(2017, Map<String,Map<String, Map<String, Integer>>> sales = _orm.getSalesCrossTable(2017,
"PRODUCTFAMILY"); "PRODUCTFAMILY",
"NAME");
printTable(sales); printTable(sales);
} }
private static void printTable(Map<String, Map<String, Map<String, Integer>>> crossTable) { private static void printTable(Map<String, Map<String, Map<String, Integer>>> crossTable) {
List<String> articleNames = new ArrayList<>(crossTable.get("total").get("total").keySet()); List<String> articleNames = new ArrayList<>(crossTable.get("total").get("total").keySet());
Collections.sort(articleNames); Collections.sort(articleNames);
StringBuilder columnHeaderEdge = new StringBuilder("+-----------------+-----------------+"); StringBuilder columnHeaderEdge = new StringBuilder("+------------------------+-----------------+");
StringBuilder columnHeader = new StringBuilder("| Location | Time |"); StringBuilder columnHeader = new StringBuilder("| Location | Time |");
StringBuilder leftAlignFormat = new StringBuilder("| %-15s | %-15s |"); StringBuilder leftAlignFormat = new StringBuilder("| %-22s | %-15s |");
for (String article : articleNames) { for (String article : articleNames) {
if (article.equals("total")) continue; if (article.equals("total")) continue;

View File

@ -202,10 +202,14 @@ public class ORM {
* @param year the year to be queried * @param year the year to be queried
* @param productDimension the column of the productDimension to be used (name for article name, productgroup, * @param productDimension the column of the productDimension to be used (name for article name, productgroup,
* productfamily, productcategory) * productfamily, productcategory)
* @param locationDimension the column of the locationDimension to be used (name for shop name, city, region, country)
* @return the cross table * @return the cross table
*/ */
public Map<String,Map<String, Map<String, Integer>>> getSalesCrossTable(int year, String productDimension) { public Map<String,Map<String, Map<String, Integer>>> getSalesCrossTable(int year,
String querySQL = "SELECT SUM(s.SOLDUNITS) AS sales, a." + productDimension + " AS article, sh.CITY AS city, " + String productDimension,
String locationDimension) {
String querySQL = "SELECT SUM(s.SOLDUNITS) AS sales, a." + productDimension + " AS article, sh." +
locationDimension + " AS city, " +
"d.QUARTER AS quarter " + "d.QUARTER AS quarter " +
"FROM VSISP12.SALES AS s, " + "FROM VSISP12.SALES AS s, " +
"VSISP12.DATETABLE AS d, " + "VSISP12.DATETABLE AS d, " +
@ -215,9 +219,10 @@ public class ORM {
"AND s.STOREID = sh.ID " + "AND s.STOREID = sh.ID " +
"AND s.ARTICLEID = a.ID " + "AND s.ARTICLEID = a.ID " +
"AND d.YEAR = " + year + " " + "AND d.YEAR = " + year + " " +
"GROUP BY GROUPING SETS ( (), (sh.CITY), (a." + productDimension + "), " + "GROUP BY GROUPING SETS ( (), (sh." + locationDimension + "), (a." + productDimension + "), " +
"(sh.CITY, a." + productDimension + "), " + "(sh." + locationDimension + ", a." + productDimension + "), " +
"(d.QUARTER, sh.CITY), (sh.CITY, a." + productDimension + ", d.QUARTER) )"; "(d.QUARTER, sh." + locationDimension + "), " +
"(sh." + locationDimension + ", a." + productDimension + ", d.QUARTER) )";
Map<String,Map<String, Map<String, Integer>>> sales = new HashMap<>(); Map<String,Map<String, Map<String, Integer>>> sales = new HashMap<>();
try { try {