select语句求救!!!

rebat 2002-02-01 05:42:37
表格式如下:
number bill type
1011 1 a
1011 10 a
1011 10 b
1012 1 a
1013 1 b
1014 1 b

最终结果如下:
number billa billb
1011 11 10
1012 1 0
1013 0 1
1014 0 1

请问各位大虾select语句怎么写?

...全文
120 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
rebat 2002-02-04
  • 打赏
  • 举报
回复
用寒玉的方法通过了,谢谢!
也谢谢其他朋友。

hanyu010 2002-02-01
  • 打赏
  • 举报
回复
ORACLE中通过:
SELECT NUMBER,SUM(BILLA) BILLA,SUM(BILLB) BILLB FROM (
SELECT NUMBER,NVL(SUM(BILL),0) BILLA,0 BILLB FROM TABLENAME WHERE TYPE = 'a' GROUP BY TYPE,NUMBER
UNION
SELECT NUMBER,0 BILLA,NVL(SUM(BILL),0) BILLB FROM TABLENAME WHERE TYPE = 'b' GROUP BY TYPE,NUMBER
) GROUP BY NUMBER ORDER BY NUMBER;
liuyingbo 2002-02-01
  • 打赏
  • 举报
回复
用临时表!!!
蓝天 2002-02-01
  • 打赏
  • 举报
回复
select x.number , x.a billa, y.b billb from (select sum(isnull(bill,0)) a ,number from table1 where type ='a' group by number )x full join
( select sum(isnull(bill,0)) b ,number from table1 where type ='b' group by number )y on x.number=y.number
rebat 2002-02-01
  • 打赏
  • 举报
回复
数据库系统没用了,明天再来,谢谢大家
rebat 2002-02-01
  • 打赏
  • 举报
回复
谢谢大家,我试试…………
net_steven 2002-02-01
  • 打赏
  • 举报
回复
只有两种类型的话,chjcwl(PBVC)写法值得学习。
zztzz 2002-02-01
  • 打赏
  • 举报
回复
http://www.csdn.net/Expert/topic/508/508264.shtm
net_steven 2002-02-01
  • 打赏
  • 举报
回复
sql:
-----------------------------------------------
select a.number,isnull(b.bill,0) as billa,isnull(c.bill,0) as billb
from (select distinct number from yourtable) a
left join (select number,sum(isnull(bill,0)) as bill from yourtable
where type='a' group by number) b
on a.number=b.number
left join (select number,sum(isnull(bill,0)) as bill from yourtable
where type='b' group by number) c
on a.number=c.number
order by a.number
chjcwl 2002-02-01
  • 打赏
  • 举报
回复
不好意思,有点错:
select number,sum(case when type='a' then 0 else bill end ) as billb,
sum(case when type='b' then 0 else bill end ) as billa from asdf
group by number;
chjcwl 2002-02-01
  • 打赏
  • 举报
回复
用什么开发工具?如果是PB只要用交叉表,一个就出来了:
selcet * from afds
行选 number,列选 type ,值选bill,30秒就搞定。
如果是其他就麻烦,两个还好处理,但如果type是动态就一定要建存储过程了。
两个可这样:
select number,sum(case when type='a' then 0 else bill end ) as billa,
sum(case when type='b' then 0 else bill end ) as billb from asdf;
hawkcai 2002-02-01
  • 打赏
  • 举报
回复
select 我不会写,不过这样的视图我会做(假定你的表名字叫aa
create or replace view billa(number,bill)
as select number,sum(bill)
from aa
where aa.type=a
group by number;

create or replace view billb(number,bill)
as select number,sum(bill)
from aa
where aa.type=b
group by number;

create or replace view billab(number,billa,billb)
as select number,bill,0
from billa
union all
select number,0,bill
from billa

create or replace view billab1(number,billa,billb)
as select number,sum(billa),sum(billb)
from billab
group by number;

不知道这样写对不对

linazhu 2002-02-01
  • 打赏
  • 举报
回复
你的billb是怎么得来的?

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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