下表是REORGANIZED表
TID ITEMS TIMES TWU
1 D 5 633
1 M 5 665
1 R 14 861
1 F 4 871
1 I 8 910
1 A 7 942
1 N 7 950
1 Z 2 986
1 H 2 1020
2 S 4 551
2 R 7 861
2 F 6 871
2 I 4 910
2 A 6 942
2 N 8 950
2 Z 6 986
2 H 2 1020
3 U 4 354
3 V 7 528
3 B 2 641
3 J 4 842
3 F 4 871
3 I 2 910
3 A 6 942
3 N 2 950
3 Z 4 986
4 X 4 338
4 O 2 442
4 D 2 633
4 B 6 641
4 M 1 665
4 F 5 871
4 A 1 942
4 N 7 950
4 Z 10 986
4 H 1 1020
5 T 5 365
5 C 8 370
5 K 7 397
5 Q 5 397
5 P 5 471
5 S 3 551
5 D 1 633
5 B 6 641
5 M 6 665
5 J 4 842
5 R 6 861
5 I 1 910
5 A 4 942
5 Z 10 986
5 H 7 1020
6 L 5 305
6 U 1 354
6 K 2 397
上表按升序排序。我认为twu的项目的最小值为每个TID的叶子节点。剩余项目是中间节点。下表是LEAFNODES
TID ITEMS
1 D
2 S
3 U
4 X
5 T
6 L
现在我想选择LEAFNODES中的ITEMS作为重组的中间节点
答案 0 :(得分:0)
要获取另一个表中不存在的记录,请使用NOT EXISTS。您想知道是否存在中间记录。您考虑所有记录中没有中间记录的最小记录。因此,所有具有比最小值更高的twu的记录都是中间记录,并且您希望从非中间的叶子节点中选择所有项目。
select *
from leafnodes
where not exists
(
select *
from reorganized
where twu >
(
select min(twu)
from reorganized leaf
where leaf.tid = reorganized.tid
)
and reorganized.items = leafnodes.items
);
这与IN子句相同,我认为它更具可读性。在这里,您将中间项视为一个集合,并且您希望项目不是该集合。
select *
from leafnodes
where items not in
(
select items
from reorganized
where twu >
(
select min(twu)
from reorganized leaf
where leaf.tid = reorganized.tid
)
);
答案 1 :(得分:0)
select * from LEAFNODES
Excluse
(with temp as
(select TID , min(TWU) as TWU from LEAFNODES )
select TID , Items from REORGANISED as a inner join temp on a.TID=temp.TID and a.TWU=Temp.TWU))
答案 2 :(得分:0)
;WITH CTE1 AS
(
-- Select the TId with minimum value from REORGANISED
SELECT TID,MIN(TWU)TWU
FROM REORGANISED
GROUP BY TID
)
,CTE2 AS
(
-- Now we will get value(ITEMS) for the min value and TID from CTE1 for
SELECT C1.TID,R.ITEMS,C1.TWU
FROM CTE1 C1
JOIN REORGANISED R ON C1.TID=R.TID AND C1.TWU=R.TWU
)
SELECT L.*
FROM CTE2 C2
JOIN LEAFNODES L ON C2.TID=L.TID AND C2.ITEMS=L.ITEMS