Jump to content

simple to some, kinda hard for me.. sql counting


Recommended Posts

I have a table.. explain this as quick as I can..

 

 

ok ..  you all know what this does correct?

 

select playerref, count(playerref) as gamesplayed FROM contest group by playerref 

say I have 51 rows.. Joe is 50 of them.

and 1 row is  pete..

 

the above will return two rows as the result..
one  joe 50

one  pete 1

 

what im trying to do is keep all 51 rows..

but at the ends is a new column in all joes 50 rows, in this new column it shows 50

and the last row  it shows one row pete 1

Basically I want the count in a new column at the end of every row.

 

possible? thanks anyone who can help.

 

 

 I understand what your asking. My question is, why do you want to do this? What is the overall task you are trying to accomplish? And I don't mean how you are trying to accomplish it. I sense an XY Problem. See my signature for explanation.

honestly... Im using excel and i5 laptop, and its all a P.O.S.  I have 400,000 rows and i simply want all the names to have the number of games played at the end of every row... I started it at 8:30am.. its now 3:06pm...  so I was messing around and threw the list into a database and its a hell of alot faster..

So my plan let sql do the calcs quicker.. export back to excel.. voilia. sorting simpler.

 

but ah! since you asked me this.. maybe sorting the excel sheet by names then doing the calcs is faster.. thx.

as for the question above im still looking for an answer.

Edited by 0o0o0

Join the original table with the result set of your current query.

SELECT contest.playerref, contest_stats.games_played
FROM contest
JOIN (
  SELECT playerref, COUNT(*) AS games_played
  FROM contest
  GROUP BY playerref
) AS contest_stats USING (playerref)
;

Proper database systems like PostgreSQL have window functions for that purpose, but MySQL doesn't and probably never will.

  • Like 1
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.