NewtonianB
asked on
SQL SELECT calculate probabilities
I'm trying to get some probabilities for my data
(don't try to make too much sense out of this problem as I simplified it alot, the car brand is irrevelant in my analysis)
I want to calculate probabilities of the price paid given the color of the car
So for each "color" I want to count from my database how many of each "price" was paid.
The only way I can think of doing this right now is writing a script that will call SQL function and do this for me but it looks like a lot of work.
Do you know of any sql tricks I could use to help me with this?
How would I modify my select query to do this?
----------------------
i.e. all data in data base:
red 20000
yellow 20000
red 20000
black 20000
probabilities i want to calculate and retrieve:
red (20000) = 2/4
red (30000) = 0/4
red (40000) = 0/4
black (20000) = 1/4
black (30000) = 0/4
black (40000) = 0/4
yellow (20000) = 1/4
yellow (30000) = 0/4
yellow (40000) = 0/4
mysql> SELECT cr.name, pr.name, cl.name,
-> FROM colour cl, priceround pr, cars cr
-> WHERE cl.id = cr.colour
-> and pr.id = cr. priceround;
+------------------------- -+-------- ---+------ ---------- -+
| name | name | name |
+------------------------- -+-------- ---+------ ---------- -+
| Honda | red | 20000 |
| BMW | yellow | 20000 |
| Toyota | red | 20000 |
| Ford | white | 20000 |
....
priceround table
+----+-----------------+
| id | name |
+----+-----------------+
| 0 | 20000 |
| 1 | 30000 |
| 2 | 40000 |
colour table
+----+-----------+
| id | name |
+----+-----------+
| 1 | red |
| 2 | black |
| 3 | yellow |
(don't try to make too much sense out of this problem as I simplified it alot, the car brand is irrevelant in my analysis)
I want to calculate probabilities of the price paid given the color of the car
So for each "color" I want to count from my database how many of each "price" was paid.
The only way I can think of doing this right now is writing a script that will call SQL function and do this for me but it looks like a lot of work.
Do you know of any sql tricks I could use to help me with this?
How would I modify my select query to do this?
----------------------
i.e. all data in data base:
red 20000
yellow 20000
red 20000
black 20000
probabilities i want to calculate and retrieve:
red (20000) = 2/4
red (30000) = 0/4
red (40000) = 0/4
black (20000) = 1/4
black (30000) = 0/4
black (40000) = 0/4
yellow (20000) = 1/4
yellow (30000) = 0/4
yellow (40000) = 0/4
mysql> SELECT cr.name, pr.name, cl.name,
-> FROM colour cl, priceround pr, cars cr
-> WHERE cl.id = cr.colour
-> and pr.id = cr. priceround;
+-------------------------
| name | name | name |
+-------------------------
| Honda | red | 20000 |
| BMW | yellow | 20000 |
| Toyota | red | 20000 |
| Ford | white | 20000 |
....
priceround table
+----+-----------------+
| id | name |
+----+-----------------+
| 0 | 20000 |
| 1 | 30000 |
| 2 | 40000 |
colour table
+----+-----------+
| id | name |
+----+-----------+
| 1 | red |
| 2 | black |
| 3 | yellow |
ASKER
GREAT!! that will save me so much code thanks alot!
is there any way I can make do the probability calculation within the select statement
so instead of outputting COUNT(cr.name) I would like COUNT(cr.name)/numberOfRec ords
is there any way I can make do the probability calculation within the select statement
so instead of outputting COUNT(cr.name) I would like COUNT(cr.name)/numberOfRec
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
sorry I meant is there anyway of making it evaluate and actually give me the floating point value of the decimal because what this output gives me i'm then looping through it and inserting it into another table but now i'm trying to insert a string fraction to a floating point field... not going to work...
ASKER
thank you so much you saved me alot of time
SELECT pr.name, cl.name, COUNT(cr.name) as cars
FROM colour cl, priceround pr, cars cr
WHERE cl.id = cr.colour
and pr.id = cr. priceround
GROUP BY cl.name, pr.name