Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Trying to find whats missing

Author  Topic 

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-02-17 : 12:26:16
Afternoon,

We have a master table with a range of numbers like so:

BegTag EndTag
---------- ----------
3260 3309
3310 3329
3330 3369
3370 3384


I have another table where users data enter the tags into an inventory table:


TagNumber
----------
3620
3621
3633
36122122
3634
2753


I'm trying to find out what tags haven't been entered yet. I tried this but with no luck:

select TagNumber
from dbo.InventoryWipTags
where TagNumber not IN(select *
from dbo.InventoryTagMaster
where PhysInvDate = '2010-02-18'
AND TagNumber between min(BegTag) AND max(EndTag))


I get the error message:


Msg 147, Level 15, State 1, Line 6
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
Msg 116, Level 16, State 1, Line 6
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.


Can anyone think of a slick way to do this that wont yell at me?

Thanks in advance.

Laura

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 12:28:02
you mean the values in range given in first table but not present in second table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-02-17 : 12:32:32
Yes that is correct.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-17 : 12:40:21
thats possible.


SELECT m.BegTag,m.EndTag,m.Val
FROM
(
SELECT t.BegTag,t.EndTag,t.BegTag+ v.number AS Val
FROM Table t
CROSS JOIN master..spt_values v
WHERE v.type='p'
AND t.BegTag+ v.number <=t.EndTag
)m
LEFT JOIN Table2 t2
ON t2.TagNumber=m.Val
WHERE t2.TagNumber IS NULL


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-02-17 : 13:06:43
That got me a lot closer. But I get this message:


BegTag EndTag Val
---------- ---------- -----------
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'asd' to data type int.
Go to Top of Page

mayerl
Yak Posting Veteran

95 Posts

Posted - 2010-02-17 : 13:33:48
Never mind It was an error in the data itself. Thanks for your great suggestion.

Laura
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-18 : 01:11:37
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -