Jump to content

Archived

This topic is now archived and is closed to further replies.

nrsh_ram

probability in php

Recommended Posts

Hi there,

 

I need a help whereby i am wondering if SQL can perform calculation on statistics?

 

i am looking an sql command to genarate all the sample space ..for an example the user key in 123..

so the sql command muct be able to generate the sample space

 

{123,132,213,2}

 

is this can be done in sql?

kindly advice.

Share this post


Link to post
Share on other sites

do you mean.. for 123

 

123

213

221

132

312

321

231

?

 

then no :)

 

BUT before you insert it into a row you can calculate it yourself..

 

or when you retrieve it you can calculate it..

Share this post


Link to post
Share on other sites

then no :)

 

Actually yes... same way you can drive nails into a wall with a screwdriver....

Share this post


Link to post
Share on other sites

Just for fun:

create table #test(
n int
);
insert into #test(n)
values (1);
insert into #test(n)
values (2);
insert into #test(n)
values (3);

select 
distinct convert(char(1),t1.n) + convert(char(1), t2.n) + convert(char(2), t2.n) as 'output'
from #test t1 
cross join #test t2 
cross join #test t3
order by 1 asc	;

Result:

output
------
111 
122 
133 
211 
222 
233 
311 
322 
333 

(9 row(s) affected)

Seemed a lot easier than driving a nail into a wall ;) - but will become a lot more annoying when you need to add extra numbers

Share this post


Link to post
Share on other sites

And what these numbers represent?

Where are 123,132,213 as demanded by OP? :P

 

SELECT
   t1.n,t2.n,t3.n
FROM test AS t1
   CROSS JOIN test AS t2
  ON t1.n != t2.n
   CROSS JOIN test AS t3
  ON t1.n != t3.n AND t2.n != t3.n;

Share this post


Link to post
Share on other sites

Hi there,

 

I need a help whereby i am wondering if SQL can perform calculation on statistics?

 

i am looking an sql command to genarate all the sample space ..for an example the user key in 123..

so the sql command muct be able to generate the sample space

 

{123,132,213,2}

 

is this can be done in sql?

kindly advice.

 

however, I'm sure you can define a mysql function for this. but it is so pointless to have mysql do this in the first place, and insert and drop and create tables for each page view.. I shouldn't have said it was impossible, but thats alot more work then just doing it in php..

Share this post


Link to post
Share on other sites

Those numbers have gone for a walk :P.

 

But yea, just goes to show how silly one little typo can be.

 

create table #test(
n int
);
insert into #test(n)
values (1);
insert into #test(n)
values (2);
insert into #test(n)
values (3);

select 
distinct convert(char(1),t1.n) + convert(char(1), t2.n) + convert(char(1), t3.n) as 'output' -- typo last time was t2.n being used twice... silly me!
from #test t1 
cross join #test t2 
cross join #test t3
order by 1 asc	;

drop table #test;

 

And this now produces the much better output:

output
------
111
112
113
121
122
123 //Here's one!
131
132 //Ah, This one was missing too!
133
211
212
213 //And you!
221
222
223
231
232
233
311
312
313
321
322
323
331
332
333  //Only half evil!

Share this post


Link to post
Share on other sites

Yeah. Now we have solutions for both with- and without- repetition permutations. A recursive function in PHP could be written for both.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.