在线急等啊

mrzhongke 2012-04-26 10:05:11
现在有一个table 字段如下

seq,id,id1,name,age,sex,is_default

1 1 2 aa 18 F Y
1 1 3 A 19 M N
2 2 4 2 12 F N
2 2 5 A A15 F N


有四条数据,我想实现的是只进来两个参数和seq,id 对比。比如传进来是1,1那就有两条数据。现在要求是如果is_default这个字段是Y,那就只输出这条数据,如果is_default是N,比如说是传进来2,2,那就是两条数据,随便返回一条。那请问这个该怎么写呢?
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
报什么错?


既然是求查询出的数量 表里面就不用再排序了 耗时间
mrzhongke 2012-04-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
select * from table where seq=? and id =? and decode(is_default,'Y',1,rownum)=1
[/Quote]
如果条件为空的话,我想的结果是
1 1 2 aa 18 F Y

2 2 4 2 12 F N
2 2 5 A A15 F N
这两个其中一个,该怎么写呢?
loveofmylife 2012-04-27
  • 打赏
  • 举报
回复
select * from table where seq=? and id =? and decode(is_default,'Y',1,rownum)=1
wxdfire 2012-04-27
  • 打赏
  • 举报
回复
select count(1) as totalrow
from (select t2.onenum,
t2.allnum,
t2.printstatus,
t.apfd_code,
t2.gidl_doc_numb,
t2.orgid,
t.bdgt_subj,
t3.subjectcode,
t3.subjectname,
case
when t.acst_id not in
(887209, 2001, 3001, 4001, 5001, 7001, 8001, 6066768) then
decode(t2.fund_char,
'0',
'预算内',
'1',
'其他财政性资金',
'2',
'其他',
'3',
'预算内(往年)',
'4',
'其他财政性资金(往年)',
'5',
'其他(往年)')
else
'其他'
end as fund_char,
t2.pay_amnt,
t2.dtal_id,
t2.item,
t4.name,
t.gath_org_name,
t2.smry smry,
t.acst_id
from ga_t_apfd_bill t,
sysmgr_org t1,
ga_t_apfd_bill_dtal t2,
busmgr_funnew t3,
sysmgr_user t4
where 1 = 1
and t.gath_org_id = t1.orgid(+)
and t.bdgt_subj = t3.subjectcode(+)
and t.apfd_user_id = t4.userid(+)
and t2.apfd_bill_id = t.apfd_bill_id
and t.year = 2012
and t3.year = 2012
and t.ACST_ID = 1001
and t.is_Del = 0
and t.status in ('4', '5')
and t2.gidl_doc_numb like % '414%'
order by t1.orgcode, t.bdgt_subj, to_number(t.APFD_CODE))

这段哪儿错了??
mrzhongke 2012-04-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
你要写函数还是存储过程?
v_seq int;
v_id int;

v_sql := 'select seq,id,id1,name,age,sex,is_default,0 as xh from table where seq='||v_seq || ' and id='||v_id || ' and default=''Y'' union select seq,id,id1,name……
[/Quote]存储过程,这里seq,id是PK,seq,id相同时只能有一个Y,这时该怎么写呢?
UPC子夜 2012-04-26
  • 打赏
  • 举报
回复
你要写函数还是存储过程?
v_seq int;
v_id int;

v_sql := 'select seq,id,id1,name,age,sex,is_default,0 as xh from table where seq='||v_seq || ' and id='||v_id || ' and default=''Y'' union select seq,id,id1,name,age,sex,is_default,1 as xh from table where seq='||v_seq || ' and id='||v_id || ' and default=''N'' order by xh';

这样写sql的话,可以保证为Y的全在前面,为N的全在后边,然后通过rownum=1取第一条,如果有多个为Y的,那么取第一个,如果只有一个为Y的,正好,如果没有Y,只有N的,那么随便取一条

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧