Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
SQL Select items used before a date but not after
Message
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
01635849
Message ID:
01635852
Views:
50
>I am working on a SQL Select where result is parts/items used prior to a certain date but not used after this date. Here is my attempt which does not seem to work
>
>
>select * from part_file where part_pk in (select part_pk from part_used
>where used_date !> '20100101' and used_date < '20100101')
>
>
>How do I need to change the above SQL Select?
select * from dbo.part_file p where not exists (select 1 from dbo.part_used pu where used_date >='20100101' and pu.part_pk = p.part_pk)
Select all parts which were not used after 01-01-2010. (This may return parts which were not used at all)

We can add the other part to this query to make sure the parts were used, e.g.
select * from dbo.part_file p where not exists (select 1 from dbo.part_used pu where used_date >='20100101' and pu.part_pk = p.part_pk)
and exists (select 1 from dbo.part_used pu where used_date <'20100101' and pu.part_pk = p.part_pk)
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform