Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 496
  • Last Modified:

Count all Forward Slashes from MySQL and Count them

Hi there,

I'm building an attendance register. Everything works fine with it. I now have to pull the records from the database and count them all to work out the overall attendance of each student.

All the rows could have any of these characters in them so i would need to fetch each row and count each character seperately:

/
C
H
I
J
L
M
O

Can any body help me? Let me know if ya need any more information

Thanks,
Craig
0
projectstudios
Asked:
projectstudios
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you mean:
select yourfield, count(*)
  from yourtable
 group by yourfield

Open in new window

0
 
Umar Topia.Net Full Stack DeveloperCommented:
Solution provided by Angelll is correct
0
 
max-hbCommented:
You want to calculate the numer of Cs, Hs, Os and so on?!
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
kingjelyCommented:
Hi, I don't agree with the above,

If you need to count each occurance of the letters you mentioned, In a specific field,
<i would need to fetch each row and count each character seperately:

Then try this.

select
Case when Your_Field= '\\' then count(Your_Field) else '' end as 'Backslash',
Case when Your_Field = 'C' then count(Your_Field) else "" end as 'C',
Case when Your_Field = 'H' then count(Your_Field) else "" end as 'H',
Case when Your_Field = 'I' then count(Your_Field) else "" end as 'I',
Case when Your_Field = 'J' then count(Your_Field) else "" end as 'J',
Case when Your_Field = 'L' then count(Your_Field) else "" end as 'L',
Case when Your_Field = 'M' then count(Your_Field) else "" end as 'M',
Case when Your_Field = 'O' then count(Your_Field) else "" end as 'O'
from Your_table
Group by Your_Field;
0
 
projectstudiosAuthor Commented:
Hi guys, Sorry about the long reply. Had to wait till i arrived back to work this morning.

I have tried your replies but i can't seem to get them to work.

Basically my table in my DB is attatched as an image.

As you can see each student will have a new row inserted every week under the that weeks week commencing date and then the row will be updated every day till Friday.

So i now need to pull all the specific rows to each student_id and calculate all the / marks aswell as any others so i can begin the maths to show the overall attendance percentage.

Does anybody know how i can achieve this?
Thanks,
Craig


table.jpg
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
that would be:
select student_id
  , sum(
    case when mon_a = '/' then 1 else 0 end
  + case when mon_p = '/' then 1 else 0 end
  + case when tue_a = '/' then 1 else 0 end
  + case when tue_p = '/' then 1 else 0 end
  + case when wed_a = '/' then 1 else 0 end
  + case when wed_p = '/' then 1 else 0 end
  + case when thu_a = '/' then 1 else 0 end
  + case when thu_p = '/' then 1 else 0 end
  + case when fri_a = '/' then 1 else 0 end
  + case when fri_p = '/' then 1 else 0 end
   ) total_slashes
group by student_id

Open in new window

0
 
projectstudiosAuthor Commented:
Thanks for a quick reply angellll

That looks great but how do i echo the result from that?
0
 
projectstudiosAuthor Commented:
nevermind, Done it :D
One last question for you if you don't mind.

Sometimes there might be a different character in there. Such as, C, H, I, J, L, M, O.

How do i amend that code to count those too?

Thanks
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>How do i amend that code to count those too?
you can add the same expression, and replace the / by the relevant code.

or do you want to count all together? aka if it's not null, count it?
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now