Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This should be a simply query, but its driving me crazy 1

Status
Not open for further replies.

hoggle

Programmer
Jul 13, 2001
124
US
I have 3 tables(#tblMan, #tblFin, #tblAdd) all of them have the same attributes (ItemNum, totQty, ManagerNum, BasePrice)

what I want to do is first put all the records from #tblMan into #tblAdd (no problems there :) )

then I need to get all the items that are in #tblFin into #tblAdd, if they are not already there (from #tblMan). Pkey is ItemNum.

I've tried
====================================
insert into #tblAdd
( ItemNum,
totQty,
ManagerNum,
BasePrice)
select F.ItemNum,
F.totQty,
F.ManagerNum,
F.BasePrice
from #tblFin F, #tblMan M
where
F.ManagerNum = '0004'
and F.ItemNum not in (select ItemNum from #tblMan)


I've also tried
====================================
insert into #tblAdd
( ItemNum,
totQty,
ManagerNum,
BasePrice)
select F.ItemNum,
F.totQty,
F.ManagerNum,
F.BasePrice
from #tblFin F, #tblMan M
where
F.ManagerNum = '0004'
and F.ItemNum <> M.ItemNum


=============================================

in tblMan I have 96 records
in tblFin I have 128 (most of which are already in tblMan)
when I run the above query I get 6336 into tblAdd
at the most it would be 224 (but should be more like 150)
 
Hi,

Try this query, UNION shd eliminate all the duplicates. so u shd disticnt recs from both tbl's

insert into #tblAdd
( ItemNum,
totQty,
ManagerNum,
BasePrice)
select ItemNum,
totQty,
ManagerNum,
BasePrice
from #tblFin
UNION
select ItemNum,
totQty,
ManagerNum,
BasePrice
from #tblman

Hope it helps

Sunil
 
Code:
nsert into #tblAdd
(    ItemNum,
    totQty,
    ManagerNum,
    BasePrice)
select     F.ItemNum,
    F.totQty,
    F.ManagerNum,
    F.BasePrice
from #tblFin F
where
F.ManagerNum = '0004'
and F.ItemNum not in (select ItemNum from #tblMan)
 
I spaced it for a second, I just selected distinct on the tblFin and everything works.
thanks sunila7
 
you need to join the tables and create an unmatched query

something like
insert into #tblAdd
( ItemNum,
totQty,
ManagerNum,
BasePrice)
select F.ItemNum,
F.totQty,
F.ManagerNum,
F.BasePrice
from #tblFin F
left outer join #tblAdd A on
f.itemNum = A.itemnum

where
A.ManagerNum = '0004'
and A.ItemNum is null
 
Use this, just remove #tblMan from your select:
insert into #tblAdd
( ItemNum,
totQty,
ManagerNum,
BasePrice)
select F.ItemNum,
F.totQty,
F.ManagerNum,
F.BasePrice
from #tblFin F
where
F.ManagerNum = '0004'
and F.ItemNum not in (select ItemNum from #tblMan)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top