首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

?SQL?

2012-01-23 
????SQL求助????select*frombbcifobject_id( bbc )isnotnulldroptablebbcgocreatetablebbc(namenvarchar(5

????SQL求助????
select   *   from   bbc

if   object_id( 'bbc ')   is   not   null
drop   table   bbc
go
create   table   bbc   (name   nvarchar(50),region   nvarchar(50),area   bigint,population   bigint,gdp   bigint)
insert   bbc   select   'Albania ', 'Europe ',28728,3200000,6656000000
union   all   select   'Algeria ', 'Middle   East ',2400000,32900000,75012000000
union   all   select   'Angola ', 'Africa ',1250000,14500000,14935000000
union   all   select   'Antigua   and   Barbuda ', 'Americas ',442,77000,770000000
union   all   select   'Argentina ', 'Americas ',2800000,39300000,146196000000
union   all   select   'Armenia ', 'Europe ',29743,3000000,3360000000
union   all   select   'Australia ', 'Asia-Pacific ',7700000,20300000,546070000000
union   all   select   'Austria ', 'Europe ',83871,8100000,261630000000
union   all   select   'Bahamas ', 'Americas ',13939,32100000,4789320000
union   all   select   'Eoomp ', 'Milld ',500015,0,0
union   all   select   'Colmoomp ',Europe, '562015 ',6233,866200000

--name     国家名称
--region   地区
--population   人口数
--gdp     GDP生产总值
--显示每个地区以及该地区国家总人口不少于1000千万的国家总数


--   给出地区中所有国家的人口总数为0的地区.  


--有些国家的人口数比她的周边国家(周边国家指在同一地区的国家)要多三倍,列出这些国家和地区.  

第一问题的结果(我要的结果)
region                           数量
--------------       --------
Africa                           1
Americas                       2                                                                            
Asia-Pacific               1
Middle   East                 1

[解决办法]
select region,count(1),sum(population)
from bbc
where population> =10000000
group by region
order by region

---

Africa   1  14500000
Americas   2  71400000
Asia-Pacific  1  20300000
Middle East  1  32900000

[解决办法]
1、
select region,sum(case when population> 10000000 then 1 else 0 end) from bbc group by region
[解决办法]
--显示每个地区以及该地区国家总人口不少于1000千万的国家总数
理解的
select region,
sum(1) as 地区国数,
sum(case when population> =10000000 then 1 else 0 end) as 总人口不少于1千万的国家总数
from bbc
group by region

你要的
select region,
count(*) as 数量
from bbc
where population> =10000000
group by region

ps:应该是1千万,不是1000千万

[解决办法]
-- 给出地区中所有国家的人口总数为0的地区.
select region
from bbc


group by region
having sum(population)=0


[解决办法]

select region,数量=count(*) from bbc
where population> 10000000
group by region

--result
region 数量
-------------------------------------------------- -----------
Africa 1
Americas 2
Asia-Pacific 1
Middle East 1

(4 row(s) affected)
[解决办法]
--有些国家的人口数比她的周边国家(周边国家指在同一地区的国家)要多三倍,列出这些国家和地区.
select * from bbc a
where exists (
select 1 from bbc
where region=a.region
and population*3 <a.population
)

[解决办法]
cefriend(青草) ( ) 信誉:100 Blog 2007-01-31 17:28:43 得分: 0


晕,第一题这么简介的语句,


------------------
哈哈,是啊,一开始刚才那个帖子给想复杂了

热点排行
Bad Request.