2

在大型机 DB2 中,我有两个表 TAB 1 和 TAB 2。我在这些表中有如下记录:

选项卡 1:

AP_NBR  LOC_NBR
1000    1
1000    2
1000    3

选项卡 2:

LOC_NBR ITM_ID
1   500
1   600
2   450
2   750

她我需要如下输出:

AP_NBR  COUNT(LOC_NBR)  COUNT(ITM_ID)
1000    3                     4

如何为此编写 COUNT 查询?谁能帮我解决这个问题?

4

2 回答 2

4
select t1.AP_NBR, count(distinct t1.LOC_NBR), count(distinct t2.ITM_ID)
from TAB1 t1 join TAB2 t2 on t1.LOC_NBR = t2.LOC_NBR
group by t1.AP_NBR
于 2012-10-17T08:46:29.330 回答
1

试试这个:

select * from
(select AP_NBR,COUNT(*) cnt_LOC_NBR from TAB1 group by AP_NBR)a,
(select COUNT(*) cnt_ITM_ID from TAB2 where LOC_NBR in 
(select LOC_NBR from TAB1))b
于 2012-10-17T08:48:06.923 回答