I have the following SQL syntax. I ran the SQL in SQL Server Management Studio and it ran successfully. I created the dataset by copying the SQL into my SSRS report within the Datasets.
When I preview my report, I am getting the following error message:
Did I do something in creating the dataset?
DECLARE @DatabaseName Varchar(80) -- database Name ---- Exclude non-Tower DBs and demo dbs -------------------------- DECLARE db_cursor CURSOR FAST_FORWARD FOR SELECT name FROM master.dbo.sysdatabases WHERE NAME = 'Cocoa' OPEN db_cursor FETCH NEXT FROM db_cursor INTO @DatabaseName WHILE @@FETCH_STATUS = 0 BEGIN ------ BEGIN QUERY RESULTS ----------------------- declare @SQL nvarchar(max) set @SQL = ------ Tower Stats Insert ------------------------ ' USE ['+@DatabaseName+'] SELECT Company.CompanyID, Customer.CustClass, Site.SiteID, Site.SiteName, ServiceHistory.WorkOrderID, Route.CustType, DailyRouteEntry.Driver, Route.LineOfBusiness, ServiceHistory.Route, Worker.Worker, DailyRouteEntry.Vehicle, DailyRouteEntry.WorkDate, DayName = CASE (DATEPART(dw, DailyRouteEntry.WorkDate) + @@DATEFIRST) % 7 WHEN 1 THEN ''Sunday'' WHEN 2 THEN ''Monday'' WHEN 3 THEN ''Tuesday'' WHEN 4 THEN ''Wednesday'' WHEN 5 THEN ''Thursday'' WHEN 6 THEN ''Friday'' WHEN 0 THEN ''Saturday'' END, ISNULL(WorkOrder_vw_Totals.Charges,0) Charges, ---- For FtMyers Only 6/19/2012 ------ (SELECT Count(r.Route) FROM DailyRouteEntry de INNER JOIN Route r ON de.Route = r.Route WHERE (Workdate = DailyRouteEntry.WorkDate) and (r.LineOfBusiness IN (''T'', ''Temporary'', ''Roll Off'', ''RollOff'')) AND (de.Status IN (2)) AND (ISNULL(Vehicle,''0'') <> ''0'')) AS RouteCountPerWorkDay, ---- For FtMyers Only 6/19/2012 ------ ServiceHistory.Status, LTRIM(RTRIM(WorkflowCode.WorkflowCode)) AS WorkflowCode, Company.CompanyName, Site.Salesperson, ServiceInfo.TemporaryPermanent, ----- Permanent Non Work Order Charge Revenue (NOTE: Used Period for Non Work Order Charge) (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401202410000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) and ar.CompanyID = ''0402'') WHEN ''0601'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401202410000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) and ar.CompanyID = ''0601'') WHEN ''0603'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401202410000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) and ar.CompanyID = ''0603'') WHEN ''0120'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401202410000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) and ar.CompanyID = ''0120'') WHEN ''0122'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401202410000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) and ar.CompanyID = ''0122'') ELSE (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401202410000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) and ar.CompanyID = Company.CompanyID) END) AS PermanentRevenueNonWorkOrderCharge, --WHERE ar.CreditAcct IN (''401202410000'') AND ar.TransactionDate = DailyRouteEntry.WorkDate and ar.DetailType <> 2) as PermanentRevenueNonWorkOrderCharge, ----- Temporary Non Work Order Charge Revenue (NOTE: Used Period for Non Work Order Charge) (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) AND ar.CompanyID = ''0402'') WHEN ''0601'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) AND ar.CompanyID = ''0601'') WHEN ''0603'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) AND ar.CompanyID = ''0603'') WHEN ''0120'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) AND ar.CompanyID = ''0120'') WHEN ''0122'' THEN (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) AND ar.CompanyID = ''0122'') ELSE (SELECT ISNULL(SUM(ar.OriginalAmount),0) FROM Customer c INNER JOIN ARDetail ar ON c.CustID = ar.CustID INNER JOIN GLAccount gl ON gl.GLAccount = ar.CreditAcct WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.Period = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),6) and ar.DetailType IN (1,3,4) AND ar.CompanyID = Company.CompanyID) END) AS TemporaryRevenueNonWorkOrderCharge, --WHERE ar.CreditAcct IN (''401102410000'',''401102510000'') AND ar.TransactionDate = DailyRouteEntry.WorkDate and ar.DetailType <> 2) as TemporaryRevenueNonWorkOrderCharge, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([01_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget01, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [01_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget01, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([02_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget02, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [02_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget02, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([03_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget03, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [03_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget03, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([04_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget04, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [04_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget04, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([05_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget05, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget05, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [05_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([06_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget06, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [06_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget06, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([07_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget07, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [07_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget07, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([08_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget08, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [08_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget08, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([09_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget09, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [09_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget09, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([10_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget10, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [10_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget10, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([11_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget11, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [11_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget11, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT SUM([12_MBudget]) FROM WP_Solomon_Budgets b where GLAcct in (''401202410000'',''451202410000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID) --ORDER BY LastUpdateDate DESC) END) PermanentBudget12, (CASE Company.CompanyID WHEN ''0402'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0402'' ORDER BY LastUpdateDate DESC) WHEN ''0601'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0601'' ORDER BY LastUpdateDate DESC) WHEN ''0603'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0603'' ORDER BY LastUpdateDate DESC) WHEN ''0120'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0120'' ORDER BY LastUpdateDate DESC) WHEN ''0122'' THEN (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = ''0122'' ORDER BY LastUpdateDate DESC) ELSE (SELECT TOP 1 [12_MBudget] FROM WP_Solomon_Budgets b where GLAcct in (''401102410000'',''401102510000'') and LedgerType = ''BUDGET'' and FiscYr = LEFT(CONVERT(VARCHAR(8),DailyRouteEntry.WorkDate,112),4) and b.CompanyID = Company.CompanyID ORDER BY LastUpdateDate DESC) END) TemporaryBudget12 FROM ((((((([dbo].WorkflowCode WorkflowCode LEFT OUTER JOIN (((DailyRouteEntry DailyRouteEntry LEFT OUTER JOIN ServiceHistory ServiceHistory ON (DailyRouteEntry.Route=ServiceHistory.Route) AND (DailyRouteEntry.WorkDate=ServiceHistory.ServiceDate)) LEFT OUTER JOIN Route Route ON DailyRouteEntry.Route=Route.Route) LEFT OUTER JOIN Worker Worker ON DailyRouteEntry.Driver=Worker.Worker) ON WorkflowCode.WorkflowCode=ServiceHistory.WorkflowCode) LEFT OUTER JOIN WorkOrder_vw_Totals WorkOrder_vw_Totals ON ServiceHistory.WorkOrderID=WorkOrder_vw_Totals.WorkOrderID) LEFT OUTER JOIN Trip Trip ON ServiceHistory.WorkOrderID=Trip.WorkOrderID) LEFT OUTER JOIN ServiceBilling ServiceBilling ON ServiceHistory.ServiceInfoUID=ServiceBilling.ServiceInfoUID INNER JOIN ServiceInfo ServiceInfo ON ServiceInfo.ServiceInfoUID = ServiceHistory.ServiceInfoUID) LEFT OUTER JOIN Site Site ON ServiceBilling.SiteID=Site.SiteID) LEFT OUTER JOIN Customer Customer ON Site.CustID=Customer.CustID LEFT OUTER JOIN Company Company ON Company.CompanyID = Site.CompanyID) LEFT OUTER JOIN WP_Solomon_Budgets B ON B.CompanyID = Company.CompanyID and GLAcct in (''401102410000'',''401102510000'',''401202410000'',''451202410000'') and LedgerType = ''budget'') WHERE (DailyRouteEntry.WorkDate BETWEEN ''02-01-2013'' AND ''02-28-2013'') AND ServiceHistory.Status=1 AND Route.LineOfBusiness IN (''T'', ''Temporary'', ''Roll Off'', ''RollOff'') ------------------------------------------------------------------------------------- --AND ISNULL(Site.Salesperson,0) = -- Select All Salesperson or Single Salesperson -- (CASE @Salesperson -- WHEN ''All'' THEN ISNULL(Site.Salesperson,0) -- ELSE @Salesperson -- END) -------------------------------------------------------------------------------------- AND ISNULL(Company.CompanyID,0) = ''0102'' -- Select All Divisions or Single Division -- (CASE @Division --WHEN ''All'' THEN ISNULL(Company.CompanyID,0) --ELSE @Division -- END) -------------------------------------------------------------------------------------- GROUP BY ServiceHistory.WorkOrderID, Customer.CustClass, Site.SiteID, Site.SiteName, Company.CompanyID, Route.CustType, DailyRouteEntry.Driver, Route.LineOfBusiness, ServiceHistory.Route, Worker.Worker, DailyRouteEntry.Vehicle, DailyRouteEntry.WorkDate, WorkOrder_vw_Totals.Charges, ServiceHistory.Status, WorkflowCode.WorkflowCode, Company.CompanyName, Site.Salesperson, ServiceInfo.TemporaryPermanent ' EXECUTE sp_executesql @SQL FETCH NEXT FROM db_cursor INTO @DatabaseName END CLOSE db_cursor DEALLOCATE db_cursor