我在下一份报告中遇到了一个相当困难的问题。我有一些线索,但似乎无法将其包装在正确的查询中。我尝试使用Group_Concat
而不是Concat
个数字,但我没有得到预期的结果。
以完全所有权,永久产权和 Usufruct (Google翻译这些条款)交换份额
这些交易后的情况:
- 在数据库中,这些交易将存储为:
+----+-------------+-----------+------------+------------+------------+------+ | ID | Shareholder | Quantity | IsPurchase | NumberFrom | NumberTill | Type | +----+-------------+-----------+------------+------------+------------+------+ | 1 | Person A | 100 | true | 1 | 100 | FO | | 2 | Person A | 20 | false | 81 | 100 | FO | | 3 | Person A | 20 | true | 81 | 100 | FH | | 4 | Person B | 20 | true | 81 | 100 | US | | 5 | Person A | 100 | true | 201 | 300 | FO | +----+-------------+-----------+------------+------------+------------+------+
- >我的查询所需的输出如下:
+-------------+-----------+-----------+-----------+----------------+------------+------------+ | Shareholder | Shares_FO | Shares_FH | Shares_US | Numbers_FO | Numbers_FH | Numbers_Us | +-------------+-----------+-----------+-----------+----------------+------------+------------+ | Person A | 180 | 20 | | '1,80;201,300' | '81,100' | | | Person B | | | 20 | | | '81,100' | +-------------+-----------+-----------+-----------+----------------+------------+------------+
到目前为止我所拥有的是除了股票号码之外的所有内容。我在CASE WHEN
列上使用Type
将SUM
分配到FO,FH和US列中。当IsPurchase为false时,我减去共享的数量。所以这一切都有效。我只缺少最后三列。
求和的代码片段。
SELECT Shareholder,
SUM(CASE WHEN Type = 'FO' THEN
CASE WHEN IsPurchase THEN Quantity ELSE -Quantity END
END) AS Shares_FO,
SUM(CASE WHEN SRI_ID = 'FH' THEN
CASE WHEN IsPurchase THEN Quantity ELSE -Quantity END
END) AS Shares_FH,
SUM(CASE WHEN SRI_ID = 'US' THEN
CASE WHEN IsPurchase THEN Quantity ELSE -Quantity END
END) AS Shares_US
非常感谢提前!