Jump to content

Recommended Posts

Hello,

 

How can I construct this query?

 

I have a bunch of values in a table. I want to output the % of values that only occur once to the % of values that occur more than once. In other words, if I have 1, 2, 2, 2, 3, 4, 4, I would like to output:

 

New numbers: 50%

Existing numbers: 50%

 

..new numbers because the 1 and 3 only occurred once and existing numbers of 2 and 4 because they occurred more than once. %s are %s of individual numbers.

 

What is the query I should use?

 

Ryan

Link to comment
https://forums.phpfreaks.com/topic/126501-calculating-new-vs-old/
Share on other sites

try

SELECT A.newold, COUNT(*)*100/B.totnums as pcent
FROM
(SELECT val,
   CASE COUNT(*) WHEN 1 THEN 'new' ELSE 'old' END as newold
   FROM mytable
   GROUP BY val) as A
JOIN 
(SELECT COUNT(DISTINCT val) as totnums FROM mytable) as B
GROUP BY A.newold

 

with my data

[pre]

+------+

| val  |

+------+

|    2 |

|    3 |

|    5 |

|    4 |

|    2 |

|    5 |

+------+

 

-->

 

'new', 50.0000

'old',  50.0000

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.