From 9cd2e68f0bf6297f16eacac590794410ecae867e Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 21 Jun 2020 20:04:00 +0200 Subject: [PATCH] group by fix --- packages/datalib/src/GridConfig.ts | 2 +- packages/datalib/src/GridDisplay.ts | 71 ++++++++++++------- .../web/src/datagrid/ColumnHeaderControl.js | 4 +- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/packages/datalib/src/GridConfig.ts b/packages/datalib/src/GridConfig.ts index ec98d5f9..0aa5f3bf 100644 --- a/packages/datalib/src/GridConfig.ts +++ b/packages/datalib/src/GridConfig.ts @@ -16,7 +16,7 @@ export interface GridReferenceDefinition { }[]; } -export type GroupFunc = 'GROUP' | 'MAX' | 'MIN' | 'SUM' | 'AVG' | 'COUNT' | 'COUNT DISTINCT' +export type GroupFunc = 'GROUP' | 'MAX' | 'MIN' | 'SUM' | 'AVG' | 'COUNT' | 'COUNT DISTINCT' | 'NULL'; export interface GridConfig extends GridConfigColumns { filters: { [uniqueName: string]: string }; diff --git a/packages/datalib/src/GridDisplay.ts b/packages/datalib/src/GridDisplay.ts index cfacbb91..d99c1b08 100644 --- a/packages/datalib/src/GridDisplay.ts +++ b/packages/datalib/src/GridDisplay.ts @@ -5,7 +5,7 @@ import { parseFilter, getFilterType } from '@dbgate/filterparser'; import { filterName } from './filterName'; import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet'; import { Expression, Select, treeToSql, dumpSqlSelect } from '@dbgate/sqltree'; -import { group } from 'console'; +import { isTypeLogical } from '@dbgate/tools'; export interface DisplayColumn { schemaName: string; @@ -206,23 +206,27 @@ export abstract class GridDisplay { const uniqueName = select.columns[i].alias; if (groupColumns && groupColumns.includes(uniqueName)) continue; const grouping = this.getGrouping(uniqueName); - let func = 'MAX'; - let argsPrefix = ''; - if (grouping) { - if (grouping == 'COUNT DISTINCT') { - func = 'COUNT'; - argsPrefix = 'DISTINCT '; - } else { - func = grouping; + if (grouping == 'NULL') { + select.columns[i].alias = null; + } else { + let func = 'MAX'; + let argsPrefix = ''; + if (grouping) { + if (grouping == 'COUNT DISTINCT') { + func = 'COUNT'; + argsPrefix = 'DISTINCT '; + } else { + func = grouping; + } } + select.columns[i] = { + alias: select.columns[i].alias, + exprType: 'call', + func, + argsPrefix, + args: [select.columns[i]], + }; } - select.columns[i] = { - alias: select.columns[i].alias, - exprType: 'call', - func, - argsPrefix, - args: [select.columns[i]], - }; } select.columns = select.columns.filter((x) => x.alias); } @@ -284,6 +288,7 @@ export abstract class GridDisplay { if (this.isGrouped) { if (this.config.grouping[uniqueName]) return this.config.grouping[uniqueName]; const column = this.baseTable.columns.find((x) => x.columnName == uniqueName); + if (isTypeLogical(column?.dataType)) return 'COUNT DISTINCT'; if (column?.autoIncrement) return 'COUNT'; return 'MAX'; } @@ -405,15 +410,33 @@ export abstract class GridDisplay { } getCountQuery() { - const select = this.createSelect(); - select.columns = [ - { - exprType: 'raw', - sql: 'COUNT(*)', - alias: 'count', - }, - ]; + let select = this.createSelect(); select.orderBy = null; + + if (this.isGrouped) { + select = { + commandType: 'select', + from: { + subQuery: select, + alias: 'subq', + }, + columns: [ + { + exprType: 'raw', + sql: 'COUNT(*)', + alias: 'count', + }, + ], + }; + } else { + select.columns = [ + { + exprType: 'raw', + sql: 'COUNT(*)', + alias: 'count', + }, + ]; + } const sql = treeToSql(this.driver, select, dumpSqlSelect); return sql; } diff --git a/packages/web/src/datagrid/ColumnHeaderControl.js b/packages/web/src/datagrid/ColumnHeaderControl.js index 78eb3085..7a603b5c 100644 --- a/packages/web/src/datagrid/ColumnHeaderControl.js +++ b/packages/web/src/datagrid/ColumnHeaderControl.js @@ -41,7 +41,9 @@ export default function ColumnHeaderControl({ column, setSort, onResize, order, return ( - {grouping && {grouping.toLowerCase()}:} + {grouping && ( + {grouping == 'COUNT DISTINCT' ? 'distinct' : grouping.toLowerCase()}: + )} {order == 'ASC' && (