我有一个表t1的postgresql数据库,我想计算一个阈值。 阈值应该是例如汽车1使用的燃料比所有汽车的75%多,car2使用的燃料比所有汽车的50%多,.... 在数学上我理解我想做什么,但我不知道如何构建查询
id | name | value | threshold
________________________
1 | car1 | 30 | ...%
2 | car2 | 15 | ..%
3 | car3 | 7 |
4 | car4 | 5 |
这是一个sql小提琴 http://sqlfiddle.com/#!15/1e914/1
UPDATE t1
SET threshold =
select count(value)
from t1
其中(值> [每行]) 然后是* 100 /总计数()
抱歉这个糟糕的尝试,但我有点迷茫。还尝试了一些集合函数。答案 0 :(得分:5)
您可以使用window function:
非常优雅地解决这个问题UPDATE t1
SET threshold = sub.thr
FROM (
SELECT id, 100. * (rank() OVER (ORDER BY value) - 1) / count(*) OVER () AS thr
FROM t1) sub
WHERE t1.id = sub.id;
rank()
函数给出有序集中的排名(从1开始),在本例中为value
列,然后除以集合中的总行数。请注意,count(*) OVER ()
计算分区中的总行数,但它不像常规count(*)
那样聚合行。
答案 1 :(得分:1)
WITH q AS
(
SELECT *,
(RANK() OVER (ORDER BY value) - 1) * 100. / COUNT(*) OVER () nt
FROM mytable
)
UPDATE mytable
SET threshold = nt
FROM q
WHERE mytable.id = q.id
答案 2 :(得分:0)
您想计算使用燃料的百分比吗?
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.cbp.dv</groupId>
<artifactId>demo-cbp-dv</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>demo app </name>
<url>http://www.demhas.com</url>
<packaging>bundle</packaging>
<properties>
<fabric8.profile>demhas-cbp-dv</fabric8.profile>
<fabric8.features>feature-camel</fabric8.features>
<fabric8.bundles>wrap:mvn:com.oracle/ojdbc6/${ojdbc-version}
wrap:mvn:commons-dbcp/commons-dbcp/${commons-dbcp-version}
wrap:mvn:org.json/json/${json-version}
</fabric8.bundles>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<activemq-version>5.10.0</activemq-version>
<jackson-version>2.5.4</jackson-version>
<json-version>20150729</json-version>
<json2-version>20140107</json2-version>
<slf4j-version>1.7.12</slf4j-version>
<log4j-version>1.2.17</log4j-version>
<ojdbc-version>11.2.0.4</ojdbc-version>
<commons-dbcp-version>1.4</commons-dbcp-version>
<camel-version>2.15.2</camel-version>
<fabic8-version>1.2.0.redhat-133</fabic8-version>
</properties>
<dependencies>
<dependency>
<groupId>org.ow2.asm</groupId>
<artifactId>asm-commons</artifactId>
<version>4.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-blueprint</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-salesforce</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-http</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jsonpath</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mybatis</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test-blueprint</artifactId>
<version>${camel-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-test</artifactId>
<version>${camel-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jaxb</artifactId>
<version>${camel-version}</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${ojdbc-version}</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>${commons-dbcp-version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-client</artifactId>
<version>${activemq-version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-camel</artifactId>
<version>${activemq-version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-pool</artifactId>
<version>${activemq-version} </version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>${json-version}</version>
</dependency>
</dependencies>
<repositories>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>public.fusesource.org</id>
<name>FuseSource Public Repository</name>
<url>http://repo.fusesource.com/nexus/content/groups/public/</url>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>release.fusesource.org</id>
<name>FuseSource Release Repository</name>
<url>http://repo.fusesource.com/nexus/content/repositories/releases</url>
</repository>
<repository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>ea.fusesource.org</id>
<name>FuseSource Community Early Access Release Repository</name>
<url>http://repo.fusesource.com/nexus/content/groups/ea</url>
</repository>
<repository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>snapshot.fusesource.org</id>
<name>FuseSource Snapshot Repository</name>
<url>http://repo.fusesource.com/nexus/content/repositories/snapshots</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>public.fusesource.org</id>
<name>FuseSource Public Repository</name>
<url>http://repo.fusesource.com/nexus/content/groups/public</url>
</pluginRepository>
<pluginRepository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>release.fusesource.org</id>
<name>FuseSource Release Repository</name>
<url>http://repo.fusesource.com/nexus/content/repositories/releases</url>
</pluginRepository>
<pluginRepository>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
<id>ea.fusesource.org</id>
<name>FuseSource Community Early Access Release Repository</name>
<url>http://repo.fusesource.com/nexus/content/groups/ea</url>
</pluginRepository>
<pluginRepository>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>snapshot.fusesource.org</id>
<name>FuseSource Snapshot Repository</name>
<url>http://repo.fusesource.com/nexus/content/repositories/snapshots</url>
</pluginRepository>
</pluginRepositories>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>2.3.7</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Private-Package>com.mycompany.*</Private-Package>
<Import-Package>*</Import-Package>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-maven-plugin</artifactId>
<version>${camel-version}</version>
<configuration>
<useBlueprint>true</useBlueprint>
</configuration>
</plugin>
<!-- fabric8 plugin for using deploying via mvn fabric:deploy -->
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>fabric8-maven-plugin</artifactId>
<version>${fabic8-version}</version>
</plugin>
</plugins>
</build>
</project>
或者,如果您想计算特定阈值,您可以显示预期结果吗?