如何计算PIVOT中的百分比

时间:2019-07-03 09:46:18

标签: sql oracle

我正在尝试计算数据透视表中每个部门的车辆类型百分比。我做到了:

mobx observer/observable

结果:

import React from 'react';
import _ from 'lodash';
import TodoCreate from './todo-create';
import TodoList from './todo-list';
import 'semantic-ui-css/semantic.min.css';
import { Header } from 'semantic-ui-react';
import {decorate, observable} from "mobx"
import {observer} from "mobx-react"
export default class App extends 
React.Component {

constructor(props) {
    super(props);

    this.state = {
       todos : todos
    }

    this.createTask = this.createTask.bind(this);
    this.saveTask = this.saveTask.bind(this);
    this.deleteTask = this.deleteTask.bind(this);
    this.toggleTask = this.toggleTask.bind(this);
}

createTask(task) {
    this.state.todos.push({
        task,
        isCompleted: false,
        creationDate:new Date().toLocaleTimeString() + new Date().toLocaleDateString(),
        updateDate:new Date().toLocaleTimeString() +  new Date().toLocaleDateString()
    });
    this.setState({ todos: this.state.todos });
}

deleteTask(taskToDelete) {
    _.remove(this.state.todos, todo => todo.task === taskToDelete);
    this.setState({ todos: this.state.todos });
}

saveTask(oldTask, newTask) {
    const foundTodo = _.find(this.state.todos, todo => todo.task === oldTask);
    foundTodo.task = newTask;
    foundTodo.updateDate = new Date().toLocaleTimeString() +  new Date().toLocaleDateString();
    this.setState({ todos: this.state.todos });
}

toggleTask(task) {
    const foundTodo = _.find(this.state.todos, todo => todo.task === task);
    foundTodo.isCompleted = !foundTodo.isCompleted;
    foundTodo.updateDate = new Date().toLocaleTimeString() + '' + new Date().toLocaleDateString();
    this.setState({ todos: this.state.todos });
}

render() {
    return (
        <div>
            <div className="wrap">
            <div className="header">
                <Header as="h2" className="headerclass">ls-tech Todo-list</Header></div>
                  <div className="wrap-list">

            <div className="td-list-con">

                <TodoCreate
                    todos={this.state.todos}
                    createTask={ this.createTask}
                    />

                <TodoList
                    todos={ this.state.todos }
                    saveTask={ this.saveTask }
                    deleteTask={ this.deleteTask }
                    toggleTask={ this.toggleTask }
                    />
               </div>
            </div>
        </div>
    </div>
    )
}
}

这是我想要的:

    SELECT
    *
FROM
    (
    SELECT
        CASE
            WHEN TYPE_V = 'N1' THEN 'CAR'
            WHEN TYPE_INCIDENT IN ('N2',
            'N3') THEN 'BUS'
        END TYPE_VEH,
        DEPARTEMENT
    FROM
        DWH
    WHERE
        CODE = 'YES' ) PIVOT (COUNT(DEPARTEMENT)FOR DEPARTEMENT IN ('D1' AS D1,
    'D2' AS D2,
    ) )

4 个答案:

答案 0 :(得分:1)

您可以使用

SELECT TYPE_VEH, D1 / (D1+D2) as D1, D2 / (D1+D2) as D2 FROM ...

代替

SELECT * FROM

答案 1 :(得分:0)

我认为您甚至不需要PIVOT。

您可以直接从表DW_DECSI2.DWH_DSI_F_PLAN_PERFORMANCE中获取此输出,如下所示:

SELECT
    DEPARTEMENT,
    (D1 * 100) / ( D1 + D2 ) || '%' AS D1,
    (D2 * 100) / ( D1 + D2 ) || '%' AS D2
FROM
    (
        SELECT
            DEPARTEMENT,
            SUM(CASE
                WHEN TYPE_V = 'N1' THEN 1
                ELSE 0
            END) AS D1,
            SUM(CASE
                WHEN TYPE_V <> 'N1'
                     AND TYPE_INCIDENT IN(
                    'N2', 'N3'
                ) THEN 1
                ELSE 0
            END) AS D2
        FROM
            DW_DECSI2.DWH_DSI_F_PLAN_PERFORMANCE
        WHERE
            CODE = 'YES'
        GROUP BY
            DEPARTEMENT
    );

干杯!

答案 2 :(得分:0)

您需要aggregation来获取百分比,我建议您使用窗口分析功能来在子查询中获取聚合,而无需进行分组。

SELECT TYPE_VEH, ( D1 / TOT ) * 100||'%' AS D1, ( D2 / TOT ) * 100||'%' AS D2               
  FROM (SELECT CASE
                 WHEN TYPE_V = 'N1' THEN
                  'CAR'
                 WHEN TYPE_INCIDENT IN ('N2', 'N3') THEN
                  'BUS'
              END TYPE_VEH,
              DEPARTMENT,
              COUNT(*) OVER (PARTITION BY CASE WHEN TYPE_V = 'N1' THEN 1 ELSE 0 END 
                             ORDER BY 1) TOT
         FROM DWH
        WHERE CODE = 'YES') 
 PIVOT(COUNT(DEPARTMENT) FOR DEPARTMENT IN('D1' AS D1,'D2' AS D2));

Demo

答案 3 :(得分:0)

我只会使用条件聚合:

SELECT TYPE_VEH,
       AVG(CASE WHEN DEPARTMENT = 'D1' THEN 100.0 ELSE 0 END) as D1,
       AVG(CASE WHEN DEPARTMENT = 'D2' THEN 100.0 ELSE 0 END) as D2
FROM (SELECT (CASE WHEN TYPE_V = 'N1' THEN 'CAR'
                   WHEN TYPE_INCIDENT IN ('N2', 'N3') THEN 'BUS'
              END) AS TYPE_VEH,
              DWH.*
      FROM DWH
      WHERE CODE = 'YES'
     ) D
GROUP BY TYPE_VEH;

通常,您希望此值为 number 。但是,如果您希望将其格式化为字符串,则可以使用|| '%'来显示百分比时间。