Table_User
Pid 用户ID
Name 姓名
Age 年龄
--------------------
Table_COst
Pid 用户ID
ORGcode 医院代号
Cost 费用
type 类型 0为门诊 1为住院
Indate 入院日期
1.写条SQL统计费用超过10000的所有pid(注意type)
2.查询凡是有费用记录的个人信息
3一条SQL统计某用户pid 为1的2011年7月(31天)门诊,住院总费用?
Oracle版的可以这么写(当然也有其他写法):
1.写条SQL统计费用超过10000的所有pid(注意type)
[code="sql"]
select pid, decode(type, 0, '门诊', 1, '住院') type, sum(cost)
from table_cost
group by pid, type
having sum(cost) > 1000;
[/code]
2.查询凡是有费用记录的个人信息
[code="sql"]
select distinct a.*
from table_user a, table_cost b
where a.pid = b.pid
and b.cost > 0;
[/code]
3一条SQL统计某用户pid 为1的2011年7月(31天)门诊,住院总费用?
[code="sql"]
select decode(sum(cost), null, 0) cost
from table_cost
where pid = 'whisky'
and indate between to_date('2011/7/1', 'yyyy/mm/dd') and
to_date('2011/7/31', 'yyyy/mm/dd');
[/code]
1.
select pid,sum(cost)
from table_cost
group by pid
having sum(cost)>10000;
2.
select * from table_user a
where exists(select 1 from table_cost b where b.pid=a.pid and b.cost > 0);
3.
select sum(cost)
from table_cost
where pid=1
and indate between to_date('2011-7-1','yyyy-mm-dd')
and to_date('2011-7-31','yyyy-mm-dd');
太简单,我不忍心回答你,你错过了自学成才的机会啊