If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > joining to a 'fetch first 20' temp table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-18-09, 16:16
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
Question joining to a 'fetch first 20' temp table

1st post:
I have a query that pulls the first 20 rows of instances
(Select id, name from T1 fetch first 20 rows only)

If I pull info from another table against the TEMP table I get 20 rows.
(select TEMP.id, T2.Stuff from TEMP left join T2 on TEMP.ID = T2.ID).

HOWEVER, If I run that join again I get 600 records:

Select id, name, D1.Stuff from TEMP
left join (
select TEMP.id, T2.Stuff from TEMP left join T2 on TEMP.ID = T2.ID
) D1 on TEMP.id = D1.ID

When I remove the 'fetch first 20 rows only' , then I get all row counts the same from TEMP to final pull. Is this ia quirk in DB@ or am I missing something?
Reply With Quote
  #2 (permalink)  
Old 11-18-09, 16:17
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
There are no dups in any of the result sets.
Reply With Quote
  #3 (permalink)  
Old 11-18-09, 16:47
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by CandyMan View Post
am I missing something?
May be you are, but it's hard to tell without seeing the actual query.
Reply With Quote
  #4 (permalink)  
Old 11-18-09, 17:01
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
Similar on delirium.
But, if you have the DB2 V9, you can apply "fetch first 20 rows only" inside of subselects.

Lenny
Reply With Quote
  #5 (permalink)  
Old 11-18-09, 18:36
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
if I take LEFT JOIN SQL from below and insert it just below temp, run TEMP and that section only, I get 20 records.



with TEMP As (
Select
MA.ID
,cast(MA.ACCT_NBR as varchar(16)) ACCTNBR
,MA.AMT
,h.APV_DT
from MyTables.MyAccounts MA
join MyTables.AccountHist H
on MA.ID = h.ID
where
h.APV_DT >= '2009-09-01 00:00:00'
and h.APV_DT < '2009-10-01 00:00:00'
fetch first 20 rows only
)





SELECT
TEMP.ID,
TEMP.AMT,
TEMP.APV_DT,
TEMP.acctnbr
D1.Curr_Bal
FROM temp
LEFT JOIN
(
SELECT
TEMP.ID
, BAL.CreditLine
, int (BAL.Currbal) Curr_Bal
FROM temp
inner JOIN MyTables.AccountBal BAL
ON temp.acctnbr = BAL.acctnbr
and TEMP.APV_DT = BAL.TIME_ID

) D1
on temp.ID = D1.ID
Reply With Quote
  #6 (permalink)  
Old 11-18-09, 22:02
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
So, how many rows does that return?
Code:
SELECT 
 TEMP.ID
 , BAL.CreditLine
 , int (BAL.Currbal) Curr_Bal 
FROM temp
inner JOIN MyTables.AccountBal BAL
 ON temp.acctnbr = BAL.acctnbr
 and TEMP.APV_DT = BAL.TIME_ID
Reply With Quote
  #7 (permalink)  
Old 11-19-09, 11:00
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
600 and no dups. I've never seen this before. If I remove the fetch statement then I get say 1000 records in TEMP and 1000 as the finial record set. The count is the same.
Reply With Quote
  #8 (permalink)  
Old 11-19-09, 11:22
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by CandyMan View Post
600
There you go. Join 600 and 20 records with matching IDs and you get 600 records.
Reply With Quote
  #9 (permalink)  
Old 11-19-09, 11:59
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
No, that is not it. It has to be with the FETCH. and sometihing in memory. I still get 600 records.

If i simplify the SQL to :
with TEMP As (
Select
MA.ID
,MA.AMT
from MyTables.MyAccounts MA
where
fetch first 20 rows only
)

SELECT
TEMP.ID,
TEMP.AMT,
T.ID T_ID
FROM temp
LEFT JOIN
TEMP T
on TEMP.ID = T.ID
Reply With Quote
  #10 (permalink)  
Old 11-19-09, 12:15
Lenny77 Lenny77 is offline
Registered User
 
Join Date: Jul 2009
Location: NY
Posts: 886
If u simplify the SQL to (?):

Quote:
with TEMP As (
Select
ID, AMT
from MyTables.MyAccounts
)

SELECT
t1.ID,
t1.AMT,
T2.ID T_ID
FROM temp t1
LEFT JOIN
TEMP t2
on t1.ID = t2.ID
fetch first 20 rows only


Lenny
Reply With Quote
  #11 (permalink)  
Old 11-19-09, 12:21
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by CandyMan View Post
No, that is not it. It has to be with the FETCH. and sometihing in memory.
If you say so. However, I do not have this problem:
Code:
#cat t.sql
with bigtable (d,l) as (
 select cast(rand() as decimal(10,2)) ,1 from sysibm.sysdummy1 t1
  union all
 select cast(rand() as decimal (10,2)), t.l+1
  from sysibm.sysdummy1 t2, bigtable t
  where  t.l < 50
),
temp (val, key) as (
 select d, l from bigtable fetch first 5 rows only
)
select temp.key, temp.val, t.key 
from temp 
 left join
temp t 
 on t.key = temp.key
;
#db2 -tf t.sql

KEY         VAL          KEY
----------- ------------ -----------
          1         0.00           1
          2         0.65           2
          3         0.30           3
          4         0.67           4
          5         0.10           5

  5 record(s) selected.
Reply With Quote
  #12 (permalink)  
Old 11-19-09, 12:26
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
Memory solution:
rename TEMP to PreTEMP
add: ,with TEMP as(Select ID, AMT from PreTEMP)
Results: 20 records.
Why?
Reply With Quote
  #13 (permalink)  
Old 11-19-09, 12:35
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
Memory solution:
rename TEMP to PreTEMP
add: ,with TEMP as(Select ID, AMT from PreTEMP)
Results: 20 records.
Why?
Reply With Quote
  #14 (permalink)  
Old 11-19-09, 12:44
CandyMan CandyMan is offline
Registered User
 
Join Date: Nov 2009
Posts: 8
Interesting.
My Memory solution:
rename TEMP to PreTEMP
add: ,with TEMP as(Select ID, AMT from PreTEMP)
Results: 20 records.
Go figure
Reply With Quote
Reply

Tags
fetch, subqueries

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On