Jump to content

Retrieve number of duplicates while using distinct


proggR

Recommended Posts

I have a query that returns only distinct records based on a number of columns in my table that I'm using for reporting. The only catch is that I also need to know the number of times each of these records occured that were ignored by using distinct. The table in question is a statistics table and I'm creating a report to display these statistics. I could leave out distinct and loop through the results checking if the record has been accounted for already and sum up each instance of its occurrence but I'm sure there's a way to do it with a single MySQL query.

The following query gives me each interaction with a specific community by a specific user as well as the type of interaction. The other information I need is the number of times this type of interaction occurred per community entity.

 

SELECT DISTINCT proxy_id, module, action_field, action_value, action FROM statistics WHERE module LIKE 'community:2:%' AND proxy_id = 1;

 

I'm sure the answer is simpler than I am making it out to be but I haven't found any way to do it yet.

 

Thanks in advance for your help.

 

Link to comment
Share on other sites

I figured it out. If I use GROUP BY instead of DISTINCT it gives me the results I want.

SELECT COUNT(*) AS count, proxy_id, module, action_field, action_value, action FROM statistics WHERE module LIKE 'community:2:%' AND proxy_id = 1 GROUP BY proxy_id, module, action_field, action_value, action;

 

Link to comment
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.