0

抱歉,如果之前有人问过这个问题,我的数据库表结构如下:

AgentName | Sold1 | SoldDate1 | Sold2 | SoldDate2 | Sold3 | SoldDate3
Geoff     | True  | 20/06/2012 | False | Null     | True  | 20/04/2012
Alex      | True  | 13/02/2012 | True  | 26/06/2012 | False | Null

基本上我要做的是计算上个月 Geoff 在 Sold1 中售出的数量,但首先它必须检查 sold1 是否为真,然后对 Sold2 和 Sold3 再次进行同样的检查。

我的目标是让返回的数据按 AgentName 分组并显示为:

AgentName     TotalSold1       TotalSold2      TotalSold3

我希望有人能提供帮助,因为我整天都在寻找解决方案:(

4

1 回答 1

1

table_name 是表的名称

select t0.AgentName, CountSold1, CountSold2, CountSold3 from (
  select distinct AgentName from table_name
) t0 left join (
  select AgentName, count(*) CountSold1 from table_name
  where Sold1 = 'True' and SoldDate1 between '01-JUN-2012' and '30-JUN-2012'
  group by AgentName
) t1 on t0.AgentName = t1.AgentName left join (
  select AgentName, count(*) CountSold2 from table_name
  where Sold2 = 'True' and SoldDate2 between '01-JUN-2012' and '30-JUN-2012'
  group by AgentName
) t2 on t1.AgentName = t2.AgentName left join (
  select AgentName, count(*) CountSold3 from table_name
  where Sold3 = 'True' and SoldDate3 between '01-JUN-2012' and '30-JUN-2012'
  group by AgentName
) t3 on t2.AgentName = t3.AgentName
于 2012-07-03T16:07:41.230 回答