Wrox Programmer Forums
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2009, 02:17 PM
Authorized User
 
Join Date: Jun 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default Update Command

Any idea how to randomly update table field with multiple subjects on % basis

For example: We have a list of 1000 contacts (email addresses) and want to send email with different subjects on percentage basis.

Subject 1 : GPS on sale ------------ (user selected - 30% in front end)
Subject 2: GPS Free------------ (user selected - 50% in front end )
Subject 3: GPS - Upto 70% off------------ (user selected - 20% in front end)

Current Scenairo and it is working for 1 subject.

1. INSERT INTO distributionqueue (subject, fromname, fromemail, tofirstname,tolastname, toemail, company, datetobesent, sentstatus etc...)
SELECT 'GPS on sale' subject, fromname, fromemail, tofirstname,tolastname, toemail, company, '2009-05-20 10:00:00' FROM AddressBook where contactlistid= 1;
2. System will send email to 1000 contacts with the same subject (subject1)

Requirement :

We need to update distributionqueue table randomly with following criteria.
50% (500) rows should get updated with Subject 2
20% (200) rows should get updated with Subject 3

How can i write Where Clause in update command

Subject2: UPDATE distributionqueue SET Subject='GPS Free' WHERE EmailID=1
Subject3: UPDATE distributionqueue SET Subject='GPS - Upto 70% off' WHERE EmailID=1

Hope i explained well, let me know if you have any questions.

I appreciate your great help
 
Old May 21st, 2009, 03:49 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

What database are you using?

Pretty easy to do this with SQL Server.

Harder with other DBs.
 
Old May 21st, 2009, 07:31 AM
Authorized User
 
Join Date: Jun 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Using MySQL database
__________________
Rams
 
Old May 21st, 2009, 10:34 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
user selected - 30% in front end
user selected - 50% in front end
user selected - 20% in front end
Do you have the User's selection, saved in the DB somewhere?
__________________
- Vijay G
 
Old May 21st, 2009, 01:08 PM
Authorized User
 
Join Date: Jun 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes, Saving Subject 1, 2, 3, 4,5 and Percent 1,2,3,4,5 in Distribution setup table
__________________
Rams
 
Old May 21st, 2009, 03:46 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Okay, had to check MySQL docs, and fortunately MySQL behaves well with the RAND( ) function.

So it's really simple:

First of all, clean out the field you want to randomly set:
Code:
UPDATE distributionqueue SET Subject=NULL;
Find out how many records are in that table:
Code:
SELECT COUNT(*) FROM distributionqueue
record that number (e.g., in a variable in PHP/ASP/whatever you use)

and then simply do:
Code:
UPDATE distributionqueue SET Subject='XYZ'
ORDER BY RAND()
LIMIT (thatCount * percentage)
In other words, if you have 800 records in the table, and you want to set 50% of them to "GPS - Upto 70% off", you would do
Code:
UPDATE distributionqueue SET Subject='GPS - Upto 70% off'
ORDER BY RAND()
LIMIT 400
Now, for the next subject, say "GPS on sale" where you wanted 30% of the records to be changed:
Code:
UPDATE distributionqueue SET Subject='GPS on sale'
WHERE Subject IS NULL
ORDER BY RAND()
LIMIT 240  // 240 is 30% of 800
Obviously, you could set up a stored procedure to do all this, excepting only the setting all the Subject fields to NULL to get started.

This is off the top of my head, utterly untested (I don't happen to have MySQL on this computer right now):
Code:
delimiter //

CREATE PROCEDURE setRandomSubject(
    subj VARCHAR(255),
    percentage FLOAT )
BEGIN
SELECT COUNT(*) INTO allCount FROM distributionqueue;

UPDATE distributionqueue SET Subject = subj
WHERE Subject IS NULL
ORDER BY RAND()
LIMIT percentage * allCount;
END;

delimiter ;
I *think* that works. If not, if the LIMIT can't be used like that with UPDATE (but I've seen it used similarly), then we could do it another way:
Code:
UPDATE distributionqueue SET Subject = subj
WHERE emailID IN (
    SELECT emailID FROM distributionqueue
    WHERE Subject IS NULL
    ORDER BY RAND()
    LIMIT percentage * allCount
    );
As I said, untested, but give the idea a shot.
 
Old May 21st, 2009, 03:49 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Just noticed that the expression used with LIMIT might produce a non-integer number, so you probably want to cast or conver that to INT.

And note that percentage is passed in as a fraction (e.g., 0.4) instead of integer (that is, *NOT* 40%). You could change that, of course, if you wanted.
 
Old May 22nd, 2009, 08:21 AM
Authorized User
 
Join Date: Jun 2006
Posts: 85
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, Thank you.

I will execute your solution today.

I came up with different calculation yesterday. Still am testing the results.

Here it is:
For ex: I have 24823 rows

By Default all 24823 rows are with Subject 1.

/* Subject 2 - Formula */

Select * from distributionqueue
WHERE EmailID=875
AND (Seq - MINSeq in this table) mod TotalRows in this table BETWEEN TotalRows * SUB1Percent/100 and TotalRows * (SUB1Percent+SUB2Percent)/100 -1 order by seq;

/* Subject 2 Update Query - Updating 35% of the rows */

UPDATE distributionqueue SET Subject = 'S2' WHERE EmailID=875
AND (Seq - 24) mod 24823 BETWEEN 24823 * 45/100 and 24823 * (45+35)/100 -1 ;
----------------------------

/* Subject 3 - Formula */

Select Seq, EmailID,ScheduledTime, subject,Email_details FROM distributionqueue
WHERE EmailID=875
AND (Seq - nMINSeq) mod TotalRows BETWEEN TotalRows * (SUB1Percent+SUB2Percent)/100 and TotalRows * (SUB1Percent+SUB2Percent+SUB3Percent)/100 -1 ;

/* Subject 3 Update Query - Updating 20% of the rows */

UPDATE distributionqueue SET Subject = 'S3' WHERE EmailID=875
AND (Seq - 24) mod 24823 BETWEEN 24823 * (45+20)/100 and 24823 * (45+35+20)/100 -1 ;


I appreciate your great help.
__________________
Rams





Similar Threads
Thread Thread Starter Forum Replies Last Post
Update command yogeshyl Oracle 0 November 28th, 2007 08:05 AM
Invalid UPDATE command Mr. Vage SQL Language 4 April 14th, 2007 05:59 PM
Update command in Datagrid chnswam C# 0 March 17th, 2006 03:04 AM
Update Command In DataGrid RPG SEARCH ASP.NET 1.0 and 1.1 Basics 9 February 21st, 2005 09:20 AM
ADO Command Update hcweb Classic ASP Basics 2 January 14th, 2004 06:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.