SQL Server 2000
I have to write a stored procedure:
SELECT
Customers.SalesRep,
Jobs.JNr,
Jobs.JNr,
Mov.Type,
Mov.Date
FROM
Customers
INNER JOIN Jobs on Customers.Acc=Jobs.Acc
INNER JOIN JobsMov on Jobs.JNr=JobsMov.JNr
INNER JOIN Mov on JobsMov.MNr=Mov.MNr
WHERE
Customers.SalesRep=”Ara”
AND Jobs.JNr not in (‘54’,’49’)
AND Mov.Type<>’imp’
AND Jobs.JNr in
(
SELECT
FROM Ch
WHERE
Substring(Ch.Cost,4,7)=Jobs.JNr
AND
Ch.Type<>’Cust’
)
But the nested select is obviously not correct, because in the Ch table I have
24 Cust
24 A
24 A
25 A
25 A
25 A
25 A
I should eliminate all records that have the JNr 24, but not 25. (I eliminate only the record 24 Cust which is not correct because JNr 24 shouldn’t appear at all in report); that’s why I am thinking at aggregates on JNr, but I don’t know how to write that?
Any idea is very welcome
Thank you
I have to write a stored procedure:
SELECT
Customers.SalesRep,
Jobs.JNr,
Jobs.JNr,
Mov.Type,
Mov.Date
FROM
Customers
INNER JOIN Jobs on Customers.Acc=Jobs.Acc
INNER JOIN JobsMov on Jobs.JNr=JobsMov.JNr
INNER JOIN Mov on JobsMov.MNr=Mov.MNr
WHERE
Customers.SalesRep=”Ara”
AND Jobs.JNr not in (‘54’,’49’)
AND Mov.Type<>’imp’
AND Jobs.JNr in
(
SELECT
FROM Ch
WHERE
Substring(Ch.Cost,4,7)=Jobs.JNr
AND
Ch.Type<>’Cust’
)
But the nested select is obviously not correct, because in the Ch table I have
24 Cust
24 A
24 A
25 A
25 A
25 A
25 A
I should eliminate all records that have the JNr 24, but not 25. (I eliminate only the record 24 Cust which is not correct because JNr 24 shouldn’t appear at all in report); that’s why I am thinking at aggregates on JNr, but I don’t know how to write that?
Any idea is very welcome
Thank you