ORACLE查询,多表关联,分组加条件查询问题.表结构如下.
CREATETABLETEST(ANUMBER,BNUMBER)/INSERTINTOTESTVALUES(1,1);INSERTINTOTESTVALUES(1,1);...
CREATE TABLE TEST(
A NUMBER,
B NUMBER
)
/
INSERT INTO TEST VALUES(1,1);
INSERT INTO TEST VALUES(1,1);
INSERT INTO TEST VALUES(2,2);
INSERT INTO TEST VALUES(2,2);
INSERT INTO TEST VALUES(2,2);
CREATE TABLE TEST_1(
A NUMBER,
C NUMBER,
D NUMBER
)
INSERT INTO TEST_1 VALUES(1,1,3);
INSERT INTO TEST_1 VALUES(2,2,4);
1.TEST和TEST_1是通过A字段来关联.
2.目标结果:查询出TEST.A,条件为,GTOUP BY A , TEST.COUNT(B)大于TEST_1.D - TEST_1.C
中文翻译: TEST表中,以A分组,B的数量大于TEST_1中D和C的差.的所有A的值.
问题稍微有点小绕,不过相信各位大神应该看得懂.悬赏200求解. 展开
A NUMBER,
B NUMBER
)
/
INSERT INTO TEST VALUES(1,1);
INSERT INTO TEST VALUES(1,1);
INSERT INTO TEST VALUES(2,2);
INSERT INTO TEST VALUES(2,2);
INSERT INTO TEST VALUES(2,2);
CREATE TABLE TEST_1(
A NUMBER,
C NUMBER,
D NUMBER
)
INSERT INTO TEST_1 VALUES(1,1,3);
INSERT INTO TEST_1 VALUES(2,2,4);
1.TEST和TEST_1是通过A字段来关联.
2.目标结果:查询出TEST.A,条件为,GTOUP BY A , TEST.COUNT(B)大于TEST_1.D - TEST_1.C
中文翻译: TEST表中,以A分组,B的数量大于TEST_1中D和C的差.的所有A的值.
问题稍微有点小绕,不过相信各位大神应该看得懂.悬赏200求解. 展开
2个回答
展开全部
这跟上一个差不多吧
SELECT *
FROM TEST
WHERE A IN
(SELECT T1.A
FROM (SELECT A, COUNT(B) CNT FROM TEST GROUP BY A) T1, TEST_1 T2
WHERE T1.A = T2.A
AND T1.CNT > (T2.D - T2.C))
更多追问追答
追问
那如果在count(B)的时候,要对B加限制怎么办? 我直接在最里层子查询中加了一个HAVING B IN(1,2)不行.
追答
怎么限制b,像这样吗
SELECT *
FROM TEST
WHERE A IN
(SELECT T1.A
FROM (SELECT A, COUNT(B) CNT
FROM TEST
WHERE B IN (1,2)
GROUP BY A) T1,
TEST_1 T2
WHERE T1.A = T2.A
AND T1.CNT > (T2.D - T2.C))
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询