union的应用

You have just become the database manager for your bookie. He keeps

records on horse races for statistical purposes, and his basic table looks
like this:
 
CREATE TABLE RacingResults
(track_id CHAR(3) NOT NULL,
race_date DATE NOT NULL,
race_nbr INTEGER NOT NULL,
win_name CHAR(30) NOT NULL,
place_name CHAR(30) NOT NULL,
show_name CHAR(30) NOT NULL,
PRIMARY KEY (track_id, race_nbr_date, race_nbr));
 
   The track_id column is the name of the track where the race was
held, race_date is when it was held, race_nbr is number of the each
race, and the other three columns are the names of the horses that won,
placed, or showed for that race. If you do not know these terms, won
means that the horse was in first place; placed means that the horse was
in first or second place, and showed means the horse was in first,
second, or third place.
   Your bookie comes to you one day and wants to know how many
times each horse was in the money. What  SQL query do you write for
this?
sql like this:
select a.win_name, count(a.*) from RacingResults r,    
  (select win_name from RacingResults   
    union   
   select place_name from RacingResults   
    union   
   select show_name from RacingResults)   
    as a   
  where r.win_name = a.win_name    
   or    
  r.place_name =a.win_name   
   or   
  r.show_name = a.win_name   
 
group by (a.win_name);  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值