例如,我有一个包含3列的表A,其中包含相同的数据:
TABLE A
KEY COL1 COL2 COL3
1 A B C
2 B C null
3 A null null
4 D E F
5 null C B
6 B C A
7 D E F
因此,我希望该表具有不同的值,并且顺序无关紧要。因此密钥1和6相同,密钥2和5以及密钥4和7也一样。
当然,我不能在选择项中使用仅过滤4和7的字符。
我可以使用非常复杂的case语句,也可以使用带order by的select中的select。但这需要在转换中使用,因此此处的性能是一个问题。 有没有人能做到这一点?
我期望的结果
COL1 COL2 COL3
A B C
B C null
A null null
D E F
答案 0 :(得分:3)
如果可以有很多列,则可以<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.sac.arq.servicing.spring</groupId>
<artifactId>pom-padre-frontend</artifactId>
<version>5.4.4.4</version>
</parent>
<groupId>com.sac.ergo.riskbusinessadmissions</groupId>
<artifactId>risk-business-admissions</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>risk-business-admissions</name>
<repositories />
<pluginRepositories />
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<podam.version>7.2.3.RELEASE</podam.version>
<mockito-all.version>1.9.5</mockito-all.version>
<mockito-core.version>1.9.5</mockito-core.version>
<enax_bdd_test_core.version>1.1.7</enax_bdd_test_core.version>
<enax_bdd_test_impl.version>1.1.5</enax_bdd_test_impl.version>
<jackson-core.version>2.9.8</jackson-core.version>
<jackson-annotations.version>2.9.8</jackson-annotations.version>
<jacoco.version>0.8.3</jacoco.version>
<eiaq_routines.version>1.0.6</eiaq_routines.version>
<enax_validation.version>1.0.4</enax_validation.version>
<conditions.version>1.0.2</conditions.version>
<kygg_imcUtils.version>1.0.0</kygg_imcUtils.version>
<openpojo.version>0.7.3</openpojo.version>
<guava.version>27.1-jre</guava.version>
<roo.version>2.0.0.RELEASE</roo.version>
<!-- <arq-spring-core-spi.version>5.4.0.0</arq-spring-core-spi.version> -->
</properties>
<dependencies>
<!-- Core ASO -->
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-servicing-core</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-rest</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-restutils</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-core-api</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-backend-apx</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-backend-commons</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-backend-otma</artifactId>
</dependency>
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-backend-ps</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>${org.mapstruct.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.9.Final</version>
</dependency>
<!--ThirdParty ASO -->
<!-- <dependency> -->
<!-- <groupId>com.sac.jee.arq.spring.core</groupId> -->
<!-- <artifactId>arq-spring-core-spi</artifactId> -->
<!-- <version>${arq-spring-core-spi.version}</version> -->
<!-- </dependency> -->
<dependency>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-servicing-logging</artifactId>
</dependency>
<!-- Infraestructure Dependencies -->
<dependency>
<groupId>com.sac.infrastructure.multichannel.api</groupId>
<artifactId>multi-channel-infrastructure-api</artifactId>
</dependency>
<dependency>
<groupId>com.sac.infrastructure.multichannel.serv</groupId>
<artifactId>multi-channel-infrastructure-serv-facade</artifactId>
</dependency>
<!-- ROO dependencies -->
<dependency>
<groupId>org.springframework.roo</groupId>
<artifactId>org.springframework.roo.annotations</artifactId>
<scope>provided</scope>
<type>pom</type>
</dependency>
<!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- ThirdParty dependencies -->
<dependency>
<groupId>com.googlecode.openpojo</groupId>
<artifactId>openpojo</artifactId>
<version>${openpojo.version}</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<!--Dependencias Swagger -->
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-core_2.10</artifactId>
<version>${swagger-core.version}</version>
</dependency>
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.10</artifactId>
<version>${swagger-jaxrs.version}</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>uk.co.jemos.podam</groupId>
<artifactId>podam</artifactId>
<version>${podam.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito-all.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<!-- Cucumber dependencies -->
<dependency>
<groupId>com.sac.enax.test</groupId>
<artifactId>enax_bdd_test_core</artifactId>
<version>${enax_bdd_test_core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sac.enax.global.bdd</groupId>
<artifactId>enax_bdd_test_impl</artifactId>
<version>${enax_bdd_test_impl.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
<scope>test</scope>
</dependency>
<!-- Rutines and Libraries dependencies -->
<dependency>
<groupId>com.sac.eiaq</groupId>
<artifactId>eiaq_routines</artifactId>
<exclusions>
<exclusion>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-host-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-host-apx</artifactId>
</exclusion>
</exclusions>
<version>${eiaq_routines.version}</version>
</dependency>
<dependency>
<groupId>com.sac.enax</groupId>
<artifactId>enax_validation</artifactId>
<exclusions>
<exclusion>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-host-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-host-apx</artifactId>
</exclusion>
</exclusions>
<version>${enax_validation.version}</version>
</dependency>
<dependency>
<groupId>com.sac.enax</groupId>
<artifactId>conditions</artifactId>
<exclusions>
<exclusion>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-host-api</artifactId>
</exclusion>
<exclusion>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-connector-host-apx</artifactId>
</exclusion>
</exclusions>
<version>${conditions.version}</version>
</dependency>
<dependency>
<groupId>com.sac.kygg</groupId>
<artifactId>kygg_imcUtils</artifactId>
<version>${kygg_imcUtils.version}</version>
</dependency>
</dependencies>
<build>
<finalName>ergo_${project.artifactId}</finalName>
<!-- Core -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${arq.spring.jdk.version}</source>
<target>${arq.spring.jdk.version}</target>
<encoding>UTF-8</encoding>
<useIncrementalCompilation>false</useIncrementalCompilation>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${org.mapstruct.version}</version>
</path>
<!-- other annotation processors -->
</annotationProcessorPaths>
<compilerArgs>
<arg>-proc:only</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven-resources-plugin.version}</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>generacionFacade.xml</descriptor>
<descriptor>generacionSN.xml</descriptor>
</descriptors>
<outputDirectory>target</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>${aspectj-maven-plugin.version}</version>
<configuration>
<complianceLevel>${arq.spring.jdk.version}</complianceLevel>
<outxml>true</outxml>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-servicing-core</artifactId>
</aspectLibrary>
<aspectLibrary>
<groupId>com.sac.jee.arq.spring.core</groupId>
<artifactId>arq-spring-restutils</artifactId>
</aspectLibrary>
</aspectLibraries>
<source>${arq.spring.jdk.version}</source>
<target>${arq.spring.jdk.version}</target>
</configuration>
<executions>
<execution>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
</plugin>
<!-- Test -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<configuration>
<argLine>${surefireArgLine} -XX:-UseSplitVerifier</argLine>
<printSummary>false</printSummary>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<excludes>
<exclude>**/*_Roo_*</exclude>
<exclude>${skip.tests}</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<configuration>
<excludes>
<exclude>com/sac/enax/cards/dao/model/**/*.class</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/jacoco.exec</destFile>
<propertyName>surefireArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/jacoco.exec</dataFile>
<outputDirectory>${project.reporting.outputDirectory}/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!-- IDE -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${maven-eclipse-plugin.version}</version>
<!-- Note 2.8 does not work with AspectJ aspect path -->
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
<wtpversion>2.0</wtpversion>
<additionalBuildcommands>
<buildCommand>
<name>org.eclipse.ajdt.core.ajbuilder</name>
<arguments>
<aspectPath>org.springframework.aspects</aspectPath>
</arguments>
</buildCommand>
<buildCommand>
<name>org.springframework.ide.eclipse.core.springbuilder</name>
</buildCommand>
</additionalBuildcommands>
<additionalProjectnatures>
<projectnature>org.eclipse.ajdt.ui.ajnature</projectnature>
<projectnature>com.springsource.sts.roo.core.nature</projectnature>
<projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
</additionalProjectnatures>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-idea-plugin</artifactId>
<version>${maven-idea-plugin.version}</version>
<configuration>
<downloadSources>true</downloadSources>
<dependenciesAsLibraries>true</dependenciesAsLibraries>
</configuration>
</plugin>
<!-- Servers -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>${tomcat-maven-plugin.version}</version>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty-maven-plugin.version}</version>
<configuration>
<webAppConfig>
<contextPath>/${project.name}</contextPath>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>unit-test</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<build.profile.id>unit-test</build.profile.id>
<skip.tests>**/RunIntegradoTest.java</skip.tests>
</properties>
</profile>
<profile>
<id>integration-test</id>
<properties>
<build.profile.id>integration-test</build.profile.id>
<skip.tests>**/business/**, **/dao/**, **/facade/**,
**/TestGetterSetter.java</skip.tests>
</properties>
</profile>
</profiles>
</project>
然后对值进行排序,然后依次UNPIVOT
并进行PIVOT
行:
Oracle设置:
DISTINCT
查询:
CREATE TABLE table_name ( KEY, COL1, COL2, COL3 ) AS
SELECT 1, 'A', 'B', 'C' FROM DUAL UNION ALL
SELECT 2, 'B', 'C', null FROM DUAL UNION ALL
SELECT 3, 'A', null, null FROM DUAL UNION ALL
SELECT 4, 'D', 'E', 'F' FROM DUAL UNION ALL
SELECT 5, null, 'C', 'B' FROM DUAL UNION ALL
SELECT 6, 'B', 'C', 'A' FROM DUAL UNION ALL
SELECT 7, 'D', 'E', 'F' FROM DUAL
输出:
COL1 | COL2 | COL3 :--- | :--- | :--- A | B | C B | C | null D | E | F A | null | null
db <>提琴here
答案 1 :(得分:0)
复杂的case
表达式将具有最佳性能。但是最简单的方法是条件聚合:
select key,
max(case when seqnum = 1 then col end) as col1,
max(case when seqnum = 2 then col end) as col2,
max(case when seqnum = 3 then col end) as col3
from (select key,col,
row_number() over (partition by key order by col asc) as seqnum
from ((select key, col1 as col from t) union all
(select key, col2 as col from t) union all
(select key, col3 as col from t)
) kc
where col is not null
) kc
group by key;