0

我有下表:

      State           Soccer players     Tennis players
        CT                   0                 0        
        IL                   5                 10
        IN                   3                  8
        MI                  12                 14
        OH                   8                  9
        AR                   2                  2
        KS                   14                 16
        AL                   8                  7
        CA                   1                  13 
        NV                   2                   3

我想形成一个如下所示的输出表

Region         Total_players
East               0
MidWest            60
SouthWest          34
West               29
SouthEast          0

我也试图在没有任何球员的东部地区获得结果。但是,在我的结果集中,我没有得到“东”。

我尝试了以下不产生“东部”区域的查询。

select CASE 
        WHEN STATE IN ('AL','FL','GA','KY','LA','MS','NC','SC','TN') THEN 'SE'
        WHEN STATE IN ('IL','IN','MI','OH','WI') THEN 'MW'
            WHEN STATE IN ('AR','KS','MO','OK','TX') THEN 'SW'
            WHEN STATE IN ('CT') THEN 'E'
            WHEN STATE IN ('CA','NV') THEN 'W'
            ELSE 'Error'
        END AS Region,
        COUNT(*) as Total,
        from players WHERE TRUNC(t.date) >= to_char(to_date(?,'DY MON DD HH24:MI:SS YYYY'),'DD-MON-YYYY') 
                and TRUNC(t.date) <= to_char(to_date(?,'DY MON DD HH24:MI:SS YYYY'),'DD-MON-YYYY')
        GROUP BY ROLLUP(Region) ORDER BY Region
4

3 回答 3

1

如果您在球员表中缺少东部地区,那么您必须从另一个表中获取地区列表,然后将查询结果与左连接。如果您不想创建表格(例如一次性报告),您可以使用 union 和 dual 构建表格,例如:

select region.long_name, region.short_name 
from (
    select 'SouthEast' long_name, 'SE' short_name from dual
    union all
    select 'MidWest' long_name, 'MW' short_name from dual
    union all
    select 'SouthWest' long_name, 'SW' short_name from dual
    union all
    select 'East' long_name, 'E' short_name from dual
    union all
    select 'West' long_name, 'W' short_name from dual
) region

当您创建查询时,您选择该区域的所有行(虚拟表或真实表)并与每个区域的玩家总和左连接,例如:

select 
    region.long_name "Region",
    sum (player.total_players) "Total players"
from (
    select 'SouthEast' long_name, 'SE' short_name from dual
    union all
    select 'MidWest' long_name, 'MW' short_name from dual
    union all
    select 'SouthWest' long_name, 'SW' short_name from dual
    union all
    select 'East' long_name, 'E' short_name from dual
    union all
    select 'West' long_name, 'W' short_name from dual
) region
left join (
    select CASE 
        WHEN STATE IN ('AL','FL','GA','KY','LA','MS','NC','SC','TN') THEN 'SE'
        WHEN STATE IN ('IL','IN','MI','OH','WI') THEN 'MW'
        WHEN STATE IN ('AR','KS','MO','OK','TX') THEN 'SW'
        WHEN STATE IN ('CT') THEN 'E'
        WHEN STATE IN ('CA','NV') THEN 'W'
        ELSE 'Error'
    END region_short_name,
    (players.soccer_players + players.tennis_players) total_players
    from players 
    WHERE TRUNC(t.date) >= to_char(to_date(?,'DY MON DD HH24:MI:SS YYYY'),'DD-MON-YYYY') 
    and TRUNC(t.date) <= to_char(to_date(?,'DY MON DD HH24:MI:SS YYYY'),'DD-MON-YYYY')
) player on player.region_short_name = region.short_name
group by region.long_name
于 2013-05-16T10:09:46.710 回答
0

快速回答:

您评论说东部地区在您的表中肯定有一行,这意味着它没有显示的简单原因是您的 WHERE 子句。删除它,该区域应该重新出现。只有你才能判断该子句是否正确。


根据您对此答案的评论,您需要有一个地区和州表,或者您需要创建一个;哪个没关系。

首先,您需要一个区域唯一的表,这是您在区域级别存储所有信息的地方

create table regions (
    region varchar2(10)
  , ...
  , constraint pk_regions primary key (region)
    ); 

下一个唯一的状态,这是您在状态级别存储所有信息的地方

create table states (
    state  varchar2(20)
  , ...
  , constraint pk_states primary key (state)
    ); 

然后,您应该使用您的信息填充这两个表,并创建第三个表来连接两者。

create table region_states (
    region varchar2(10)
  , state  varchar2(20)
  , constraint pk_region_states primary key (region, state)
  , constraint fk_rs_regions foreign key (region) references regions (region)
  , constraint fx_rs_states foreign key (state) references states (state)
    ); 

接下来,您应该真正STATES从自己的表中获得一个外键。这是可取的,但可能会影响您当前的表,因此请小心移动。

alter table your_table
  add constraint fk_players 
      foreign key (state)
      references states (state)

最后,您加入以REGION_STATES获取信息。

select rs.region, sum(p.soccer_players + p.tennis_players) as total_players
  from players p
  join region_states rs
    on p.state = rs.state
 group by rs.region

也请不要说我使用了 SUM() 而不是 COUNT() ...您想要玩家总数,并且由于数据已经在州级汇总,所以 COUNT() 计算州的数量,而不是数量玩家。

于 2013-05-16T09:37:03.183 回答
0

它将提供所需的输出。无法理解 where 子句中的日期,因为它不在表中。

    select CASE 
    WHEN STATE IN ('AL','FL','GA','KY','LA','MS','NC','SC','TN') THEN 'SE'
    WHEN STATE IN ('IL','IN','MI','OH','WI') THEN 'MW'
        WHEN STATE IN ('AR','KS','MO','OK','TX') THEN 'SW'
        WHEN STATE IN ('CT') THEN 'E'
        WHEN STATE IN ('CA','NV') THEN 'W'
        ELSE 'Error'
    END AS Region,
    sum(soccer_players + tennis_players) as total_players,
    from players
    group by region
于 2013-05-16T09:48:07.293 回答