group by fix

This commit is contained in:
Jan Prochazka 2020-06-21 20:04:00 +02:00
parent e1eb8ffd56
commit 9cd2e68f0b
3 changed files with 51 additions and 26 deletions

View File

@ -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 { export interface GridConfig extends GridConfigColumns {
filters: { [uniqueName: string]: string }; filters: { [uniqueName: string]: string };

View File

@ -5,7 +5,7 @@ import { parseFilter, getFilterType } from '@dbgate/filterparser';
import { filterName } from './filterName'; import { filterName } from './filterName';
import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet'; import { ChangeSetFieldDefinition, ChangeSetRowDefinition } from './ChangeSet';
import { Expression, Select, treeToSql, dumpSqlSelect } from '@dbgate/sqltree'; import { Expression, Select, treeToSql, dumpSqlSelect } from '@dbgate/sqltree';
import { group } from 'console'; import { isTypeLogical } from '@dbgate/tools';
export interface DisplayColumn { export interface DisplayColumn {
schemaName: string; schemaName: string;
@ -206,23 +206,27 @@ export abstract class GridDisplay {
const uniqueName = select.columns[i].alias; const uniqueName = select.columns[i].alias;
if (groupColumns && groupColumns.includes(uniqueName)) continue; if (groupColumns && groupColumns.includes(uniqueName)) continue;
const grouping = this.getGrouping(uniqueName); const grouping = this.getGrouping(uniqueName);
let func = 'MAX'; if (grouping == 'NULL') {
let argsPrefix = ''; select.columns[i].alias = null;
if (grouping) { } else {
if (grouping == 'COUNT DISTINCT') { let func = 'MAX';
func = 'COUNT'; let argsPrefix = '';
argsPrefix = 'DISTINCT '; if (grouping) {
} else { if (grouping == 'COUNT DISTINCT') {
func = grouping; 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); select.columns = select.columns.filter((x) => x.alias);
} }
@ -284,6 +288,7 @@ export abstract class GridDisplay {
if (this.isGrouped) { if (this.isGrouped) {
if (this.config.grouping[uniqueName]) return this.config.grouping[uniqueName]; if (this.config.grouping[uniqueName]) return this.config.grouping[uniqueName];
const column = this.baseTable.columns.find((x) => x.columnName == uniqueName); const column = this.baseTable.columns.find((x) => x.columnName == uniqueName);
if (isTypeLogical(column?.dataType)) return 'COUNT DISTINCT';
if (column?.autoIncrement) return 'COUNT'; if (column?.autoIncrement) return 'COUNT';
return 'MAX'; return 'MAX';
} }
@ -405,15 +410,33 @@ export abstract class GridDisplay {
} }
getCountQuery() { getCountQuery() {
const select = this.createSelect(); let select = this.createSelect();
select.columns = [
{
exprType: 'raw',
sql: 'COUNT(*)',
alias: 'count',
},
];
select.orderBy = null; 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); const sql = treeToSql(this.driver, select, dumpSqlSelect);
return sql; return sql;
} }

View File

@ -41,7 +41,9 @@ export default function ColumnHeaderControl({ column, setSort, onResize, order,
return ( return (
<HeaderDiv> <HeaderDiv>
<LabelDiv> <LabelDiv>
{grouping && <GroupingLabel>{grouping.toLowerCase()}:</GroupingLabel>} {grouping && (
<GroupingLabel>{grouping == 'COUNT DISTINCT' ? 'distinct' : grouping.toLowerCase()}:</GroupingLabel>
)}
<ColumnLabel {...column} /> <ColumnLabel {...column} />
{order == 'ASC' && ( {order == 'ASC' && (