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
 General SQL Server Forums
 New to SQL Server Programming
 Optimize the query...

Author  Topic 

chiman

21 Posts

Posted - 2008-01-24 : 13:18:30
Hi,

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-24 : 13:40:48
Try like this:-

select
d.DocumentNumber as [Delivery number],
d.ReceiptDate as [Receipt Date],
tmp1.[Total Cartons],
tmp2.[Total Units],
tmp1.[Cartons Received],
tmp2.[Units Received],
tmp1.[Cartons Open]
From
Document d
inner join
(SELECT Document_ID,
COUNT(DISTINCT c.CartonStatus_ID) as [Total Units],
SUM(CASE
WHEN cl.Codevalue='Carton Closed'
THEN 1
ELSE 0
END) AS [Cartons Received],
SUM(CASE
WHEN cl.Codevalue='Carton Open'
THEN 1
ELSE 0
END) AS [Cartons Open]
FROM Carton c
INNER JOIN codelist cl
ON cl.Codelist_id = c.CartonStatus_ID
GROUP BY Document_ID)tmp1
on tmp1.Document_Id = d.Document_Id
INNER JOIN (
SELECT Document_ID,
Sum(QtyShipped) as [Total Units],
Sum(CD.QtyReceived) as [Units Received],
FROM Carton c
INNER JOIN CartonDetail cd
on cd.Carton_Id = c.Carton_Id
GROUP BY Document_ID
)tmp2
ON tmp2.Document_ID=d.Document_Id
inner join Stores s on s.Store_Id = d.SourceId
Inner join Codelist cl on cl.Codelist_id = c.CartonStatus_Id
inner join dbo.Codelist cl1 on cl1.Codelist_Id = d.DocType
left outer join CartonDetail cd on cd.Carton_Id = c.Carton_Id
Left outer join Codelist cl2 on cl2.Codelist_Id = cd.CartonDetailType_Id
Where
cl.Codevalue='Carton Open'
and s.StoreType =5
and not d.DocumentNumber is null
and d.destinationId= 8637
and cl1.CodeValue = 'JaxReceipts'
and cl2.Codevalue='Carton Details'
Group by DocumentNumber, d.Receiptdate,d.Document_ID
Order by DocumentNumber
Go to Top of Page

chiman

21 Posts

Posted - 2008-01-24 : 13:45:45
Hey thanks..
its giving some errors.. I'll try on this and get back to you..

Thanks
Renu
Go to Top of Page

chiman

21 Posts

Posted - 2008-01-24 : 13:55:00
The query gives the correct output. But it takes more than double the time. :(
Total time with my original query is 10 sec..
and now with this new one, it takes 30 sec..

Is there any other way..

Thanks
Renu
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2008-01-24 : 14:22:36
One thing I noticed is that the LEFT OUTER JOINS are really INNER JOIN, so that might get your a little performance. Unless they are supposed to be LEFT OUTER JOINs, then you need to move your restrictions out of the WHERE clause and onto the JOIN criteria.
Go to Top of Page

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-01-24 : 17:20:27
quote:
Originally posted by renu.khot@gmail.com

Hi,




Why do that? Deleting your original post makes it that much harder for the next person to use the thread to find an answer.
Go to Top of Page
   

- Advertisement -