2 SQL queries in one stored procedure - sql

Can I run 2 queries in one stored procedure ?
CREATE PROCEDURE AddProd
#Store_Name varchar(50),
#Price int,
#Prod_Name varchar(50),
#Qty int,
#ProductDescription varchar(50),
#RatingSum int,
#RatingCount int,
#ProductImage varchar(50),
#Prod_Date date,
AS
BEGIN
SELECT S.Store_ID
FROM Store S
WHERE StoreName=#StoreName
INSERT INTO Product (Store_ID, Price, Prod_Name, Qty, ProductDescription, RatingSum, RatingCount, ProductImage, Prod_Date)
VALUES (S.Store_ID, #Price, #Prod_Name, #Qty, #ProductDescrpition, #RatingSum, #RatingCount, #ProductImage, #Prod_Date)
END
GO
For this code above, I want retrieve a STORE_ID by giving the STORE_NAME the user give as a parameter.
I want to use this STORE_ID in the INSERT statement.
Can I do this ?!
AKA, is the S.store_ID returned from the first query, the same as that one I used in "Values" ?

Technically, you can do this in a single query:
INSERT INTO Product
(Store_ID, Price, Prod_Name, Qty, ProductDescription, RatingSum, RatingCount, ProductImage, Prod_Date)
SELECT S.Store_ID, #Price,#Prod_Name,#Qty,#ProductDescription,#RatingSum,#RatingCount,#ProductImage,#Prod_Date
FROM Store S
WHERE StoreName=#StoreName
I don't have test data handy to check, but you may have to give the appropriate names to each of the columns in the select clause from that query, instead of just the variable names. The only other reason this might not work is if you also wanted to return the selected storeID from the stored procedure, but even in that case you can just add an OUTPUT clause:
INSERT INTO Product
(Store_ID, Price, Prod_Name, Qty, ProductDescription, RatingSum, RatingCount, ProductImage, Prod_Date)
OUTPUT S.Store_ID
SELECT S.Store_ID, #Price,#Prod_Name,#Qty,#ProductDescription,#RatingSum,#RatingCount,#ProductImage,#Prod_Date
FROM Store S
WHERE StoreName=#StoreName
But for the title question, the answer is affirmative; you can execute multiple statements inside a single stored procedure.

unless you want to return the storeID from the sp remove that query and put it into the insert
INSERT INTO Product (Store_ID,Price,Prod_Name,Qty,ProductDescription,RatingSum,RatingCount,ProductImage,Prod_Date)
values (
(SELECT S.Store_ID FROM Store S WHERE StoreName=#StoreName),
#Price,#Prod_Name,#Qty,#ProductDescrpition,#RatingSum,#RatingCount,#ProductImage,#Prod_Date)

If StoreID is unique for each store name you can store it in a variable and use it in your insertion
CREATE PROCEDURE AddProd
#Store_Name varchar(50),
#Price int,
#Prod_Name varchar(50),
#Qty int,
#ProductDescription varchar(50),
#RatingSum int,
#RatingCount int,
#ProductImage varchar(50),
#Prod_Date date,
AS
BEGIN
DECLARE #StoreID [DataType]
SELECT #StoreID = S.Store_ID
FROM Store S
WHERE StoreName=#StoreName
INSERT INTO Product (Store_ID,Price,Prod_Name,Qty,ProductDescription,RatingSum,RatingCount,ProductImage,Prod_Date)
values (#StoreID,#Price,#Prod_Name,#Qty,#ProductDescrpition,#RatingSum,#RatingCount,#ProductImage,#Prod_Date)
END
GO
In any scenario you can use the following
CREATE PROCEDURE AddProd
#Store_Name varchar(50),
#Price int,
#Prod_Name varchar(50),
#Qty int,
#ProductDescription varchar(50),
#RatingSum int,
#RatingCount int,
#ProductImage varchar(50),
#Prod_Date date,
AS
BEGIN
INSERT INTO Product (Store_ID,
Price,
Prod_Name,
Qty,
ProductDescription,
RatingSum,
RatingCount,
ProductImage,
Prod_Date
)
SELECT
S.Store_ID
#StoreID,
#Price,
#Prod_Name,
#Qty,
#ProductDescrpition,
#RatingSum,
#RatingCount,
#ProductImage,
#Prod_Date
FROM Store S
WHERE StoreName=#StoreName
END
GO

Related

Trying to aggregate quantities across multiple tables SQL

I'm building a stock synch which compares product quantities from three different tables:
EWM
USOR
DSOR
The maths behind the stock synch is EWM - (USOR + DSOR) = Misalignment
The EWM table divides the product quantities by the different storage types that products are held in which necessitates the need to have the quantities aggregated before being used in the end query but I keep getting the wrong figure after aggregating the quantities so I think I'm doing something wrong but I can't seem to spot it.
Here's my code:
--Table for EWM Data--
create table EWM(
Date_loaded date,
Sap_code bigint,
Product_description varchar(100),
Location nvarchar(50),
Storage_type varchar(50),
Quantity int,
Sap_batch nvarchar(50),
Expiry_date date,
Stock_type varchar(50)
);
--Table for USOR Data--
create table USOR(
Date_Loaded date,
Sap_Code bigint,
Product_description varchar(100),
Pack nvarchar(50),
Cost_price decimal,
Trade_price decimal,
Stock int,
Location nvarchar(50),
);
--Table for DSOR--
create table DSOR(
Date_Loaded datetime,
Sap_code bigint,
Product_description varchar(100),
Bin_location nvarchar(50),
Location varchar(50),
Pack nvarchar(50),
Units int,
Total int,
);
--Table for difference between EWM and USOR+DSOR--
create table Misalignment
(
Date_loaded date,
Sap_code bigint,
Product_description varchar(100),
Pack nvarchar(50),
Location nvarchar(50),
Total_difference int
)
--Inserting data in Misalingment using query from USOR, USOR_DSOR, EWM --
WITH x AS (
SELECT SAP_Code, Quantity
FROM EWM
GROUP BY SAP_Code, Quantity
)
insert into Misalignment(Date_Loaded, Sap_code, Product_description, Location, Total_difference)
Select USOR.Date_Loaded, USOR.Sap_Code, USOR.Product_description, USOR.Location, sum(x.Quantity - (USOR.stock + DSOR.Total))as Total_difference
FROM x
INNER JOIN USOR ON x.SAP_Code = USOR.SAP_Code
INNER JOIN DSOR ON x.SAP_Code = DSOR.SAP_Code
group by USOR.Date_Loaded, USOR.Sap_Code, USOR.Product_description, USOR.Pack, USOR.Location
select * from Misalignment
where Total_difference > 0
order by Total_difference
desc
I ran the query below to test my code out:
select * from ewm
where Date_loaded = '2020-10-05' and Sap_code = 1002945
select * from DSOR
where Sap_code = 1002945 and Date_loaded= '2020-10-05'
select * from Misalignment
where Sap_code = 1002945 and Date_loaded = '2020-10-05'
This was the result:
The actual result should be 0.
Any suggestions?

Error converting data type varchar to real

I built an INSTEAD OF trigger that supposed to start when I try to insert into a view of products that consists of 2 tables : Approved_Products and Products.
My point is to reroute the insert and insert the data to the Approved_Products table.
The data for the insert is coming from a CURSOR that holds the DATA from tuple I try to insert.
The code is as follows :
CREATE TABLE Product_Approval
(
Product_ID VARCHAR(100) NOT NULL PRIMARY KEY,
Type VARCHAR(100) NOT NULL,
Price REAL NOT NULL,
Name VARCHAR(100) NOT NULL,
Supplier_ID VARCHAR(100) NOT NULL
FOREIGN KEY REFERENCES SUPPLIERS(Supplier_ID),
CONSTRAINT Ck_Price CHECK (Price > 0)
)
CREATE VIEW vw_ProductList
AS
SELECT Product_ID, Type, Name, Price, Supplier_ID, 'Approved' Approval_Status
FROM dbo.PRODUCTS
UNION
SELECT Product_ID, Type, Name, Price, Supplier_ID, 'Pending Approval' Approval_Status
FROM dbo.Product_Approval
SELECT * FROM VW_ProductList
CREATE TRIGGER ProductApprovals
ON VW_ProductList
INSTEAD OF INSERT
AS
BEGIN
SET NOCOUNT ON
DECLARE
#Product_ID VARCHAR(100),
#Type varchar(100),
#Name VARCHAR(100),
#Price REAL,
#Supplier_ID VARCHAR(100)
DECLARE CCC CURSOR
FOR (SELECT Product_ID, Type, Name, Price, Supplier_ID FROM INSERTED)
OPEN CCC
FETCH NEXT FROM CCC INTO #Product_ID, #Type, #Name, #Price, #Supplier_ID
WHILE ##FETCH_STATUS = 0
BEGIN
INSERT INTO Product_Approval
VALUES ('#Product_ID', '#Type', '#Name', #Price, '#Supplier_ID')
END
FETCH NEXT FROM CCC INTO #Product_ID, #Type, #Name, #Price, #Supplier_ID
CLOSE CCC
DEALLOCATE CCC
END
I try to insert
INSERT INTO VW_ProductList(Product_ID , Type , Name , Price , Supplier_ID)
VALUES ('0101010' , 'Whiskey' , 'Varmunollan' , '90' , '00000')
but I get an error:
Msg 8114, Level 16, State 5, Procedure ProductApprovals, Line 76
Error converting data type varchar to real.
What did I do wrong?
One should avoid inserting data without listing columns explicitly. Your error is the exact reason why.
INSERT INTO Product_Approval
VALUES ('#Product_ID', '#Type', '#Name', #Price, '#Supplier_ID')
The order of columns in the table is different from the one in the INSERT statement.

Preparing Rules Based List for Promotions

I am trying to put together a promotions list based on a set of rules
In table #productdetail I need to pick up items that have available =1 or available =2 and expected <=08052019 and not_sellable =0
From #product table I need to join id with pid in #productdetail and coalesce (vendor_id to get the vendor_id
Using the vendor_id that I have got from the step above i need to get the vendorname from #vendor table..
From the #Asin table I need to select the latest lastconfirmedasin from the list of items selected in step 1 and also ensure that I should not pick up data from rows where disable =1
From the #pageviews table I need to get the number of pageviews perday for every item based on the lastconfirmedasin for the list of items selected in step 1
Here for every item if I have datetype =day and also datetype =month then I need to select data from datetype =day and select sum(pageviews)/count of days to get the sum of pageviews per day
Also if I have only datetype =month then I need to select (sum(pageviews)(count of rows that have month)/30
Then in the promo table I need to select the average promo% got for the items selected in step 1..The average promo% is calculated as avg((pre_promo_price-promo_price)/promo_price*100))...This will be calculated at the vendor level...So if the vendor has 3 items then the average promo% is calculated as avg([avg((pre_promo_price-promo_price)/promo_price*100)
Then the cost is taken based on if in the table #promo has the date in between or included in promo_start and promo_end (for example today's date being 08/16/2019 is included then the pre promo cost is to taken as the cost for that item otherwise the cost from #product_detail table should be taken
Then I am calculating the priority of the items by vendor(meaning within each vendor ) prioritizing the items based on the power(calculated as pageviewsperday*price)-Higher value of an item within a vendor gets the first priority
The tables:
create table #productdetail
(
itemid int,
available int,
expected date,
isnotsellable int,
pid int,
vendor_id varchar(50),
cost float,
price float)
insert into #productdetail values
('123','1',NULL,'1','1','201','180','200'),
('125','2','08/05/2019','0','1','NULL','40','60'),
('127','1',NULL,'0','1','201','60','80'),
('129','2',NULL,'0','2','203','80','100'),
('131','1',NULL,'0','2','203','70','90'),
('133','1',NULL,'1','2','203','90','110'),
('135','1',NULL,'0','7','206','110','130'),
('137','1',NULL,'0','8','207','120','140'),
('139','1',NULL,'0','8','207','30','50'),
('141','1',NULL,'0','8','207','40','60'),
('143','1',NULL,'0','11','210','60','80'),
('145','1',NULL,'0','11','210','70','90'),
('147','1',NULL,'1','11','210','50','70'),
('149','1',NULL,'0','11','210','20','40'),
('151','1',NULL,'0','15','211','30','50'),
('153','1',NULL,'0','16','NULL','40','60'),
('155','2','08/29/2019','1','15','211','60','80'),
('157','2','08/04/2019','1','18','216','30','50'),
('159','2','08/06/2019','0','19','217','20','40'),
('161','2','08/03/2019','0','20','218','60','80'),
('163','2','08/15/2019','0','21','NULL','90','110')
create table #product
(id int,
vendor_id varchar(50)
)
insert into #product values
('1','201'),
('1','201'),
('1','201'),
('2','203'),
('2','203'),
('2','203'),
('7','NULL'),
('8','207'),
('8','207'),
('8','207'),
('11','210'),
('11','210'),
('11','210'),
('11','210'),
('15','211'),
('15','211'),
('15','211'),
('16','206'),
('18','216'),
('19','NULL'),
('20','218'),
('21','219')
create table #vendor
(vendor_id varchar(50),
vendorname varchar(50)
)
insert into #vendor values
('201','cola'),
('203','foam'),
('207','fill'),
('210','falon'),
('211','chiran'),
('216','Hummer'),
('218','sulps'),
('219','culp'),
('217','jko'),
('206','JINCO')
create table #asin
(disable int,
item_id int,
lastconfirmed datetime2,
lastconfirmedasin varchar(50)
)
insert into #asin values
('0','123','12/19/18 10:19 PM','iopyu'),
('1','123','12/19/16 10:19 PM','hjyug'),
('1','125','5/19/19 10:19 PM','uirty'),
('0','125','12/19/16 10:19 PM','1yuio'),
('0','127','2/19/19 10:19 PM','klbnm'),
('1','127','12/19/18 10:19 PM','lopgh'),
('0','127','12/19/16 10:19 PM','nmbh'),
('0','129','11/19/16 10:19 PM','jklh'),
('0','131','11/19/19 10:19 PM','werat'),
('1','133','6/19/19 10:19 PM','vbnwe'),
('0','133','1/19/19 10:19 PM','mnwer'),
('0','133','11/19/17 10:19 PM','sdert'),
('0','135','6/19/19 10:19 PM','vbsdx'),
('0','137','6/19/17 10:19 PM','bnxct')
create table #pageviews
(startdate date,
lastconfirmedasin varchar(50),
noofpageviews int,
datetype varchar(20))
insert into #pageviews values
('05/08/2017','hjyug','102','day'),
('09/05/2017','hjyug','201','day'),
('10/05/2019','hjyug','1002','day'),
('09/05/2018','iopyu','345','month'),
('10/06/2018','iopyu','545','month'),
('06/05/2019','1yuio','300','day'),
('06/06/2019','1yuio','200','day'),
('09/04/2019','uirty','150','day'),
('11/4/2019','uirty','200','day'),
('12/4/2019','nmbh','300','day'),
('04/15/2019','lopgh','400','day'),
('04/15/2019','klbnm','500','day'),
('04/16/2019','klbnm','1000','day'),
('04/15/2019','jklh','600','day'),
('04/15/2019','werat','700','day'),
('04/15/2019','sdert','800','day'),
('04/15/2019','mnwer','900','day'),
('04/15/2019','vbnwe','1000','day'),
('04/15/2019','vbsdx','1100','day'),
('04/25/2019','vbsdx','3000','day'),
('04/15/2019','bnxct','1200','month'),
('05/31/2019','bnxct','2200','month'),
('04/16/2019','bnxct','90','day'),
('04/17/2019','bnxct','100','day'),
('04/18/2019','bnxct','120','day')
create table #promo
(itemid int,
promo_cost float,
pre_promo_cost float,
promo_start varchar(50),
promo_end varchar(50))
insert into #promo values
('123','214.7','220','10/08/2019','22/08/2019'),
('123','225','230','01/02/2018','15/02/2018'),
('125','400','430','12/08/2019','19/08/2019'),
('125','380','390','15/02/2019','30/02/2019'),
('127','120','140','15/03/2019','30/03/2019'),
('129','80','100','15/04/2019','30/04/2019'),
('129','110','120','01/04/2019','10/04/2019'),
('131','80','100','01/02/2019','15/02/2019'),
('131','110','120','10/01/2019','15/01/2019'),
('133','230','420','10/01/2019','15/01/2019'),
('135','250','440','10/01/2019','15/01/2019'),
('137','270','460','10/01/2019','15/01/2019'),
('139','290','480','10/01/2019','15/01/2019'),
('141','310','500','10/01/2019','15/01/2019'),
('143','330','520','10/01/2019','15/01/2019'),
('145','350','540','10/08/2019','22/08/2019')
create table #output
(itemid int,
available int,
expected date,
isnotsellable int,
pid int,
vendor_id varchar(50),
vendorname varchar(50),
lastconfirmedasin varchar(50),
pageviewsperday int,
promoavg float,
cost float,
price float,
[power] int,
[priority] int)
insert into #output values
('151','1',NULL,'0','15','211','chiran','','0','0','30','50','0','1'),
('127','1',NULL,'0','1','201','cola','klbnm','750','6.3','60','80','60000','1'),
('125','2','08/05/2019','0','1','201','cola','','0','0','430','60','0','2'),
('143','1',NULL,'0','11','210','falon','','0','0','60','80','0','1'),
('145','1',NULL,'0','11','210','falon','','0','0','540','90','0','2'),
('149','1',NULL,'0','11','210','falon','','0','0','20','40','0','3'),
('137','1',NULL,'0','8','207','fill','bnxct','103','65.73','120','140','14420','1'),
('139','1',NULL,'0','8','207','fill','','0','0','30','50','0','2'),
('141','1',NULL,'0','8','207','fill','','0','0','40','60','0','3'),
('131','1',NULL,'0','2','203','foam','werat','700','30.16','70','90','63000','1'),
('135','1',NULL,'0','7','206','JINCO','vbsdx','2050','76','110','130','266500','1'),
('153','1',NULL,'0','16','206','JINCO','','0','0','40','60','0','2'),
('161','2','08/03/2019','0','20','218','sulps','','0','0','60','80','0','1')
Code tried:
; WITH firstsel AS (
SELECT itemid, available, expected, isnotsellable, pid, vendor_id
FROM #productdetail
WHERE available = 1
AND isnotsellable = 0
UNION ALL
SELECT itemid, available, expected, isnotsellable, pid, vendor_id
FROM #productdetail
WHERE available = 2
AND isnotsellable = 0
AND expected <= '20190805'
), addvendorid AS (
SELECT fs.itemid, fs.available, fs.expected, fs.pid, fs.isnotsellable,
coalesce(fs.vendor_id, p.vendor_id) AS vendor_id
FROM firstsel fs
JOIN #product p ON fs.pid = p.id
), asinnumbered AS (
SELECT item_id, lastconfirmedasin, row_number() OVER (PARTITION BY item_id ORDER BY lastconfirmedasin DESC) AS rowno
FROM #asin
)
SELECT av.itemid, av.available, av.expected, av.isnotsellable, av.pid,
av.vendor_id, v.vendorname, an.lastconfirmedasin,
isnull(pv.pageviews, 0) AS pageviews, isnull(p.promoavg, 0) AS promoavg
FROM addvendorid av
JOIN #vendor v ON av.vendor_id = v.vendor_id
LEFT JOIN (asinnumbered an
JOIN (SELECT lastconfirmedasin, SUM(pageviews) AS pageviews
FROM #pageviews
GROUP BY lastconfirmedasin) AS pv ON an.lastconfirmedasin = pv.lastconfirmedasin)
ON an.item_id = av.itemid
AND an.rowno = 1
LEFT JOIN (SELECT itemid, avg((pre_promo_price-promo_price)/promo_price*100) AS promoavg
FROM #promo
GROUP BY itemid) AS p ON p.itemid = av.itemid
ORDER BY av.itemid

Is there any way to speed up this query capturing customer information? Is there a better method for this data?

I have a long query but I'll try to break it down into it's parts.
First, there are the variables,
DECLARE #LocalCompanyCode VARCHAR(5)
SET #LocalCompanyCode = '09'
DECLARE #LocalDivisionCode VARCHAR(5)
SET #LocalDivisionCode = '001'
DECLARE #CustomerBaseFromDate DATETIME --CustomerBase
SET #CustomerBaseFromDate = '1/1/2019'
DECLARE #CustomerBaseToDate DATETIME
SET #CustomerBaseToDate = '5/30/2019'
DECLARE #RecurringBaseFromDate DATETIME --Recurring Base
SET #RecurringBaseFromDate = '1/1/2018'
DECLARE #RecurringBaseToDate DATETIME
SET #RecurringBaseToDate = '1/1/2019'
DECLARE #LifetimeBaseFromDate DATETIME --Lifetime Base
SET #LifetimeBaseFromDate = '1/1/2015'
DECLARE #LifetimeBaseToDate DATETIME
SET #LifetimeBaseToDate = '1/1/2018'
Company Code and Division Code select which Company we are running this query for. Customer Base will be the base of customers that we will be looking at. So in our example, it is the past 5 months.
So for all customers in the past 5 months, we will look in our Recurring Base. This is modular so we can control what time before we consider a customer "lost". We will compare against the recurring base and see how many customers are new customers who only ordered once over the customer and recurring base, and how many customers are recurring customers, those who ordered multiple times over those periods.
Then we will also have our Lifetime base. We will check customers who ordered once or more in our lifetime base, did not order in our recurring base, but did order again in our 5 months customer base. This calculates how many customers are "Reactivated" or were considered lost but bought with us again recently.
Then I declare the four tables
DECLARE #FullBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountBase NUMERIC,
TotalOrdersBase NUMERIC
)
DECLARE #LifetimeBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountLifetimeBase NUMERIC,
TotalOrdersLifetimeBase NUMERIC
)
DECLARE #RecurringBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountRecurringBase NUMERIC,
TotalOrdersRecurringBase NUMERIC
)
DECLARE #CustomerBase TABLE
(
Date_Created DATE,
Company_Code VARCHAR(2),
Division_Code VARCHAR(3),
Invoice_Number VARCHAR(50),
CUST_PO VARCHAR(50),
Total_Quantity NUMERIC,
TotalPrice MONEY,
City VARCHAR(50),
State VARCHAR(50),
Zip VARCHAR(50),
CountryCode VARCHAR(50),
Month NUMERIC,
CustomerEmail VARCHAR(MAX),
OrderCountCustomerBase NUMERIC,
TotalOrdersCustomerBase NUMERIC
)
Then I insert all of our customers into the "FullBase", from the beginning of the Lifetime Base, to the end of the Customer Base
INSERT INTO #FullBase
SELECT Orders.Date_Created
,Orders.Company_Code
,Orders.Division_Code
,Orders.Invoice_Number
,Orders.CUST_PO
,Orders.Total_Quantity
,Orders.Total
,Orders.City
,Orders.State
,Orders.Zip
,Orders.CountryCode
,Orders.Month
,Orders.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountBase
,Count(*) over (partition by CustomerEmail) TotalOrdersBase
FROM(
Select
CONVERT(Date, OrderCreated) Date_Created
,CONCAT ('0', LEFT(OrderName,1)) Company_Code
,CONCAT ('00', RIGHT(LEFT(OrderName,2),1)) Division_Code
,InvoiceNumber Invoice_Number
,OrderName CUST_PO
,1 Total_Quantity
,TotalPrice Total
,ShippingCity City
,CASE WHEN ShippingCountryCode <> 'US' THEN 'INT' ELSE ShippingProvinceCode END State
,ShippingZip Zip
,ShippingCountryCode CountryCode
,Month( OrderCreated) Month
,Email CustomerEmail
From [SHOPIFY].[shopify_moret].[dbo].orderwrappers O
Where CONVERT(Date, O.OrderCreated) >= Convert(datetime, '05/29/2019')
AND CONCAT ('0', LEFT(O.OrderName,1)) = #LocalCompanyCode--'09'
AND CONCAT ('00', RIGHT(LEFT(O.OrderName,2),1)) = #LocalDivisionCode --'001'
UNION
Select
Archive.Date_Created
,Archive.Company_Code
,Archive.Division_Code
,Archive.Invoice_Number
,('91'+Archive.CUST_PO) CUST_PO
,Archive.Total_Quantity
,Archive.Total
,Archive.City
,Archive.State
,Archive.Zip
,Archive.Country
,Archive.Month
,Archive.CustomerEmail
FROM SpraygroundArchivedOrders Archive
Where Archive.Date_Created < Convert(datetime, '05/29/2019')
) Orders
Where Orders.Date_Created BETWEEN #LifetimeBaseFromDate AND #CustomerBaseToDate
Here is an example of how the data looks like:
https://docs.google.com/spreadsheets/d/1wnjVHcPHHnugywa7Qz-aqctaD4cDI5DxehNna0TyaFU/edit?usp=sharing
Then I use this full base to populate the three other tables with orders from their range.
INSERT INTO #LifetimeBase
SELECT
F.Date_Created
,F.Company_Code
,F.Division_Code
,F.Invoice_Number
,F.CUST_PO
,F.Total_Quantity
,F.TotalPrice
,F.City
,F.State
,F.Zip
,F.CountryCode
,F.Month
,F.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountLifetimeBase
,Count(*) over (partition by CustomerEmail) TotalOrdersLifetimeBase
FROM #FullBase F
Where F.Date_Created BETWEEN #LifetimeBaseFromDate AND #LifetimeBaseToDate
INSERT INTO #RecurringBase
SELECT
F.Date_Created
,F.Company_Code
,F.Division_Code
,F.Invoice_Number
,F.CUST_PO
,F.Total_Quantity
,F.TotalPrice
,F.City
,F.State
,F.Zip
,F.CountryCode
,F.Month
,F.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountRecurringBase
,Count(*) over (partition by CustomerEmail) TotalOrdersRecurringBase
FROM #FullBase F
Where F.Date_Created BETWEEN #RecurringBaseFromDate AND #RecurringBaseToDate
INSERT INTO #CustomerBase
SELECT
F.Date_Created
,F.Company_Code
,F.Division_Code
,F.Invoice_Number
,F.CUST_PO
,F.Total_Quantity
,F.TotalPrice
,F.City
,F.State
,F.Zip
,F.CountryCode
,F.Month
,F.CustomerEmail
,Row_Number() over (partition by CustomerEmail order by Date_Created asc) OrderCountCustomerBase
,Count(*) over (partition by CustomerEmail) TotalOrdersCustomerBase
FROM #FullBase F
Where F.Date_Created BETWEEN #CustomerBaseFromDate AND #CustomerBaseToDate
If I just select * from an of the bases, including the full base, the query only takes a few seconds to run and returns 140,000 rows (for the #FullBase) of all customer orders.
However, the final part of this query, which is the part that runs, takes 10 minutes to run for my 6 months of customers
SELECT
CC.CustomerEmail
,CC.TotalOrdersCustomerBase
,RC.TotalOrdersRecurringBase
,LC.TotalOrdersLifetimeBase
From
(
SELECT DISTINCT
C.CustomerEmail
,C.TotalOrdersCustomerBase
FROM
#CustomerBase C
) CC
LEFT JOIN
(
SELECT DISTINCT
R.CustomerEmail
,R.TotalOrdersRecurringBase
FROM
#RecurringBase R
) RC ON CC.CustomerEmail = RC.CustomerEmail
LEFT JOIN
(
SELECT DISTINCT
L.CustomerEmail
,L.TotalOrdersLifetimeBase
FROM
#LifetimeBase L
) LC ON CC.CustomerEmail = LC.CustomerEmail
Does anyone have any tips for me at all? Is there any better way to do this?

SQL Server: using Temporary table

Would I get the value of temporary table from calling the store procedure?
Since #productTable is a temporary table, can I EXEC [dbo.FindProductTotal]
and get the value in [dbo.updateProductTotal]?
It does not seem to me it works. If not, is there a way to get the value
from the result of the first temp table from executing in the 2nd store procedure? The real coding is more complicated so it's not i didn't know just doing simple insert and update tables. I need to know if this way of coding would work for my real coding.
ALTER PROCEDURE dbo.FindProductTotal (#ProductID int)
AS
DECLARE #productTable TABLE (subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)
insert into #productTable
select productID, productCode, productDesc, chargeAmt
from product
where region='CA'
--calculate charge here
UPDATE #productTable
SET chargeAmt = (calculation here)
where ProductID = #ProductID
ALTER PROCEDURE dbo.updateProductTotal (#ProductID int)
AS
DECLARE #productTable TABLE (subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)
EXEC dbo.FindProductTotal #ProductID = 123456
UPDATE dbo.Manufacture
SET productChgAmt = p.chargeAmt
FROM #productTable p
WHERE productID = #ProductID
CREATE PROCEDURE dbo.FindProductTotal (#ProductID int)
AS
IF SELECT OBJECT_ID('tempdb..##productTable') IS NULL
DROP TABLE ##productTable
CREATE TABLE ##productTable(subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)
--DECLARE #productTable TABLE (subProductID int, productCode varchar(10), productDesc varchar(25), chargeAmt money)
insert into ##productTable
select productID, productCode, productDesc, chargeAmt
from product
where region='CA'
--calculate charge here
UPDATE ##productTable
SET chargeAmt = (calculation here)
where ProductID = #ProductID
CREATE PROCEDURE dbo.updateProductTotal (#ProductID int)
AS
EXEC dbo.FindProductTotal #ProductID = 123456
UPDATE dbo.Manufacture
SET productChgAmt = p.chargeAmt
FROM ##productTable p
WHERE productID = #ProductID