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 2008 Forums
 Transact-SQL (2008)
 how to optimize this query?

Author  Topic 

nobelins
Starting Member

8 Posts

Posted - 2011-03-19 : 14:20:51
hi

i have a large table (~10.000.000 rows) and i have to make some calculations on it. i tried the
query below:

select z.ItemName,z.ItemId from (
total_amount = (SELECT SUM(AMOUNT) FROM ITEM_LIST WHERE ITEM_ID = z.ItemId),
total_cost = (SELECT SUM(COST) FROM ITEM_LIST WHERE ITEM_ID = z.ItemId),
send_back = (SELECT SUM(AMOUNT) FROM ITEM_LIST WHERE ITEM_ID = z.ItemId AND SB =1),
send_back_percent = (SELECT SUM(AMOUNT) FROM ITEM_LIST WHERE ITEM_ID = z.ItemId) * 100 / (SELECT SUM(AMOUNT) FROM ITEM_LIST WHERE ITEM_ID = z.ItemId AND SB =1)
FROM SELECT(DISTINCT(ItemId)) FROM ITEM_LIST AS z ORDER BY ItemId ASC


the problem is the query is too slow.
is there a way to getting data faster?

thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-03-19 : 14:26:18
This is not your original query.
So how should we help?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-03-19 : 14:30:12
SELECT ItemName, ItemID, Sum(Amount) AS total_amount,
SUM(Cost) AS total_cost,
SUM(CASE WHEN SB=1 THEN Amount END) AS send_back,
SUM(Amount)/SUM(CASE WHEN SB=1 THEN Amount END)*100 AS send_back_percent
FROM ItemList
GROUP BY ItemName, ItemID
Go to Top of Page

nobelins
Starting Member

8 Posts

Posted - 2011-03-19 : 14:30:54
what do you mean?
Go to Top of Page

nobelins
Starting Member

8 Posts

Posted - 2011-03-19 : 14:31:48
thanks robvolk. i'll give it a try
Go to Top of Page
   

- Advertisement -