Here's a little puzzle that's cooking my brains. What can be done to solve?
Guys
Insightful wisdoms herein, the following small problem has landed with me.
Suppose I run a bank and there is a big table of financial transactions. Every credit card therein gains points for every dollar they spend. Keep it simple, 1 point for $1.
A third party company sells gifts in exchange for the points, and we send a file every night of the number of points that must be added to our member's account over at their store.. Joe Schmoe spent $200 today, add 200 points to his account. This part is easy
Problem comes when something is refunded with us (the "bank"), the points balance at the third party CANNOT be decremented (dumb, I know), so that $1000 purchase of a TV netted Joe 1000 points even though he took the TV back a week later and got his money back. Refunds can occur at any time, and you cannot assume that an item will be refunded properly between two runs of the report that prepares the points file (i.e. before sending over the points, you can't add up all the spends, add up all the refunds and send the difference as the points, because some refunds won't have happened yet).
The proposal is that as a result, Joe Schmoe's card should go into "points debt" of 1000 points, and we only start sending new points to the third party once Joe has spent more than $1000 again.. So points updates should be paused and if Joe spends over the next 4 days:
500
300
150
90
then the last transaction for $90 on day 4 clears his outstanding points debt of -50 points and we should send 40 points over to the third party company
If such a problem could be entirely solved within a SELECT, then that's cool but I have development ability on the reports system that runs the SQLs so I can add options to run other query types after, or add support for returning a cursor and implementing this in PL/SQL with lots more advanced logic
Regards
cj
I am unsure of the efficiency of this and it is hard to tell if this is what you need without example data and output, but I think this is what you are looking for.
Suppose we have sample data like this (where negative numbers are normal transactions - debits, and positive numbers are refunds or credits back to the account.
with t as (
select 1 id, sysdate - 5 dte, -1000 amount from dual UNION ALL
select 1, sysdate - 4, -500 from dual UNION ALL
select 1, sysdate - 3, 500 from dual UNION ALL
select 1, sysdate - 2, -250 from dual UNION ALL
select 1, sysdate - 1, -300 from dual)
select * from t;
ID DTE AMOUNT
1 28-FEB-09 -1000
1 01-MAR-09 -500
1 02-MAR-09 500
1 03-MAR-09 -250
1 04-MAR-09 -300
5 rows selectedFirst step is to get the actual number of points the customer should have at each date.
with t as (
select 1 id, sysdate - 5 dte, -1000 amount from dual UNION ALL
select 1, sysdate - 4, -500 from dual UNION ALL
select 1, sysdate - 3, 500 from dual UNION ALL
select 1, sysdate - 2, -250 from dual UNION ALL
select 1, sysdate - 1, -300 from dual)
select t.*,
sum(-amount) over (partition by id order by dte rows unbounded preceding) actual
from t;
ID DTE AMOUNT ACTUAL
1 28-FEB-09 -1000 1000
1 01-MAR-09 -500 1500
1 02-MAR-09 500 1000
1 03-MAR-09 -250 1250
1 04-MAR-09 -300 1550
5 rows selectedThe next step is slightly more tricky - I am going to use the max() analytic function combined with a sliding window so we can eliminate the reduction in the actual. See below:
with t as (
select 1 id, sysdate - 5 dte, -1000 amount from dual UNION ALL
select 1, sysdate - 4, -500 from dual UNION ALL
select 1, sysdate - 3, 500 from dual UNION ALL
select 1, sysdate - 2, -250 from dual UNION ALL
select 1, sysdate - 1, -300 from dual)
select b.*,
max(actual) over (partition by id order by dte rows unbounded preceding) curr
from (select t.*,
sum(-amount) over (partition by id order by dte rows unbounded preceding) actual
from t) b;
ID DTE AMOUNT ACTUAL CURR
1 28-FEB-09 -1000 1000 1000
1 01-MAR-09 -500 1500 1500
1 02-MAR-09 500 1000 1500
1 03-MAR-09 -250 1250 1500
1 04-MAR-09 -300 1550 1550
5 rows selectedSo as you can see, this gives the total number of points at each date that should have been sent to the customer.
And so finally, we use lag on that result and find the difference (using NVL to handle the first case):
with t as (
select 1 id, sysdate - 5 dte, -1000 amount from dual UNION ALL
select 1, sysdate - 4, -500 from dual UNION ALL
select 1, sysdate - 3, 500 from dual UNION ALL
select 1, sysdate - 2, -250 from dual UNION ALL
select 1, sysdate - 1, -300 from dual)
select id,
dte,
amount,
curr - nvl(lag(curr) over (partition by id order by dte),0) diff
from (select b.*,
max(actual) over (partition by id order by dte rows unbounded preceding) curr
from (select t.*,
sum(-amount) over (partition by id order by dte rows unbounded preceding) actual
from t) b) d;
ID DTE AMOUNT DIFF
1 28-FEB-09 -1000 1000
1 01-MAR-09 -500 500
1 02-MAR-09 500 0
1 03-MAR-09 -250 0
1 04-MAR-09 -300 50
5 rows selectedAnd that should give the points to send at each date.
Similar Messages
-
My ipad3 gets very hot, why is that? What can be done to solve this issue.
Can someone help me figure out how to stop my iPad from getting so hot.
Don't use any sleeve. The back cover is acting as a heat-sink to take away the heat from the CPU.
Paste a few nice stickers on the back to prevent scratches. -
i am trying to set up iTunes match on my apple tv, but it just keeps showing that it is loading, it has been running for over an hour, what can be done here, it has gone with no problem on my iPhone, iPad but not the tv
Same here and this is driving me crazy. I can buy, but can't upgrade. Grrr.
-
I updated my iPhone 5s to the new iOS 7.1 and now I have a little icon that I have no idea what it is it up in the same corner as my battery
Maybe you could describe what the icon looks like?
-
Hi my name is Hajk and i got a friend that has a problem with her macbook, she gets a blue screen and cant log on. So i told her if she had an reinstall disk so that i could reinstall for her but she didn't get one with the macbook. what can she do ?
If this is a fairly new Mac running either v10.7 Lion or v10.8 Mountain Lion she can use Lion or Mountain Recovery to repair the startup disk or reinstall OS X.
-
My username (email address) has changed. Apps that I purchased with the old username ca not be updated as the password from the old username no longer works and apple can not send me info to an addres that no longer exists. What can I do?
Try here...
Frequently Asked Questions About Apple ID -
Trying to download CS6 on my mac pro. when i click on Download, under my orders, it brings up an error page: JRun Servlet error, 413 Header Length to Large. It's been doing this each time I've tried that last few days. What can I do to download this?
clear your adobe.com cookies or use a different browser.
-
Hi wondering if anyone can help me, i have an itouch 4 gen and the front cover has started to come away, it hasn't been dropped or anything just started to come away ( although i did notice that it was heating up drastically ) looks to me like the glue has dyed up and as usual the guarantee had not long ran out. Has anyone else has this problem if so what can be done about it. Thanks
SOme users have reported that their screen has come a little loose but very few.
Try fixing it yourself
iPod Touch Repair – iFixit -
A FCP shooter gave me .mov's that PP CS5 cant open, what can I do? I guess she rewrapped the files because as they try to import into cs5 a FCP loading diologe box opens stating: Importing FCP XML.... Afterwards it says i dont have the codec necc.
I'm PC based only.
Here is a link to a short clip that can be downloaded.
I'm really stuck here guys. Cant reach the shooter.
The 5 second clip can be seen & downloaded from Vimeo:
http://vimeo.com/27230969If you tried XD Decode you must think that they originated as XDCam then, right? That is the codec I had to buy and once it was installed everything was transparent and normal. So I'm not sure what the crashing is all about, assuming your system meets the minimal tech. requirements. I'd give it another try if you haven't already.
Your best hope would be to locate the original camera files, which could very well have been deleted following the .mov copying via the Mac. I've had no problem with native XDCam in Premiere. The next best solution might be to have that FCP user do the conversions to another file format you can use. Hopefully there aren't thousands of them.
Firstodd, that's very interesting that your Avid MC was able to work with those files. I just found this from an Avid doc of last year: "If you want to play the exported movie in QuickTime, you need to purchase a third-party QuickTime plug-in. Avid recommends the Calibrated{Q} XD Decode from Calibrated Software"
Still, for the $80 I spent a year ago, XD Decode was well worth it in time saved. I see the price has gone up $50 since then though. -
My ipod touch 3,5 gen works on 4.2.1 softwear version and need to update to 4.3 so I can run my app Numbers that I've just bought yesterday.It doesn't update automatically. I'm in Portugal. What can I do to solve this?
What model iPod do you have? A 2G only can go to 4.2.1. The 3G and 4G can go to the latest. 4.3.3. Identify your iPod here:
Identifying iPod models
What computer OS and version do you have? What version of iTunes is installed? -
I have a mac pro that is running slow. what can be done?
I have a mac pro that is running slow. what can be done?
Read the last two replies in this thread: https://discussions.apple.com/message/24218682#24218682
Reply here, not there. -
This happened many times today. There is no specific page. I usually have a page open and try to redirect from that page. It shows "connecting", but never does. I have run many scans which all turn up clean. Why is this happening and what can be done to fix it?
Maybe here:
http://www.windowserrorhelps.com/itunes-error-11-how-can-i-fix-itunes-error-11-i nstantly
Is the iPod jailbroken? -
I have a annual plan, it supposed must be used in two terminals, but i haven´t been able to install any app in a second computer, it always says that the app is only available as a trial download. What can I do to solve this issue, and be able to use my paid plan in two computers?
Hi Susan,
Please refer to the help document to fix this issue:
Creative Cloud applications unexpectedly revert to trial mode | CS6, CCM
You may also refer to the thread as below:
creative cloud software says my free trial has expired, but I have a paid subscription
Regards,
Sheena -
So I have a file on my desktop and I cannot open it, move it to a folder, or delete it from my mac. I dont know what it has in it, and that makes me suspicious. What can I do to remove this unremovable file from my computer? Is it a virus?
First, I would recommend repairing the hard drive with Disk Utility.
If that doesn't fix the problem, there's a very dangerous command you can execute in the Terminal. This is very hazardous, because a simple typo can result in very drastic consequences. I have seen people erase their entire hard drive by putting a space in the wrong place! So, please follow the directions I'm going to give you very carefully!
Open the Terminal, which is found in the Utilities folder in the Applications folder. In the Terminal, enter the following:
sudo rm -f
Make sure to put a space after the "-f"! Then, drag the troublesome file from the Finder and drop it on the Terminal window. That should insert the path to the file in the command. Then go back to the Terminal and press return. You will be asked for your password, and when you type it, nothing will be shown as a security measure. Press return again after entering your password. The file should be deleted. -
I recently upgraded to 10.6.7 and Safari 5.05. Safari now has a long load time when you initially boot it up and when selecting a new tab. Other than that it works fine. What can I do ?
Check Safari / Preferences - Extensions. If you have any installed, turn that off, relaunch Safari.
If it's not Extensions, check for third party Safari add ons.
http://support.apple.com/kb/TS3230?viewlocale=en_US
Maybe you are looking for
-
Here's my disappointing story: I bought one of the 24" iMacs the day it came out. About 10 days later I received it and one of the first things I did was install windows XP. I followed the bootcamp/xp installation procedure exactly but once through t
-
Story - not able to log in and retrieve written work
I have cs 5.5 production premium and have been using story for a lot of written work and script writing over the summer. I didnt log in for a month or so and now it says i need to upgrade and i click the link and it just takes me to the adobe product
-
File transfter issue - Lenovo S860
Hello Geeks, I have detected one silly but quite serious issue in my lenovo S860 smartphone. Once a while I was trasfering a music file , I have chosen send via 'WhatsApp' option and marked on 'Always'. Now problem is whenever i want to trasfter any
-
[SOLVED} linux-dvb-apps initial scan files
Anyone know where to find the linux-dvb-apps initial scan files. They used to be under /usr/share/dvb/, but the latest linux-dvb-apps package just lists contents in /usr/share/dvb/av7110_loadkeys/ Failing that has anyone got a decent scan file for uk
-
The printer that comes up when i hit print is not my default printer
I can print from my documents or desktop but when I open firefox and try to print it opens a print window with a fax as my printer. I can hit the down arrow and see my printer but it will not allow me to select it. My default is the correct printer