Jump to content

Recommended Posts

This question may sound like a MySQL question, but I think it relates more to PHP...

Currently I have the following (pseudo-code) query in my PHP script...

	SELECT id, col-a, col-b, col-c,
       usefulCount, usefulResponses, (usefulCount/usefulResposnes) as usefulPct
	       newMedianCalculationHere
	FROM(
     SELECT DISTINCT col-a, col-b, col-c,
	         (SELECT COUNT(xtc.id)
          FROM table-7
          INNER JOIN table-8
          ON
          WHERE something
          AND respone_yn = 1) AS usefulCount,
	         (SELECT COUNT(xtc.id)
          FROM table-7
          INNER JOIN table-8
          ON something
          WHERE something) AS usefulResponses
	          (Add Median calculation here) AS newMedianCalculationHere
	     FROM 
     INNER JOIN
     WHERE
    ) AS wrapper
	ORDER BY whatever
	

 

Now I need to calculate the median of some data and drop that in my inner query.

Here is some sample code that I found online...

https://www.eversql.com/how-to-calculate-median-value-in-mysql-using-a-simple-sql-query/

	SET @rowindex := -1;
	SELECT
	   AVG(g.grade)
	FROM
	   (SELECT @rowindex:=@rowindex + 1 AS rowindex,
	           grades.grade AS grade
	    FROM grades
	    ORDER BY grades.grade) AS g
	WHERE
	g.rowindex IN (FLOOR(@rowindex / 2) , CEIL(@rowindex / 2));
	

 

Because my query is in a PHP script, I'm now sure how to incorporate these lines...

	SET @rowindex := -1;
	 
	(SELECT @rowindex:=@rowindex + 1 AS rowindex,
	

 

Oh, if it matters, I use prepared statements in my PHP...

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/312132-calculate-median-in-query-in-my-php/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.