Sorry it would probably help to see the query. I have tried setting ansi_warnings and arithabort off and adding a refresh and running it for smaller date ranges.
if object_id('sp_DBR_Beverage_Report','P') is not null
drop procedure sp_DBR_Beverage_Report
go
create procedure sp_DBR_Beverage_Report
@startdate datetime,
@enddate datetime
as
begin
SET ANSI_WARNINGS OFF;
SET ARITHABORT OFF;
select 'dbr.refresh', 90, 'counter';
SET @startdate = CAST(@startdate AS DATE);
SET @enddate = CAST(@enddate AS DATE);
IF OBJECT_ID('tempdb.dbo.#TempResults','U') IS NOT NULL
DROP TABLE #TempResults;
IF OBJECT_ID('tempdb.dbo.#coolersbytype','U') IS NOT NULL
DROP TABLE #coolersbytype;
IF OBJECT_ID('tempdb.dbo.#coolersbymarket','U') IS NOT NULL
DROP TABLE #coolersbymarket;
IF OBJECT_ID('tempdb.dbo.#mmlocations','U') IS NOT NULL
DROP TABLE #mmlocations;
IF OBJECT_ID('tempdb.dbo.#BevManufacturers','U') IS NOT NULL
DROP TABLE #BevManufacturers;
CREATE TABLE #TempResults (
pos_code varchar(32) NULL,
pos_description varchar(32) NULL,
loc_code varchar(32) NULL,
loc_description varchar(32) NULL,
cus_code varchar(32) NULL,
cus_description varchar(32) NULL,
nat_code varchar(32) NULL,
nat_description varchar(32) NULL,
sit_code varchar(32) NULL,
sit_description varchar(32) NULL,
revenue_collected varchar(32) NULL,
product_costs varchar(32) NULL,
total_comm varchar(32) NULL,
total_taxes varchar(32) NULL,
items_sold varchar(32) NULL,
price varchar(32) NULL,
net_revenue varchar(32) NULL,
gross varchar(32) NULL,
net_profit varchar(32) NULL,
pro_code varchar(32) NULL,
pro_description varchar(32) NULL,
Product_SKU varchar(32) NULL,
pdf_code varchar(32) NULL,
pdf_description varchar(32) NULL,
pro_source_id varchar(32) NULL,
pro_id varchar(32) NULL,
pkp_source_id varchar(32) NULL,
pkp_id varchar(32) NULL,
pdf_source_id varchar(32) NULL,
pdf_id varchar(32) NULL,
pos_source_id varchar(32) NULL,
pos_id varchar(32) NULL,
loc_source_id varchar(32) NULL,
loc_id varchar(32) NULL,
cus_source_id varchar(32) NULL,
cus_id varchar(32) NULL,
nat_source_id varchar(32) NULL,
nat_id varchar(32) NULL,
sit_source_id varchar(32) NULL,
sit_id varchar(32) NULL,
pro_revenue varchar(32) NULL,
pro_net_revenue varchar(32) NULL,
pro_gross varchar(32) NULL,
pro_gross_pr varchar(32) NULL,
pro_net_profit varchar(32) NULL,
pro_net_profit_pr varchar(32) NULL,
pdf_revenue varchar(32) NULL,
pdf_net_revenue varchar(32) NULL,
pdf_gross varchar(32) NULL,
pdf_gross_pr varchar(32) NULL,
pdf_net_profit varchar(32) NULL,
pdf_net_profit_pr varchar(32) NULL,
pos_revenue varchar(32) NULL,
pos_net_revenue varchar(32) NULL,
pos_gross varchar(32) NULL,
pos_gross_pr varchar(32) NULL,
pos_net_profit varchar(32) NULL,
pos_net_profit_pr varchar(32) NULL,
loc_revenue varchar(32) NULL,
loc_net_revenue varchar(32) NULL,
loc_gross varchar(32) NULL,
loc_gross_pr varchar(32) NULL,
loc_net_profit varchar(32) NULL,
loc_net_profit_pr varchar(32) NULL,
cus_revenue varchar(32) NULL,
cus_net_revenue varchar(32) NULL,
cus_gross varchar(32) NULL,
cus_gross_pr varchar(32) NULL,
cus_net_profit varchar(32) NULL,
cus_net_profit_pr varchar(32) NULL,
nat_revenue varchar(32) NULL,
nat_net_revenue varchar(32) NULL,
nat_gross varchar(32) NULL,
nat_gross_pr varchar(32) NULL,
nat_net_profit varchar(32) NULL,
nat_net_profit_pr varchar(32) NULL,
sit_revenue varchar(32) NULL,
sit_net_revenue varchar(32) NULL,
sit_gross varchar(32) NULL,
sit_gross_pr varchar(32) NULL,
sit_net_profit varchar(32) NULL,
sit_net_profit_pr varchar(32) NULL,
pos_grp_param_desc varchar(32) NULL,
pos_param_code varchar(32) NULL,
pos_param_desc varchar(32) NULL,
loc_grp_param_desc varchar(32) NULL,
loc_param_code varchar(32) NULL,
loc_param_desc varchar(32) NULL,
cus_grp_param_desc varchar(32) NULL,
cus_param_code varchar(32) NULL,
cus_param_desc varchar(32) NULL,
pdf_grp_param_desc varchar(32) NULL,
pdf_param_code varchar(32) NULL,
pdf_param_desc varchar(32) NULL,
pro_grp_param_desc varchar(32) NULL,
pro_param_code varchar(32) NULL,
pro_param_desc varchar(32) NULL,
sit_grp_param_desc varchar(32) NULL,
sit_param_code varchar(32) NULL,
sit_param_desc varchar(32) NULL,
nat_param_code varchar(32) NULL,
nat_param_desc varchar(32) NULL,
GroupNumber varchar(32) NULL
)
INSERT INTO #TempResults
EXEC Vendmax.dbo.RPT_Financials_By_PDF
@NPB_Date = @startdate,
@NPE_Date = @enddate,
@STR_Basis = 'Product Sales',
@STR_Top_Level_Grouping = 'POS',
@STR_Include_MicroMarket = 'MicroMarket Only',
@STR_Show_MM_Sales_Based_On = 'Actual MM Sales',
@STR_Level_Of_Detail = 'PRO',
@STR_Machine_Type = 'All',
@STR_Point_Of_Sale_Type = 'All',
@STR_Show_Outsourced = 'No',
@STR_Choose_Record = 'All',
@STR_Packaged_Product_Detail = 'Yes',
@STR_Vend_Visits = 'All Vend Visits';
CREATE TABLE #coolersbymarket (
locID varchar(32) NULL,
locSource varchar(32) NULL,
locationCode varchar(32) NULL,
nveCode varchar(32) NULL,
locationDesc varchar(32) NULL,
posID varchar(32) NULL,
posDesc varchar(32) NULL,
nveID varchar(32) NULL,
customer varchar(32) NULL,
locationID varchar(32) NULL,
nveDesc varchar(32) NULL
)
INSERT INTO #coolersbymarket
SELECT
loc.loc_id AS 'locID'
,loc.loc_source_id AS 'locSource'
,loc.code AS 'locationCode'
,nve.code AS 'nveCode'
,loc.description AS 'locationDesc'
,pos.pos_id AS 'posID'
,pos.description AS 'posDesc'
,nve.nve_id AS 'nveID'
,cus.description AS 'customer'
,loc.loc_id AS 'locationID'
--,loc.code AS 'locationCode'
--,nve.code AS 'nveCode'
,nve.description AS 'nveDesc'
FROM Vendmax.dbo.non_vending_equipment nve
JOIN Vendmax.dbo.equipment_placement_history eph
ON nve.nve_id = eph.nve_id
AND nve.nve_source_id = eph.nve_source_id
JOIN Vendmax.dbo.points_of_sale pos
ON eph.pos_id = pos.pos_id
AND eph.pos_source_id = pos.pos_source_id
JOIN Vendmax.dbo.locations loc
ON pos.loc_id = loc.loc_id
AND pos.loc_source_id = loc.loc_source_id
JOIN Vendmax.dbo.customers cus
ON loc.cus_id = cus.cus_id
AND loc.cus_source_id = cus.cus_source_id
WHERE eph.placement_status = 'A'
AND LEFT(nve.code,3) IN ('60-','50-','40-')
--AND pos.pos_active = 'N'
AND nve.description LIKE '%cooler%'
--AND LEFT(pos.description, 3) != 'zzz'
AND loc.loc_id IN
(SELECT loc_id FROM Vendmax.dbo.points_of_sale WHERE pos_type = 'M' AND pos_active = 'Y')
ORDER BY cus.description,loc.code
;
CREATE TABLE #mmlocations (
locCode varchar(32) NULL,
locID varchar(32) NULL,
locSource varchar(32) NULL
);
CREATE TABLE #coolersbytype (
locID varchar(32) NULL,
locSource varchar(32) NULL,
locCode varchar(32) NULL,
hasCoke varchar(32) NULL,
hasPepsi varchar(32) NULL
)
INSERT INTO #mmlocations
SELECT
MAX(locationCode) AS 'locCode',
locID,
locSource
FROM #coolersbymarket
GROUP BY locID, locSource;
INSERT INTO #coolersbytype
SELECT
MAX(mml.locID) AS 'locID'
,MAX(mml.locSource) AS 'locSource'
,MAX(mml.locCode) AS 'locCode'
,(SELECT COUNT(*) FROM #coolersbymarket WHERE MAX(mml.locCode) = locationCode AND LEFT(nveCode,3) = '50-') AS 'hasCoke'
,(SELECT COUNT(*) FROM #coolersbymarket WHERE MAX(mml.locCode) = locationCode AND LEFT(nveCode,3) = '60-') AS 'hasPepsi'
FROM #mmlocations mml
JOIN #coolersbymarket cbm
on mml.locCode = cbm.locationCode
AND mml.locCode = cbm.locationCode
GROUP BY mml.locCode
CREATE TABLE #BevManufacturers (
productID varchar(32) NULL
,proSource varchar(32) NULL
,manufacturerID varchar(32) NULL
)
INSERT INTO #BevManufacturers
SELECT pro.pro_id
,MAX(pro.pro_source_id)
,MAX(grpm.grp_id)
FROM Vendmax.dbo.products pro
JOIN Vendmax.dbo.group_members grpm
ON pro.pro_id = grpm.member_id
AND pro.pro_source_id = grpm.member_source_id
WHERE grpm.grp_id IN ('5639364','5645249','5698088','5717422','4679846', '5727297', '5800150')
GROUP BY pro.pro_id;
SELECT
CONCAT(DATENAME(month, @startdate),' ',DATEPART(day,@startdate),', ',DATEPART(year,@startdate)) AS 'ORT FROM DATE'
,CONCAT(DATENAME(month, @enddate),' ',DATEPART(day,@enddate),', ',DATEPART(year,@enddate)) AS 'ORT TO DATE'
,CASE when bev.manufacturerID = '5639364' then 'Pepsi'
when bev.manufacturerID = '5645249' then 'Coke'
when bev.manufacturerID = '5698088' then 'Red Bull'
when bev.manufacturerID = '5717422' then 'Monster'
when bev.manufacturerID = '4679846' then '7-UP'
when bev.manufacturerID = '5727297' then 'VSA'
when bev.manufacturerID = '5800150' then 'All Brands Beverage'
else 'Not in Pepsi/Coke Product Group'
end AS 'Beverage Product Manufacturer'
,'206' AS 'Franchise OPS'
,'Premier Food Service' AS 'Franchise Name'
,temp.cus_description AS 'Customer'
,temp.loc_description AS 'Location'
,loc.addr1 AS 'Address 1'
,loc.addr2 AS 'Address 2'
,loc.addr3 AS 'Address 3'
,loc.city AS 'City'
,loc.state AS 'State'
,loc.zip AS 'Zip'
,pos_code AS 'POS Code'
,CASE
WHEN cool.hasCoke > '0' AND cool.hasPepsi = '0' THEN 'COKE'
WHEN cool.hasCoke = '0' AND cool.hasPepsi > '0' THEN 'CONSUMER'
WHEN cool.hasCoke > '0' AND cool.hasPepsi > '0' THEN 'COKE/CONSUMER'
WHEN cool.hasCoke = '0' AND cool.hasPepsi = '0' THEN 'CONSUMER'
ELSE 'Not Found'
END AS 'Cooler'
,(SELECT TOP 1 barcode
FROM Vendmax.dbo.barcode_map WHERE pkp_id = pkp.pkp_id AND pkp_source_id = pkp.pkp_source_id ORDER BY LEN(barcode) DESC) AS 'UPC'
,temp.pro_description AS 'Product Name'
,temp.price AS 'TotalPrice'
,temp.items_sold AS 'Quantity'
,temp.pdf_description AS 'Product Family'
FROM #TempResults temp
LEFT JOIN Vendmax.dbo.locations loc
ON temp.loc_id = loc.loc_id
AND temp.loc_source_id = loc.loc_source_id
JOIN Vendmax.dbo.products pro
ON temp.pro_id = pro.pro_id
AND temp.pro_source_id = pro.pro_source_id
JOIN Vendmax.dbo.packaged_products pkp
ON pro.pro_id = pkp.pro_id
AND pro.pro_source_id = pkp.pro_source_id
AND (pkp.uni_id = 1 AND pkp.uni_source_id = 1)
LEFT JOIN #coolersbytype cool
ON loc.loc_id = cool.locID
AND loc.loc_source_id = cool.locSource
LEFT JOIN #BevManufacturers bev
ON temp.pro_id = bev.productID
AND temp.pro_source_id = bev.proSource
WHERE
LEFT(temp.pdf_description,1) != 'z'
AND LEFT(temp.pdf_description,3) != 'BAG'
ORDER BY cus_description, loc_description;
end
go