我需要将子项的总和添加到父项。儿童物品' childId'与父项' parentId'相同。
在下表中,ParentId' 45627'总数为0.但是,我需要为11. 11是子项目总数的总和。
+----------+---------+-------+------------+
| ParentId | ChildId | Name | TotalCount |
+----------+---------+-------+------------+
| 45627 | 12568 | Test1 | 0 |
| 52678 | 45627 | Test2 | 0 |
| 23123 | 45627 | Test3 | 7 |
| 54312 | 45627 | Test4 | 3 |
| 32123 | 45627 | Test5 | 0 |
| 12111 | 45627 | Test6 | 1 |
| 32122 | 45627 | Test7 | 0 |
| 43123 | 45627 | Test8 | 0 |
+----------+---------+-------+------------+
预期产出:
+----------+---------+-------+------------+
| ParentId | ChildId | Name | TotalCount |
+----------+---------+-------+------------+
| 45627 | 12568 | Test1 | 11 |
| 52678 | 45627 | Test2 | 0 |
| 23123 | 45627 | Test3 | 7 |
| 54312 | 45627 | Test4 | 3 |
| 32123 | 45627 | Test5 | 0 |
| 12111 | 45627 | Test6 | 1 |
| 32122 | 45627 | Test7 | 0 |
| 43123 | 45627 | Test8 | 0 |
+----------+---------+-------+------------+
虽然能够通过自我加入实现,但性能太慢。
是否有其他方法可以获得更好的预期输出效果。
我试过的查询
SELECT a.parentId
,a.childId
,out1.TotalCount
FROM test a
LEFT JOIN (
SELECT a.parentId
,sum(b.TotalCount) AS TotalCount
FROM test a
INNER JOIN test b ON a.ParentId = b.childId
GROUP BY a.ParentId
) AS out1 ON a.ParentId = out1.ParentId
注意:上表是示例。记录数可以是10,000。
抱歉标记不佳。
答案 0 :(得分:0)
如果我正确理解您的需求,这个问题应该符合您的期望:
SELECT x.parentId,
x.childId,
CASE WHEN x.total > 0 THEN x.total ELSE x.TotalCount END AS Total
FROM
(SELECT a.parentId
,a.childId,
,a.TotalCount
,(SELECT SUM(b.TotalCount) FROM test b WHERE b.childId = a.parentId) AS total
FROM test a ) x
答案 1 :(得分:0)
列名没有意义,输入数据没有意义,(但这是有争议的),并且所需的输出也没有任何意义。这就是为什么这个问题还没有得到任何答案的原因。
假设首先," ParentId"已被重命名为" Id"和" ChildId"已被重命名为" ParentId",以便模型有意义,以便数据类型有意义,然后如下:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>CucumberJavaProject</groupId>
<artifactId>cucumberJava</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.47.1</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-core</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>1.2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>gherkin</artifactId>
<version>2.12.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12</version>
<configuration>
<parallel>methods</parallel>
<threadCount>20</threadCount>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
</configuration>
</plugin>
</plugins>
</build>
产生以下输出:
CREATE TABLE TEST
(
Id INTEGER,
ParentId INTEGER,
Name NVARCHAR(100),
TotalCount INTEGER
);
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 45627, 12568, 'Test1', 0 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 52678, 45627, 'Test2', 0 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 23123, 45627, 'Test3', 7 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 54312, 45627, 'Test4', 3 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 32123, 45627, 'Test5', 0 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 12111, 45627, 'Test6', 1 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 32122, 45627, 'Test7', 0 );
INSERT INTO TEST( Id, ParentId, Name, TotalCount ) VALUES( 43123, 45627, 'Test8', 0 );
SELECT Parent.Id, SUM( Child.TotalCount ) AS Total
FROM TEST AS Parent
LEFT JOIN TEST As Child ON Parent.id = Child.ParentId
GROUP BY Parent.Id;
根据上述假设,给定输入数据的正确输出是什么。
45627也会产生11,这是OP想要的。
如果问题被编辑有意义,我会相应地修改我的答案。