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 2000 Forums
 Transact-SQL (2000)
 Help with subquery!

Author  Topic 

henrikf
Starting Member

7 Posts

Posted - 2006-02-24 : 17:34:00
Hello again!

I working with a table containing sales data and I need help finding some distinct customer orders in it. The table contains one row for each order line, giving several lines for each unique order (if more than one item is bought at that time).Each order line has a numeric status field with indicates if the line has been delivered or not.

So, my problem is that I want to find every unique customer order where ALL order lines have been delivered (=status 20). Order lines that are not delivered have status = 10.

Sample data:

OrderNo Item Status
-------------------------------
1010........Paper....20
1010........Pencil...20
1010........Staple...20
1011........Paper....20
1011........Brusch...20
1011........Bag......10

Only the order 1010 should be returned by the query.

Thanks!

//Henrik

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-24 : 18:56:08
There's probably a more efficient solution, but here's what I came up with late in the day:


SELECT OrderNo, Item, Status
FROM Table1 t1
WHERE
Status = 20 AND
OrderNo NOT IN
(
SELECT OrderNo
FROM Table1
WHERE Status = 10
)


Tara Kizer
aka tduggan
Go to Top of Page

henrikf
Starting Member

7 Posts

Posted - 2006-02-24 : 20:03:04
Thanks!

This workes out just fine if I'm only interested in one status. But, lets say that I'm interested in finding orders with status 19 and 20 and not the other orders, for which the statuses are unknown (lets asume they can have over 1000 different statuses and I'm unwilling to type them in the NOT IN part of the script...

//Henrik
Go to Top of Page

Srinika
Master Smack Fu Yak Hacker

1378 Posts

Posted - 2006-02-24 : 23:30:10
Now ur Q is different.

If u don't get the answer from the following, give some more sample data and expected results

u may use
... IN part .....
or u may use
...(Status = 19 or Status = 20) ....
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-25 : 13:55:40
Henrik, you'll need to provide more data then plus what you expect to return.

Tara Kizer
aka tduggan
Go to Top of Page

VIG
Yak Posting Veteran

86 Posts

Posted - 2006-02-25 : 16:15:30

declare @t table(OrderNo int, Item varchar(10),Status int)
insert @t
select 1010,'Paper',20 union
select 1010,'Pencil',20 union
select 1010,'Staple',20 union
select 1011,'Paper',20 union
select 1011,'Brusch',20 union
select 1011,'Bag',10 union
select 1012,'Paper',19 union
select 1012,'Bag',19 union
select 1012,'Staple',19
select 1013,'Paper',18 union
select 1013,'Bag',18 union
select 1013,'Staple',18

select OrderNo,min(status) Status
from @t t
group by OrderNo having min(t.status)=max(t.status) and
min(status) in (19,20)
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2006-02-25 : 19:48:50
quote:
Originally posted by henrikf

Thanks!

This workes out just fine if I'm only interested in one status. But, lets say that I'm interested in finding orders with status 19 and 20 and not the other orders, for which the statuses are unknown (lets asume they can have over 1000 different statuses and I'm unwilling to type them in the NOT IN part of the script...

//Henrik


You should really ask your whole question in your first post. No one likes to spend time answering a question, and then get a "Yes, that works, but my real need is..."

This code should do it.


select
t1.OrderNo,
t1.Item,
t1.Status
from
Table1 t1
where
t1.Status in ( 19, 20 ) and
t1.OrderNo not in
(
select
t2.OrderNo
from
Table1 t2
where
t2.OrderNo = t1.OrderNo
t2.Status not in ( 19, 20 )
)


CODO ERGO SUM
Go to Top of Page

henrikf
Starting Member

7 Posts

Posted - 2006-02-28 : 11:46:51
Hi Michael!

Sorry for letting u heroes think an extra time...I understand it can be boring to try to help and then get involved in some never ending problem. I think I didn’t have the problem all clear to myself. Anyway, all your input gave the help I needed!

Many thanks!

//Henrik

Go to Top of Page
   

- Advertisement -