StringBuilder sb = new StringBuilder();
sb.AppendLine("SELECT r1.SectionCode, se.SectionName, r1.ProductCode, p.ProductName, p.GroupID, pg.GroupName, r1.CCtr, c.CCtrShortName AS Sector,");
sb.AppendLine("CASE WHEN dbo.Zone.ID = 1 THEN dbo.Zone.ZoneName + ' - ' + c.CCtrShortName ELSE dbo.Zone.ZoneName END AS Zone,");
sb.AppendLine("r1.Balance, ABS(r1.Cancel) * -1 AS Cancel, r1.AccruedRevenue AS Accrued,");
sb.AppendLine("r1.Balance + (ABS(r1.Cancel) * -1) + r1.AccruedRevenue AS Total");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("CASE WHEN invoice.SectionCode IS NULL THEN AccruedRevenue.SectionCode ELSE invoice.SectionCode END AS SectionCode,");
sb.AppendLine("CASE WHEN invoice.ProductCode IS NULL THEN AccruedRevenue.ProductCode ELSE invoice.ProductCode END AS ProductCode,");
sb.AppendLine("CASE WHEN invoice.CCtr IS NULL THEN AccruedRevenue.CCtr ELSE invoice.CCtr END AS CCtr,");
sb.AppendLine("CASE WHEN invoice.Balance IS NULL THEN 0 ELSE invoice.Balance END AS Balance,");
sb.AppendLine("CASE WHEN invoice.Cancel IS NULL THEN 0 ELSE invoice.Cancel END AS Cancel,");
sb.AppendLine("CASE WHEN AccruedRevenue.AccruedRevenue IS NULL THEN 0 ELSE CONVERT(money, AccruedRevenue) END AS AccruedRevenue");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("CASE WHEN Balance_0.SectionCode IS NULL THEN Cancel_0.SectionCode ELSE Balance_0.SectionCode END AS SectionCode,");
sb.AppendLine("CASE WHEN Balance_0.ProductCode IS NULL THEN Cancel_0.ProductCode ELSE Balance_0.ProductCode END AS ProductCode,");
sb.AppendLine("CASE WHEN Balance_0.CCtr IS NULL THEN Cancel_0.CCtr ELSE Balance_0.CCtr END AS CCtr,");
sb.AppendLine("CASE WHEN Balance_0.Balance IS NULL THEN 0 ELSE Balance_0.Balance END AS Balance,");
sb.AppendLine("CASE WHEN Cancel_0.balance IS NULL THEN 0 ELSE Cancel_0.balance END AS Cancel");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("SectionCode, ProductCode, CCtr, SUM(Balance) AS Balance");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("Balance_1.SectionCode, Balance_1.ProductCode,");
sb.AppendLine("CASE WHEN Home_1.CCtr IS NULL THEN Balance_1.RevenueCode ELSE Home_1.CCtr END AS CCtr,");
sb.AppendLine("Balance_1.Balance");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("Summary_1.SectionCode, Detail_1.ProductCode, Detail_1.RevenueCode, SUM(Detail_1.Balance) AS Balance");
sb.AppendLine("FROM dbo.Detail AS Detail_1");
sb.AppendLine("INNER JOIN dbo.Summary AS Summary_1 ON Detail_1.InvoiceNo = Summary_1.InvoiceNo");
sb.AppendLine("WHERE (Summary_1.InvoiceDate BETWEEN @StartDate AND @EndDate)");
sb.AppendLine("AND (Summary_1.SectionCode <> '00000') AND (NOT (Summary_1.InvoiceNo LIKE 'MV%'))");
sb.AppendLine("GROUP BY Summary_1.SectionCode, Detail_1.ProductCode, Detail_1.RevenueCode) AS Balance_1");
sb.AppendLine("LEFT OUTER JOIN dbo.HomeLocation AS Home_1 ON Balance_1.RevenueCode = Home_1.LocationCode) AS Balance_2");
sb.AppendLine("GROUP BY SectionCode, ProductCode, CCtr) AS Balance_0");
sb.AppendLine("FULL OUTER JOIN (");
sb.AppendLine("SELECT SectionCode, ProductCode, CCtr, SUM(Balance) AS Balance");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("Cancel_3.SectionCode, Cancel_3.ProductCode,");
sb.AppendLine("CASE WHEN hh.cctr IS NULL THEN Cancel_3.RevenueCode ELSE hh.cctr END AS CCtr, ");
sb.AppendLine("Cancel_3.Balance");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("Cancel_2.SectionCode, Detail_2.ProductCode, Detail_2.RevenueCode, SUM(Detail_2.Balance) AS Balance");
sb.AppendLine("FROM dbo.Detail AS Detail_2");
sb.AppendLine("INNER JOIN");
sb.AppendLine("(SELECT Cancel_1.InvoiceNo, Summary_2.SectionCode");
sb.AppendLine("FROM dbo.Disconnect AS Cancel_1");
sb.AppendLine("INNER JOIN dbo.Summary AS Summary_2 ON Cancel_1.InvoiceNo = Summary_2.InvoiceNo");
sb.AppendLine("WHERE (Summary_2.InvoiceDate > @LastYear)");
sb.AppendLine("AND (Cancel_1.EndDate BETWEEN @StartDate AND @EndDate)");
sb.AppendLine("AND (Summary_2.SectionCode <> '00000') AND (NOT (Summary_2.InvoiceNo LIKE 'MV%'))) AS Cancel_2 ON Detail_2.InvoiceNo = Cancel_2.InvoiceNo");
sb.AppendLine("GROUP BY Cancel_2.SectionCode, Detail_2.ProductCode, Detail_2.RevenueCode) AS Cancel_3");
sb.AppendLine("LEFT OUTER JOIN dbo.HomeLocation AS hh ON Cancel_3.RevenueCode = hh.LocationCode) AS Cancel");
sb.AppendLine("GROUP BY SectionCode, ProductCode, CCtr) AS Cancel_0 ON Balance_0.SectionCode = Cancel_0.SectionCode");
sb.AppendLine("AND Balance_0.ProductCode = Cancel_0.ProductCode AND Balance_0.CCtr = Cancel_0.CCtr) AS Invoice");
sb.AppendLine("FULL OUTER JOIN (");
sb.AppendLine("SELECT SectionCode, ProductCode, CCTR, Amount1, Amount2, Amount1 + Amount2 AS AccruedRevenue");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("CASE WHEN m1.docdate IS NULL THEN m2.docdate ELSE m1.docdate END AS DocDate,");
sb.AppendLine("CASE WHEN m1.SectionCode IS NULL THEN m2.SectionCode ELSE m1.SectionCode END AS SectionCode,");
sb.AppendLine("CASE WHEN m1.ProductCode IS NULL THEN m2.ProductCode ELSE m1.ProductCode END AS ProductCode,");
sb.AppendLine("CASE WHEN m1.CCTR IS NULL THEN m2.CCTR ELSE m1.CCTR END AS CCTR,");
sb.AppendLine("CASE WHEN m1.Amount IS NULL THEN 0 ELSE m1.Amount END AS Amount1,");
sb.AppendLine("CASE WHEN m2.Amount IS NULL THEN 0 ELSE m2.Amount END AS Amount2");
sb.AppendLine("FROM (SELECT");
sb.AppendLine("SectionCode, CCTR, ProductCode, ABS(SUM(Amount)) * - 1 AS Amount,");
sb.AppendLine("DATEADD(mm, 1, CONVERT(smalldatetime, '1/' + CONVERT(nvarchar(2), MONTH(DocDate)) + '/' + CONVERT(nvarchar(4), YEAR(DocDate), 103))) AS DocDate");
sb.AppendLine("FROM dbo.AccruedRevenue AS AccruedRevenue_4");
sb.AppendLine("WHERE (CONVERT(smalldatetime, '1/' + CONVERT(nvarchar(2), MONTH(DocDate)) + '/' + CONVERT(nvarchar(4), YEAR(DocDate), 103)) <> (");
sb.AppendLine("SELECT MAX(CONVERT(smalldatetime, '1/' + CONVERT(nvarchar(2), MONTH(DocDate)) + '/' + CONVERT(nvarchar(4), YEAR(DocDate), 103))) AS DocDate");
sb.AppendLine("FROM dbo.AccruedRevenue AS AccruedRevenue_1))");
sb.AppendLine("GROUP BY SectionCode, CCTR, ProductCode, YEAR(DocDate), MONTH(DocDate)) AS m1");
sb.AppendLine("FULL OUTER JOIN (");
sb.AppendLine("SELECT SectionCode, CCTR, ProductCode, SUM(Amount) AS Amount,");
sb.AppendLine("CONVERT(smalldatetime, '1/' + CONVERT(nvarchar(2), MONTH(DocDate)) + '/' + CONVERT(nvarchar(4), YEAR(DocDate), 103)) AS DocDate");
sb.AppendLine("FROM dbo.AccruedRevenue AS AccruedRevenue_3");
sb.AppendLine("WHERE (CONVERT(smalldatetime, '1/' + CONVERT(nvarchar(2), MONTH(DocDate)) + '/' + CONVERT(nvarchar(4), YEAR(DocDate), 103)) <> (");
sb.AppendLine("SELECT MIN(CONVERT(smalldatetime, '1/' + CONVERT(nvarchar(2), MONTH(DocDate)) + '/' + CONVERT(nvarchar(4), YEAR(DocDate), 103))) AS DocDate");
sb.AppendLine("FROM dbo.AccruedRevenue AS AccruedRevenue_2))");
sb.AppendLine("GROUP BY SectionCode, CCTR, ProductCode, YEAR(DocDate), MONTH(DocDate)) AS m2 ON m1.SectionCode = m2.SectionCode");
sb.AppendLine("AND m1.CCTR = m2.CCTR AND m1.ProductCode = m2.ProductCode");
sb.AppendLine("AND m1.DocDate = m2.DocDate) AS Accrued_1");
sb.AppendLine("WHERE (DocDate = @StartDate)) AS AccruedRevenue ON Invoice.SectionCode = AccruedRevenue.SectionCode");
sb.AppendLine("AND Invoice.ProductCode = AccruedRevenue.ProductCode AND Invoice.CCtr = AccruedRevenue.CCTR) AS r1");
sb.AppendLine("INNER JOIN dbo.Section AS se ON r1.SectionCode = se.SectionCode");
sb.AppendLine("LEFT OUTER JOIN dbo.Product AS p ON r1.ProductCode = p.ProductCode");
sb.AppendLine("LEFT OUTER JOIN dbo.CostCenter AS c ON r1.CCtr = c.CCtr");
sb.AppendLine("LEFT OUTER JOIN dbo.Zone ON c.ZoneID = dbo.Zone.ID");
sb.AppendLine("LEFT OUTER JOIN dbo.ProductGroup AS pg ON p.GroupID = pg.ID");
sb.AppendLine("ORDER BY r1.SectionCode, p.GroupID, r1.ProductCode, r1.CCtr ASC");