高手进来帮帮忙,查询问题!30分相送!

GSdeng 2003-10-20 03:24:13
我有如下的一张表:

HINCD NSRIYU JSKSU
-------------------- ---------- ----------
1-771-574-21 13 9
1-771-574-21 0 8

现在我想select出来如下:(相同代码的在同一行)

HINCD JSKSU JSKSU
-------------------- ---------- ----------
1-771-574-21 9 8

可我这样查询:
select a.hincd,a.jsksu,b.jsksu from ns a,ns b where a.hincd=b.hincd and a.hincd='1-771-574-21'

得出的结果如下:

HINCD JSKSU JSKSU
-------------------- ---------- ----------
1-771-574-21 9 9
1-771-574-21 8 9
1-771-574-21 9 8
1-771-574-21 8 8
为什么会多出三行?
如果要得到我想要得结果,应该怎么做?
...全文
55 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
GSdeng 2003-10-25
  • 打赏
  • 举报
回复
你们说的都很相近,但都不行。我前几天仔细想了一下,终于给弄明白了,代码:

select C.hincd,C.jsksu,D.jsksu2 from (select B.hincd,c.jsksu from (select distinct hincd from a) B,(select hincd,sum(jsksu) as jsksu from a where nsriyu=11 or nsriyu=13 group by hincd) C where B.hincd=C.hincd(+)) C,(select hincd,sum(jsksu) as jsksu2 from a where nsriyu=10 or nsriyu=9 group by hincd) D where C.hincd=D.hincd(+)

其中:
select distinct hincd from a //列出代码,他是唯一的
select hincd,sum(jsksu) as jsksu from a where nsriyu=11 or nsriyu=13 group by hincd //统计11,13求和
select hincd,sum(jsksu) as jsksu from a where nsriyu=10 or nsriyu=9 group by hincd //统计10,9求和

其它的你们应该明白吧!



niuzhenjun 2003-10-22
  • 打赏
  • 举报
回复
楼主,以上的缺了一半条件,以下的经试验正确,结帖吧
SELECT Table1.HINCD as HINCD,Table1.you as you, Table2.cha as cha FROM
((SELECT ns.HINCD, sum(ns.JSKSU) as you ,0 as cha FROM ns WHERE ns.NSRIYU in (11,13) group by ns.HINCD) Table1) left join
((SELECT ns.HINCD, 0 as you, sum(ns.JSKSU) as cha FROM ns WHERE ns.NSRIYU in (9,10) group by ns.HINCD) Table2)
ON Table1.HINCD = Table2.HINCD
union
select Table2.HINCD as HINCD,Table2.you as you, Table2.cha as cha FROM
((SELECT ns.HINCD, 0 as you, sum(ns.JSKSU) as cha FROM ns WHERE ns.NSRIYU in (9,10) group by ns.HINCD) Table2) where table2.HINCD not in (SELECT ns.HINCD FROM ns WHERE ns.NSRIYU in (11,13))
niuzhenjun 2003-10-22
  • 打赏
  • 举报
回复
楼主,为什么不早些把问题说全,害的我忙了半天,给你sql,绝对正确,不过字段名我给改了,因为大小写是同一个字段,不允许。

SELECT Table1.HINCD,Table1.you, Table2.cha FROM
((SELECT ns.HINCD, sum(ns.JSKSU) as you ,0 as cha FROM ns WHERE ns.NSRIYU in (11,13) group by ns.HINCD) Table1) LEFT JOIN
((SELECT ns.HINCD, 0 as you, sum(ns.JSKSU) as cha FROM ns WHERE ns.NSRIYU in (9,10) group by ns.HINCD) Table2)
ON Table1.HINCD = Table2.HINCD;
ayane 2003-10-22
  • 打赏
  • 举报
回复
他有一个求和的要求了
niuzhenjun 2003-10-22
  • 打赏
  • 举报
回复
这个应该没有问题:
select table1.HINCD,table1.JSKSU,table2.JSKSU from
((SELECT ns.HINCD, ns.JSKSU FROM ns WHERE ns.NSRIYU in (11,13)) table1) ,
((SELECT ns.HINCD, ns.JSKSU FROM ns WHERE ns.NSRIYU in (9,10)) table2)
where table1.HINCD = table2.HINCD
Cocoky 2003-10-21
  • 打赏
  • 举报
回复
有没有主关键字啊!
会不会有相同字段
Cocoky 2003-10-21
  • 打赏
  • 举报
回复
你这是全连接,当然会多出记录!
ayane 2003-10-21
  • 打赏
  • 举报
回复
我想的用上你判断优差的那个字段
比如说

Select a.hincd,b.jsksu,c.jskksu
from ns a, ns b
where a.hincd=b.hincd
and a.hincd='1-771=576-21'
and a.kind<>b.kind

应该就能得出两个结果
然后将两个结果排一下序
取First()就好了

GSdeng 2003-10-21
  • 打赏
  • 举报
回复
你们发现没有:
表temp里面本来就有两行1-771-574-21,9与1-771-574-21,8。我们用的语句:
select a.hincd,a.jsksu,b.jsksu from ns a,ns b where a.hincd=b.hincd and a.hincd='1-771-574-21'
本身就会返回两行!你们的结果都不是我想要的结果,我想让同种编号的两种情况都反映在同一记录上!就是:
1-771-574-21,8,9
里面并不存在最大值与最小值的问题!
GSdeng 2003-10-21
  • 打赏
  • 举报
回复
HINCD NSRIYU JSKSU
----------------------------------
1-771-574-21 13 9
1-333-333-01 11 8
1-771-574-21 10 9
1-771-574-21 11 8
1-333-333-01 13 4

其实,13,11分别代表优秀、良,我把他放在一起是为了统一作为“好”处理,10、9分别代表较差,差握,我把他放在一起是为了统一作为“差”处理,我想要的结果:

HINCD jsksu JSKSU
----------------------------------
1-771-574-21 17 9
1-333-333-01 12

jsksu是13、11的总和,JSKSU是10的总和。
相同代码不能出现两次。
niuzhenjun 2003-10-21
  • 打赏
  • 举报
回复
楼主怎么不早说有一个字段(nsrisu)是表示是”优“还是”差“。
以下假设字段(nsrisu)为数字型字段,1表示”优“,2表示”差“。
查询如下:
select table1.HINCD,table1.JSKSU,table2.JSKSU from
((SELECT ns.HINCD, ns.JSKSU FROM ns WHERE ns.nsrisu=1) table1) ,
((SELECT ns.HINCD, ns.JSKSU FROM ns WHERE ns.nsrisu=2) table2)
where table1.HINCD = table2.HINCD
aliker 2003-10-20
  • 打赏
  • 举报
回复
去掉相同项
select a.hincd,a.jsksu,b.jsksu from ns a,ns b where a.hincd=b.hincd and a.jsksu <> b.jsksu and a.hincd='1-771-574-21'
加上a.jsksu <> b.jsksu即可
GSdeng 2003-10-20
  • 打赏
  • 举报
回复
我并不是想取最大值和最小值,你的结果还是一样,我的意思是:9是优的数量;8是差的数量。我要他显示在同一行!其实表里还有一个字段(nsrisu)是表示是”优“还是”差“。
thp 2003-10-20
  • 打赏
  • 举报
回复
上面少了个单引号:
Select a.hincd,b.jsksu,c.jskksu
from (Select DISTINCT hincd from ns) a,
(select hincd,max(jsksu) as jsksu from ns) b,
(select hincd,min(jsksu) as jsksu from ns) c
where a.hincd=b.hincd
and a.hincd=c.hincd
and a.hincd='1-771=576-21'
thp 2003-10-20
  • 打赏
  • 举报
回复
Select a.hincd,b.jsksu,c.jskksu
from (Select DISTINCT hincd from ns) a,
(select hincd,max(jsksu) as jsksu from ns) b,
(select hincd,min(jsksu) as jsksu from ns) c
where a.hincd=b.hincd
and a.hincd=c.hincd
and a.hincd='1-771=576-21

没有测试,应该可以的。

1,178

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder 数据库及相关技术
社区管理员
  • 数据库及相关技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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