Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Count all Forward Slashes from MySQL and Count them

Posted on 2010-09-23
9
Medium Priority
?
492 Views
Last Modified: 2012-06-21
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
Comment
Question by:projectstudios
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33744309
you mean:
select yourfield, count(*)
  from yourtable
 group by yourfield

Open in new window

0
 
LVL 10

Expert Comment

by:Umar Topia
ID: 33744339
Solution provided by Angelll is correct
0
 
LVL 4

Expert Comment

by:max-hb
ID: 33744698
You want to calculate the numer of Cs, Hs, Os and so on?!
0
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 8

Expert Comment

by:kingjely
ID: 33744767
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
 

Author Comment

by:projectstudios
ID: 33752132
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 33752165
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
 

Author Comment

by:projectstudios
ID: 33752200
Thanks for a quick reply angellll

That looks great but how do i echo the result from that?
0
 

Author Comment

by:projectstudios
ID: 33752212
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
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33752749
>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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question