Jump to content

SQL summarising

Recommended Posts



I'm trying to run the following query, but it's taking ages. Any idea how I can summarise it to speed it up any? 


Td.Cust_Code, td.prod_dept, td.supplier_code,s.supplier_name,
REPLACE(CONVERT(VARCHAR,CONVERT(MONEY,cast(sum(case when record_type in (7,16) and *****.dbo.ftnGetGoLocalTopScheme(td.branch,td.cust_code) like 'g%' then total_value *-1
         when record_type not in (7,16) and *****.dbo.ftnGetGoLocalTopScheme(td.branch,td.cust_code) like 'g%' then total_value
    else 0.00 end)as decimal(10,2))),1),'.00','.00') as [GL Spend]
From Transaction_Details as Td
left outer join dbo.supplier as s on td.supplier_code=s.supplier_code
Where Td.Branch >0 and Td.Date_Transaction Between '2015-06-24' AND '2016-04-29 23:59'

Group By Td.Branch, Td.Cust_Code, td.prod_dept, td.supplier_code, s.supplier_name




Link to post
Share on other sites
  • 2 weeks later...

@The Fishwhen you say ages for how many records are we talking here? Not going to fix your sql but have you got indexes on and do you need to use like and wildcards in that manner or at all?

run your query with show execution plan to point out where your bottlenecks are


like something% is basically the worst version of that and less likely to use or attempt to use any indexing 


(that's me being kind)

Link to post
Share on other sites
  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...