Showing posts with label sales. Show all posts
Showing posts with label sales. Show all posts

Wednesday, March 21, 2012

From Select to Update

I have a sales table that has a customer_number, salesman_number and sales data (like item, quantity, etc), there is a customer table that has the customer_number, name, address, etc. and salesman_number, with the passign of time salesmen come and go and the customer service department is in charge of modifying the customer table and assign the new salesman number to them. All the previous sales have to be reasigned to this new salesman even though it wasnt him the one that sold the items (I know, I know, thats the way the company requieres it, this is an old system). Now to my question:

I have this select query that gives me the rows from table sales that have to be modified:

SELECT C.sales_rep,S.salesrep,S.*
FROM SALES AS S
INNER JOIN customer AS C
ON C.Active_Flag = 'A' AND
C.sales_rep != 'XXXX' AND
C.site_code = S.site_code AND
C.cust_no = S.cust_no AND
C.cust_sffx = S.cust_sffx AND
C.division = S.division AND
C.sales_rep != S.salesrep
WHERE S.month = 2 AND
S.year = 2005
ORDER BY C.sales_rep

I just want to know if my approach to convert this query from an update to a select is ok:

UPDATE SALES AS S
SET S.salesrep = C.sales_rep
INNER JOIN customer AS C
ON C.Active_Flag = 'A' AND
C.sales_rep != 'XXXX' AND
C.site_code = S.site_code AND
C.cust_no = S.cust_no AND
C.cust_sffx = S.cust_sffx AND
C.division = S.division AND
C.sales_rep != S.salesrep
WHERE S.month = 1 AND
S.year = 2004

Thanks for your help

Luis TorresMade some changes to the SQL and it parsed ok, going to try it now:

UPDATE SALES
SET SALES.salesrep = C.sales_rep
FROM SALES AS S
INNER JOIN customer AS C
ON C.Active_Flag = 'A' AND
C.sales_rep != 'XXXX' AND
C.site_code = SALES.site_code AND
C.cust_no = SALES.cust_no AND
C.cust_sffx = SALES.cust_sffx AND
C.division = SALES.division AND
C.sales_rep != SALES.salesrep
WHERE SALES.month = 1 AND
SALES.year = 2004|||Final version, it worked perfectly :)

UPDATE SALES
SET SALES.salesrep = C.sales_rep
FROM SALES AS S
INNER JOIN customer AS C
ON C.Active_Flag = 'A' AND
C.sales_rep != 'XXXX' AND
C.site_code = S.site_code AND
C.cust_no = S.cust_no AND
C.cust_sffx = S.cust_sffx AND
C.division = S.division AND
C.sales_rep != S.salesrep
WHERE S.month = 1 AND
S.year = 2004|||Glad we could help. ;)|||Lol :) :) :)|||Can you believe that the last dba had a stored procedure with 2 cursors (one inside the other) to do this? The process lasted 21+ hours and it was part of our monthly load. I managed to reduce the time from 23+ hours total to 45 minutes, all the managers are jumping up and down ;) hopefully they will offer me a permanent position here when my contract ends in May, I really enjoy working for this company. :)|||Good luck!

I work as a consultant, and given the number of bad DBAs circulating around the workforce, its pretty common to be able to walk into a company and look like a miracle worker!

Monday, March 19, 2012

From Cursors to a simple Select

Hi all

There is a stored procedure that updates a "sales" table with the current "sales representative" taken from the "customers" table. I'm changing this mess of cursors into a simple update, my first approach was just to do a select (instead of update) just to verify that the rows selected were the ones that really needed to be changed. The select is very simple but is returning a cartesian product instead of just the 200+ (aprox) rows. I would appreciate if someone took a quick look at this select and see what I'm missing :) (I'm including the code for the cursor as well as the code for the select)

CURSOR:

CREATE PROCEDURE [ReCast_Salesman] AS
-- Recast salesman

Declare @.vsite_code varchar(2)
Declare @.vcustno varchar(10)
Declare @.vsalrep char(4)
Declare @.vcustfx smallint
Declare @.Ssite_code varchar(2)
Declare @.Scustno varchar(10)
Declare @.Ssalrep char(4)
Declare @.Scustfx smallint
Declare @.i int
Declare @.intcnt int
Declare @.cnt int
Declare @.vprodate datetime
Declare @.vpro varchar(20)
Declare @.vmesg varchar(30)
Declare @.vmesg2 varchar(30)
Declare @.vmesg3 varchar(30)
Declare @.UpdFlag varchar(1)
Declare @.Ucnt int
Declare @.icnt int

Set @.i = 0
Set @.cnt = 0
Set @.ucnt = 0
Set @.icnt = 0
Set @.UpdFlag = 'N'

Declare Customer_cur Cursor for
Select site_code, cust_no, cust_sffx, sales_rep
From Customer
where Active_Flag = 'A'
and sales_rep <> 'XXXX'
Order by site_code, cust_no, cust_sffx
For Read only

Open Customer_Cur

Fetch Next from Customer_Cur
into @.vsite_code, @.vcustno, @.vcustfx, @.vsalrep

While @.@.FETCH_STATUS = 0
Begin

Declare sales_cur Cursor for
Select site_code, cust_no, cust_sffx, salesrep
From Sales
Where site_code = @.vsite_code
and cust_no = @.vcustno
and cust_sffx = @.vcustfx
and salesrep <> @.vsalrep
Order by Site_code, cust_no, cust_sffx
For Update

Open Sales_cur

Begin

Fetch Next
from sales_cur
into @.ssite_code, @.scustno, @.scustfx, @.Ssalrep

While (@.@.fetch_status = 0)
Begin

-- If @.vsalrep <> @.Ssalrep

-- Begin
Update Sales
set salesrep = @.vsalrep
Where current of Sales_cur

set @.ucnt = @.ucnt + 1
-- End

Fetch Next from sales_cur
into @.ssite_code, @.scustno, @.scustfx, @.Ssalrep

End

FetchNext:
deallocate sales_cur

Fetch Next
from Customer_Cur
into @.vsite_code, @.vcustno, @.vcustfx, @.vsalrep

End

End

set @.vprodate = getdate()
set @.vpro = 'Recast Salesman '
set @.vmesg = Str(@.ucnt) + ' Records Changed'

Insert into Batch
(batchdate, process, message )
Values (@.vprodate, @.vpro, @.vmesg )

Close Customer_Cur
deallocate Customer_Cur

SELECT:

use salesdatamart;
Select A.site_code,
A.cust_no,
A.cust_sffx,
A.salesrep,
From Sales As A
Inner Join Customer As B
On A.site_code = B.site_code AND
A.cust_no = B.cust_no AND
A.cust_sffx = B.cust_sffx AND
A.salesrep != B.sales_rep AND
B.sales_rep != 'XXXX' AND
B.active_flag = 'A'

Thanks in advance for your help :)

Luis TorresDDL and sample data?