【问题标题】:SQL Unions and aggregation with multiple tables多个表的 SQL 联合和聚合
【发布时间】:2020-11-17 12:57:00
【问题描述】:

我有如下三张表。

表 1:

╔═════════════════════╗
║   Country_table     ║
╠══════════════╦══════╣
║ Country_Name ║ Code ║
╠══════════════╬══════╣
║ India        ║    1 ║
╠══════════════╬══════╣
║ UK           ║    2 ║
╠══════════════╬══════╣
║ france       ║    3 ║
╠══════════════╬══════╣
║ germany      ║    4 ║
╚══════════════╩══════╝

表 2:

╔════════════════════════════════════════════════════════════════════════════════╗
║                                 Trade_Details                                  ║
╠═════════╦═══════════╦═════════════╦═══════════╦══════════╦════════╦════════════╣
║ TradeID ║ ProductID ║ FromCountry ║ ToCountry ║ Curruncy ║ Amount ║ Date       ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T1      ║ P1        ║ 1           ║ 3         ║ INR      ║ 10     ║ 01/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T2      ║ P2        ║ 3           ║ 2         ║ USD      ║ 11     ║ 10/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T3      ║ P1        ║ 1           ║ 4         ║ GBP      ║ 12     ║ 20/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T4      ║ P2        ║ 2           ║ 3         ║ INR      ║ 13     ║ 21/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T5      ║ P1        ║ 1           ║ 4         ║ USD      ║ 14     ║ 22/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T6      ║ P2        ║ 4           ║ 2         ║ GBP      ║ 15     ║ 23/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T7      ║ P1        ║ 3           ║ 1         ║ INR      ║ 16     ║ 24/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T8      ║ P2        ║ 3           ║ 1         ║ USD      ║ 17     ║ 25/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T9      ║ P1        ║ 2           ║ 3         ║ GBP      ║ 18     ║ 26/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T10     ║ P2        ║ 1           ║ 4         ║ INR      ║ 19     ║ 27/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T11     ║ P1        ║ 3           ║ 1         ║ USD      ║ 20     ║ 28/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T12     ║ P2        ║ 1           ║ 1         ║ GBP      ║ 21     ║ 29/01/2020 ║
╠═════════╬═══════════╬═════════════╬═══════════╬══════════╬════════╬════════════╣
║ T13     ║ P1        ║ 2           ║ 2         ║ INR      ║ 22     ║ 30/01/2020 ║
╚═════════╩═══════════╩═════════════╩═══════════╩══════════╩════════╩════════════╝

表 3:

╔═══════════════════════════════════════════════════════╗
║                   TradeStatus_Table                   ║
╠═════════╦════════════╦═══════════════════╦════════════╣
║ TradeID ║ StatusCode ║ StatusDescription ║ Date       ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T1      ║ inProcess  ║ Reached HUB1      ║ 01/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T1      ║ inProcess  ║ Reached HUB2      ║ 01/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T1      ║ inProcess  ║ Reached HUB3      ║ 01/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T1      ║ delivered  ║ delivered         ║ 01/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T2      ║ inProcess  ║ Reached HUB1      ║ 10/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T2      ║ inProcess  ║ Reached HUB2      ║ 10/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T2      ║ inProcess  ║ Reached HUB3      ║ 10/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T2      ║ Returned   ║ returned to home  ║ 10/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T3      ║ inProcess  ║ Reached HUB1      ║ 20/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T3      ║ inProcess  ║ Reached HUB2      ║ 20/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T3      ║ inProcess  ║ Reached HUB3      ║ 20/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T3      ║ inProcess  ║ Reached HUB4      ║ 20/01/2020 ║
╠═════════╬════════════╬═══════════════════╬════════════╣
║ T3      ║ inProcess  ║ Reached HUB5      ║ 20/01/2020 ║
╚═════════╩════════════╩═══════════════════╩════════════╝

输出表:

Delivered : This column represents the total number of transactions final status as either delivered or returned.
  
InProcess : This column represents the total number of transactions doesn't contains final status as either delivered or returned.  

╔═════════════════════════════════════════════════════════════════════╗
║                         Report   1 (example)                        ║
╠═════════════╦═══════════╦═══════════╦═══════════╦═══════════════════╣
║ FromCountry ║ ToCountry ║ Delivered ║ inProcess ║ Description       ║
╠═════════════╬═══════════╬═══════════╬═══════════╬═══════════════════╣
║ India       ║ UK        ║ 1         ║ 1         ║ total transactions║
╠═════════════╬═══════════╬═══════════╬═══════════╬═══════════════════╣
║ UK          ║ India     ║ 2         ║ 1         ║ total transactions║
╠═════════════╬═══════════╬═══════════╬═══════════╬═══════════════════╣
║ France      ║ India     ║ 2         ║ 1         ║ total transactions║
╚═════════════╩═══════════╩═══════════╩═══════════╩═══════════════════╝

No of Trades : This column contains Total number of transactions were made between from country and to country.
  
Total Trade Value :- This column contains Total sum of value of the transactions made between from country and to country based on currency type. 
╔═══════════════════════════════════════════════════════════════════════╗
║                          Report   2 (example)                         ║
╠═════════════╦═══════════╦══════════════╦══════════╦═══════════════════╣
║ FromCountry ║ ToCountry ║ No of Trades ║ Currency ║ Total Trade Value ║
╠═════════════╬═══════════╬══════════════╬══════════╬═══════════════════╣
║ India       ║ UK        ║ 2            ║ INR      ║ 1000              ║
╠═════════════╬═══════════╬══════════════╬══════════╬═══════════════════╣
║ India       ║ UK        ║ 1            ║ USD      ║ 10                ║
╠═════════════╬═══════════╬══════════════╬══════════╬═══════════════════╣
║ UK          ║ India     ║ 2            ║ GBP      ║ 10                ║
╠═════════════╬═══════════╬══════════════╬══════════╬═══════════════════╣
║ France      ║ India     ║ 1            ║ INR      ║ 20                ║
╚═════════════╩═══════════╩══════════════╩══════════╩═══════════════════╝

我尝试了很多组合,但无法找出所需的输出。请帮我解决这个问题。

第一次查询,但无法容纳进程内消息数。

select source.Country_name, destination.country_name, count(*)
from Trade_Details, Country_table source, Country_table destination
where date > '2020/01/01 00:00:00'
and date <'2020/02/01/ 00:00:00'
and FromCountry = source.code
and ToCountry =destination.code
group by source.Country_name, destination.country_name

第二次查询,

select source.Country_name as source, destination.country_name as destination, count(*) as  inprocessCount
from Trade_Details a1, Country_table source, Country_table destination
where date > '2020/01/01 00:00:00'
and date <'2020/02/01/ 00:00:00'
and FromCountry = source.code
and ToCountry =destination.code
and 0=(select count(*) from Trade_Details a2 where (a2.StatusCode='delivered' or a2.StatusCode='Returned') and a1.TradeID=a2.TradeID)
group by source.Country_name, destination.country_name 

问题:

基本上我想合并两个查询输出以在单个查询中获取它。但未能如愿。如果可能的话,请您帮我处理这两个报告。

问候, KS

【问题讨论】:

  • 不要标记垃圾邮件:我怀疑这个问题与 MySql、Oracle 和 Sybase 有关系。发布屏幕截图也根本没有改进。
  • 您需要对这个问题进行大量改进,然后才能让任何人弄清楚您真正想要做什么,以及如何在 SQL 中实现这一目标。尝试在一个查询中获取两个结果表可能不值得。 SQL 的一般模式是一个查询提供一个结果表。

标签: sql sybase


【解决方案1】:

在此处报告 1 个解决方案!!!

SELECT source.country_name       AS FromCountry, 
       destination.country_name  AS ToCountry, 
       Count(Trdsts1.statuscode) AS Delivered, 
       Count(Trdsts2.statuscode) AS inProcess, 
       'no of messages'          AS Description 
FROM   trade_details Trddts, 
       tradestatus_table Trdsts1, 
       tradestatus_table Trdsts2, 
       country_table source, 
       country_table destination 
WHERE  Trddts.date > '2020/01/01 00:00:00' 
       AND Trddts.date < '2020/02/01/ 00:00:00' 
       AND Trdsts1.tradeid = Trddts.tradeid 
       AND Trdsts2.tradeid = Trddts.tradeid 
       AND Trdsts1.statuscode = "delivered"  
       AND Trdsts2.statuscode ="inprocess" 
       AND source.code = Trddts.fromcountry 
       AND destination.code = Trddts.tocountry 

【讨论】:

  • 它没有按预期工作,我得到了 fromcountry 和 tocountry 的重复元素。并且计数始终为 0。
  • 这是您的示例数据不是很好的地方,因为您在描述中没有“消息”,而不是在示例输出中实际详细说明您想要总计(我从您的计数中假设( *) 但你没有这么说...)
猜你喜欢
  • 2023-03-06
  • 1970-01-01
  • 1970-01-01
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-01
  • 1970-01-01
相关资源
最近更新 更多