紧急!!!高手请进!关于一个SQL语句!

gzwilliam 2004-12-17 12:21:21
select DISTINCT
ZBTSI_D809.WF_TEIGI_ID,
ZBTSI_D809.HAN_SU,
ZBTSI_D809.TEKIYO_KAISHI_YMD,
ZBTSI_D809.TEKIYO_SHURYO_YMD,
ZBTSI_D809.SHINSEISHO_SHUBETSU,
ZBTSI_D809.SHINSEISHO_FORMAT_ID,
ZBTSI_D809.WF_NM_N,
ZBTSI_D809.WF_SETSUMEI,
ZBTSI_D809.SAKUJO_SGN

from ZBTSI_D809,ZBTSI_D810,ZBTSI_D814,ZBTSI_D825,ZBTSI_D826

where
ZBTSI_D809.KAISHA_CD = '000'
and ZBTSI_D809.SHINSEISHO_SHUBETSU = 'S4'
and ZBTSI_D810.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.WF_TEIGI_ID = ZBTSI_D809.WF_TEIGI_ID
and ZBTSI_D810.HAN_SU = ZBTSI_D809.HAN_SU
and To_Date(ZBTSI_D809.TEKIYO_KAISHI_YMD, 'YYYY-MM-DD') <= SYSDATE
and To_Date(ZBTSI_D809.TEKIYO_SHURYO_YMD, 'YYYY-MM-DD') >= SYSDATE
and ZBTSI_D814.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D814.WF_SHAIN_ID = '0000010494344'
and ZBTSI_D825.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D825.KAISHA_CD = ZBTSI_D814.KAISHA_CD
and ZBTSI_D826.BUMON_CD = ZBTSI_D825.BUMON_CD
and ZBTSI_D826.WF_SHAIN_ID = ZBTSI_D814.WF_SHAIN_ID
and ZBTSI_D826.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.BUMON_CD = substrb(ZBTSI_D825.SOSHIKI_CD,22,6)

上面的SQL语句可以正确运行。可后来要求当ZBTSI_D809.WF_TEIGI_ID相同时只取得ZBTSI_D809.HAN_SU最大值所对应的纪录。所以我采用了将纪录按ZBTSI_D809.KAISHA_CD和ZBTSI_D809.WF_TEIGI_ID分组然后取ZBTSI_D809.HAN_SU最大值所对应的纪录的思路。写出如下SQL语句:
select DISTINCT
ZBTSI_D809.WF_TEIGI_ID,
MAX(ZBTSI_D809.HAN_SU),
ZBTSI_D809.TEKIYO_KAISHI_YMD,
ZBTSI_D809.TEKIYO_SHURYO_YMD,
ZBTSI_D809.SHINSEISHO_SHUBETSU,
ZBTSI_D809.SHINSEISHO_FORMAT_ID,
ZBTSI_D809.WF_NM_N,
ZBTSI_D809.WF_SETSUMEI,
ZBTSI_D809.SAKUJO_SGN


from ZBTSI_D809,ZBTSI_D810,ZBTSI_D814,ZBTSI_D825,ZBTSI_D826

where
ZBTSI_D809.KAISHA_CD = '000'
and ZBTSI_D809.SHINSEISHO_SHUBETSU = 'S4'
and ZBTSI_D810.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.WF_TEIGI_ID = ZBTSI_D809.WF_TEIGI_ID
and ZBTSI_D810.HAN_SU = ZBTSI_D809.HAN_SU
and To_Date(ZBTSI_D809.TEKIYO_KAISHI_YMD, 'YYYY-MM-DD') <= SYSDATE
and To_Date(ZBTSI_D809.TEKIYO_SHURYO_YMD, 'YYYY-MM-DD') >= SYSDATE
and ZBTSI_D814.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D814.WF_SHAIN_ID = '0000010494344'
and ZBTSI_D825.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D825.KAISHA_CD = ZBTSI_D814.KAISHA_CD
and ZBTSI_D826.BUMON_CD = ZBTSI_D825.BUMON_CD
and ZBTSI_D826.WF_SHAIN_ID = ZBTSI_D814.WF_SHAIN_ID
and ZBTSI_D826.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.BUMON_CD = substrb(ZBTSI_D825.SOSHIKI_CD,22,6)
GROUP BY ZBTSI_D809.KAISHA_CD, ZBTSI_D809.WF_TEIGI_ID

运行后报ORA_00979:not a GROUP BY expression错误。
请问谁有正确的解决办法啊。不会的兄弟请帮忙顶!谢谢啦!
...全文
86 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
yxxx 2004-12-17
我眼晕,帮你顶好了
回复
ATGC 2004-12-17
因为你用了MAX(ZBTSI_D809.HAN_SU),
所以select语句中剩下的字段都要在group by里
不然,就会出现这个错误
太复杂的条件了。。。
回复
gzwilliam 2004-12-17
按如下方法解决了,谢谢各位捧场!

select DISTINCT
ZBTSI_D809.KAISHA_CD,
ZBTSI_D809.WF_TEIGI_ID,
ZBTSI_D809.HAN_SU,
ZBTSI_D810.BUMON_CD,
ZBTSI_D809.TEKIYO_KAISHI_YMD,
ZBTSI_D809.TEKIYO_SHURYO_YMD,
ZBTSI_D809.SHINSEISHO_SHUBETSU,
ZBTSI_D809.SHINSEISHO_FORMAT_ID,
ZBTSI_D809.WF_NM_N,
ZBTSI_D809.WF_SETSUMEI,
ZBTSI_D809.SAKUJO_SGN,
ZBTSI_D814.WF_SHAIN_ID

from ZBTSI_D809,ZBTSI_D810,ZBTSI_D814,ZBTSI_D825,ZBTSI_D826,
(select KAISHA_CD,
WF_TEIGI_ID,
MAX(HAN_SU) as HAN_SU from ZBTSI_D809
group by KAISHA_CD,
WF_TEIGI_ID) m

where
ZBTSI_D809.KAISHA_CD = '000'
and ZBTSI_D809.SHINSEISHO_SHUBETSU = 'S4'
and ZBTSI_D809.SAKUJO_SGN = '0'
and ZBTSI_D809.WF_KAKUTEI_SGN = '1'
and ZBTSI_D810.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.WF_TEIGI_ID = ZBTSI_D809.WF_TEIGI_ID
and ZBTSI_D810.HAN_SU = ZBTSI_D809.HAN_SU
and To_Date(ZBTSI_D809.TEKIYO_KAISHI_YMD, 'YYYY-MM-DD') <= SYSDATE
and To_Date(ZBTSI_D809.TEKIYO_SHURYO_YMD, 'YYYY-MM-DD') >= SYSDATE
and ZBTSI_D814.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D814.WF_SHAIN_ID = '0000010494344'
and ZBTSI_D825.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D825.KAISHA_CD = ZBTSI_D814.KAISHA_CD
and ZBTSI_D826.BUMON_CD = ZBTSI_D825.BUMON_CD
and ZBTSI_D826.WF_SHAIN_ID = ZBTSI_D814.WF_SHAIN_ID
and ZBTSI_D826.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.BUMON_CD = substrb(ZBTSI_D825.SOSHIKI_CD,22,6)
and ZBTSI_D809.KAISHA_CD(+) = m.KAISHA_CD
and ZBTSI_D809.WF_TEIGI_ID(+) = m.WF_TEIGI_ID
and ZBTSI_D809.HAN_SU(+) = m.HAN_SU
回复
日出地上晋 2004-12-17
應該所有與分組函數一起再選擇列表中的字段在group by中要列出來
回复
ORARichard 2004-12-17
select DISTINCT
ZBTSI_D809.WF_TEIGI_ID,
ZBTSI_D809.HAN_SU,
ZBTSI_D809.TEKIYO_KAISHI_YMD,
ZBTSI_D809.TEKIYO_SHURYO_YMD,
ZBTSI_D809.SHINSEISHO_SHUBETSU,
ZBTSI_D809.SHINSEISHO_FORMAT_ID,
ZBTSI_D809.WF_NM_N,
ZBTSI_D809.WF_SETSUMEI,
ZBTSI_D809.SAKUJO_SGN

from ZBTSI_D809,ZBTSI_D810,ZBTSI_D814,ZBTSI_D825,ZBTSI_D826

where
ZBTSI_D809.KAISHA_CD = '000'
and ZBTSI_D809.SHINSEISHO_SHUBETSU = 'S4'
and ZBTSI_D810.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.WF_TEIGI_ID = ZBTSI_D809.WF_TEIGI_ID
and ZBTSI_D810.HAN_SU = ZBTSI_D809.HAN_SU
and To_Date(ZBTSI_D809.TEKIYO_KAISHI_YMD, 'YYYY-MM-DD') <= SYSDATE
and To_Date(ZBTSI_D809.TEKIYO_SHURYO_YMD, 'YYYY-MM-DD') >= SYSDATE
and ZBTSI_D814.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D814.WF_SHAIN_ID = '0000010494344'
and ZBTSI_D825.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D825.KAISHA_CD = ZBTSI_D814.KAISHA_CD
and ZBTSI_D826.BUMON_CD = ZBTSI_D825.BUMON_CD
and ZBTSI_D826.WF_SHAIN_ID = ZBTSI_D814.WF_SHAIN_ID
and ZBTSI_D826.KAISHA_CD = ZBTSI_D809.KAISHA_CD
and ZBTSI_D810.BUMON_CD = substrb(ZBTSI_D825.SOSHIKI_CD,22,6)

把你这个查询当做 tb1,字段自己做个对应。结果就应该有了。
回复
ORARichard 2004-12-17
给你个例子参考

SQL> select * from tb1;

ID A B
--------- - --
1 a 4
2 a 3
3 b 2
4 c 1
5 a 0

SQL> select max(id),a from tb1 group by a;

MAX(ID) A
--------- -
5 a
3 b
4 c

SQL> select tb1.* from tb1,(select max(id) id,a from tb1 group by a) tb2 where tb1.id=tb2.id;

ID A B
--------- - --
3 b 2
4 c 1
5 a 0

注意分组中不含B字段,但结果包含了所有的字段。
回复
gzwilliam 2004-12-17
我知道,但如果其他的字段都在group by里的话就实现不了目的了
回复
相关推荐
发帖
基础和管理
创建于2007-09-28

1.7w+

社区成员

Oracle 基础和管理
申请成为版主
帖子事件
创建了帖子
2004-12-17 12:21
社区公告
暂无公告