﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / T-SQL (SS2K8)  / In Need of some expert query help / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 20 Jun 2013 00:04:25 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: In Need of some expert query help</title><link>http://www.sqlservercentral.com/Forums/Topic1428822-392-1.aspx</link><description>Personally I don't think there's anything there that requires distinct, I suspect that distinct is there out of habit, probably the same for the nolocks.</description><pubDate>Sat, 09 Mar 2013 09:58:35 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: In Need of some expert query help</title><link>http://www.sqlservercentral.com/Forums/Topic1428822-392-1.aspx</link><description>To add to what Usman and Gail have recommended, the Udf_convert_int_date is likely a scalar function (according to the way you used it).  That's going to make life slower to begin with.  Using it in a GROUP BY is only going to be another performance problem because of that and the fact that you can GROUP BY the raw integer date even if it's not in the YYYYMMDD format and still end up with the same result but faster.I especially agree with Gails recommendation to Divide'n'Conquer this problem through the use of interim results stored in a Temp Table.  Aggregate/manipulate first, then display.  It will allow you to avoid several of the sum calculations and most of the repeated queries and their related joins.   Don't try to do it all in a single query especially since it seems that you may have some accidental many-to-many joins that require the use of DISTINCT.</description><pubDate>Sat, 09 Mar 2013 08:42:51 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: In Need of some expert query help</title><link>http://www.sqlservercentral.com/Forums/Topic1428822-392-1.aspx</link><description>Please post table definitions, index definitions and execution plan, as per [url]http://www.sqlservercentral.com/articles/SQLServerCentral/66909/[/url]You can start fixing the query by removing the nolock, unless you don't mind the query returning incorrect data (and no, I don't just mean dirty reads). See - [url]http://blogs.msdn.com/b/davidlean/archive/2009/04/06/sql-server-nolock-hint-other-poor-ideas.aspx[/url]Mixing distinct with aggregate... Are those distincts really necessary? Several scalar UDFs, if those do data access, that's going to be horridly slow. [url]http://sqlinthewild.co.za/index.php/2009/04/29/functions-io-statistics-and-the-execution-plan/[/url]Correlated subqueries, not automatically slow, but since you're accessing the same table multiple times that means that SQL will read the table multiple times.I suggest a redesign. Start with what you need from the aptcr table, write a single query that gets all the sums, max, etc. Use that as a derived table (subquery in the from clause)</description><pubDate>Sat, 09 Mar 2013 05:33:26 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: In Need of some expert query help</title><link>http://www.sqlservercentral.com/Forums/Topic1428822-392-1.aspx</link><description>Without knowing much details, It would be all guess work. Please post DDL of the tables, some data and the required output to get better help.But seeing your code I think two things I do not understand1. AGRREGATION (SUM, MAX etc) with DISTINCT2. GROUPING ON COLUMNS WHICH ARE AGGREGATEDSo look into these, as they could be addding more processing than actually needed.</description><pubDate>Sat, 09 Mar 2013 04:00:13 GMT</pubDate><dc:creator>Usman Butt</dc:creator></item><item><title>In Need of some expert query help</title><link>http://www.sqlservercentral.com/Forums/Topic1428822-392-1.aspx</link><description>The below query I have been piecing together is really slow. I was wondering if any of you might be able to give me some ideas with regard to optimizing this. My goal is to create a view then leverage the view for an SSIS project. I'm thinking there MUST be a better solution. Thanks[code="sql"]SELECT       'MySource'                                     AS SourceDB,       'MySite'                                   AS 'SiteName',       Rtrim(apibh.idvend)                          AS VendorNumber,                      Rtrim(apibh.idinvc)                          AS VoucherNumber,       Sum(apibh.amtgrosdst)                        AS VoucherTotalDomestic,       Sum(apibh.amtgrosdst) * ( apibh.exchratehc ) AS VoucherTotalUSD,       (SELECT DISTINCT ( Sum(p.amtpaym) )        FROM   aptcr r WITH(NOLOCK)               INNER JOIN aptcp p                       ON r.btchtype = p.batchtype                          AND r.cntbtch = p.cntbtch                          AND r.cntentr = p.cntrmit               INNER JOIN apibh H                       ON p.idvend = h.idvend                          AND p.idinvc = H.idinvc        WHERE  H.idinvc = apibh.idinvc        GROUP  BY H.idinvc)                         AS PayAmt,                        (Sum(apibh.amtgrosdst) -              (SELECT DISTINCT ( Sum(p.amtpaym) )        FROM   aptcr r WITH(NOLOCK)               INNER JOIN aptcp p                       ON r.btchtype = p.batchtype                          AND r.cntbtch = p.cntbtch                          AND r.cntentr = p.cntrmit               INNER JOIN apibh H                       ON p.idvend = h.idvend                          AND p.idinvc = H.idinvc        WHERE  H.idinvc = apibh.idinvc        GROUP  BY H.idinvc) )                        AS OpenAmountDomestic,                           (Sum(apibh.amtgrosdst) -              ((SELECT DISTINCT ( Sum(p.amtpaym) )        FROM   aptcr r  WITH(NOLOCK)               INNER JOIN aptcp p                       ON r.btchtype = p.batchtype                          AND r.cntbtch = p.cntbtch                          AND r.cntentr = p.cntrmit               INNER JOIN apibh H                       ON p.idvend = h.idvend                          AND p.idinvc = H.idinvc        WHERE  H.idinvc = apibh.idinvc        GROUP  BY H.idinvc) )  *  ( apibh.exchratehc ))           AS OpenAmountUSD,                (        Select DATEDIFF(day,(SELECT DISTINCT ( Max(dbo.Udf_convert_int_date(r.datermit)) )        FROM   aptcr r   WITH(NOLOCK)               INNER JOIN aptcp p                       ON r.btchtype = p.batchtype                          AND r.cntbtch = p.cntbtch                          AND r.cntentr = p.cntrmit               INNER JOIN apibh H                       ON p.idvend = h.idvend                          AND p.idinvc = H.idinvc        WHERE  H.idinvc = apibh.idinvc        GROUP  BY H.idinvc),GETDATE()))AS DueDays,                 '' As AgeDays ,                                         (SELECT DISTINCT ( Max(dbo.Udf_convert_int_date(r.datermit)) )        FROM   aptcr r  WITH(NOLOCK)               INNER JOIN aptcp p                       ON r.btchtype = p.batchtype                          AND r.cntbtch = p.cntbtch                          AND r.cntentr = p.cntrmit               INNER JOIN apibh H                       ON p.idvend = h.idvend                          AND p.idinvc = H.idinvc        WHERE  H.idinvc = apibh.idinvc        GROUP  BY H.idinvc)                         AS PaidDate,                       dbo.Udf_convert_int_date(apibh.datebus)      AS PostedDate,       dbo.Udf_convert_int_date(apibh.datebus)      AS AppliedDate,       dbo.Udf_convert_int_date(apibh.dateinvc)     AS AgingDate,       dbo.Udf_convert_int_date(apibh.datedue)      AS DueDate,       dbo.Udf_convert_int_date(apibh.dateinvc)     AS DocumentDate,       NULL                                         AS ReceivedDate,       CASE         WHEN ( apibh.datedisc ) = 0 THEN NULL         ELSE dbo.Udf_convert_int_date(apibh.datedisc)       END                                          AS DiscountDate,       apibh.codecurn                               AS CurrencyCode,       apibh.exchratehc                             AS EffectiveExchangeRateFROM   apibh AS APIBH   WITH(NOLOCK)WHERE  1 = 1--AND APIBH.IDINVC = '22036'--AND APIBH.IDVEND = 'ABE129'GROUP  BY ( apibh.idvend ),          ( apibh.idinvc ),          ( apibh.amtgrosdst ),          ( apibh.amtgrosdst - Isnull(apibh.amtgrosdst, 0) - apibh.amtdiscavl ),          ( ( apibh.amtgrosdst * apibh.exchratehc ) -            Isnull(apibh.amtgrosdst, 0) * apibh.exchratehc ) - (          apibh.amtdiscavl * apibh.exchratehc ),          ( apibh.datebus ),          apibh.datebus,          apibh.dateinvc,          apibh.datedue,          CASE            WHEN ( apibh.datedisc ) = 0 THEN NULL            ELSE dbo.Udf_convert_int_date(apibh.datedisc)          END,          apibh.amtdiscavl,          apibh.codecurn,          apibh.exchratehc,          apibh.idtrx,          Isnull(apibh.amtgrosdst, 0),          apibh.amttotdist [/code]</description><pubDate>Fri, 08 Mar 2013 18:52:22 GMT</pubDate><dc:creator>mbrady5</dc:creator></item></channel></rss>