Count on the table MS access with c# - ms-access

I have students records. If I get John repeats more than 2 times on my database, how can I count how many records John has on the database?
________________
Name | Grade
________________
John | F
Mo | A
John | F
I want to know how many F's John gets? I'm connecting this database with C#

Are you looking for this?
SELECT count(Grade),Name
FROM YourTable
GROUP BY Name

You will have to group on both Name and Grade:
SELECT
[Name],
Grade,
Count(*) As Achieved
FROM
YourTable
GROUP BY
[Name],
Grade

Related

Select Distinct value Multiple Columns

This is my simplified table
year | teacher
1 | john
2 | john
2 | sam
3 | john
3 | simon
When I run the query below
SELECT year, teacher FROM table1 GROUP BY year
It gives me the result :
year | teacher
1 | john
2 | john
3 | john
In this case, year column is fine as it shows all distinct value, however teacher column is still repeated. I wish to have distinct values on teacher columns too.
Output I am looking for :
year | teacher
1 | john
2 | sam
3 | simon
This query is not valid SQL (even if MySQL happens to accept it):
SELECT year, teacher
FROM table1
GROUP BY year;
You need an aggregation function around teacher:
SELECT year, MAX(teacher)
FROM table1
GROUP BY year;
That said, this doesn't do what you want. That is hard to do in a single query. Instead, use two queries:
SELECT DISTINCT year FROM table1;
SELECT DISTINCT teacher FROM table1;

MySQL: Select unique value in column based on another columns value

I have a table set up like this:
id | ip | name
---------------------------------
1 | 54.34.32.222 | John
2 | 23.44.64.843 | Rick
3 | 54.34.32.222 | John
4 | 23.44.64.843 | John
5 | 14.432.45.45 | Lisa
6 | 54.34.32.222 | Lisa
7 | 14.432.45.45 | Lisa
I only want to grab a unique IP per name. For example, "54.34.32.222" appears for John twice, so I only want to grab the first row. But "54.34.32.222" also appears for Lisa, so I would like to grab that IP as well.
The result should look something like this:
id | ip | name
---------------------------------
1 | 54.34.32.222 | John
2 | 23.44.64.843 | Rick
4 | 23.44.64.843 | John
5 | 14.432.45.45 | Lisa
6 | 54.34.32.222 | Lisa
How would you count the amount of times names appear? When doing so, it counts how many times the ip appears within the name, but I want the opposite.
SELECT MIN(id), COUNT(name), ip, name FROM yourTable GROUP BY ip, name
You never mentioned how you want to determine which record to retain in the case of duplicate ip-name pairs. However, based on your sample output it appears you are retaining the record with the smallest id value. In this case, we can just take the MIN(id) while grouping to get the desired result:
SELECT MIN(id), ip, name
FROM yourTable
GROUP BY ip, name
Follow the link below for a running demo:
SQLFiddle
This should work for you
SELECT min(id),ip,name FROM youTable group by ip,name
You would likely need to do a join against a derived table here to get what you want. You could also do as subselect, but I will show join solution, as for most use case it would be likely to perform better.
SELECT
yourtable.id AS id,
ip,
name
FROM yourtable
/* join regular table to a derived table
* where you have selected the first row id for each user
*/
INNER JOIN (
SELECT MIN(id)
FROM yourtable
GROUP BY name
) AS min_id_per_name
ON yourtable.id = min_id_per_name.id
ORDER BY yourtable.id
You could use the following query, which selects the lexical minimum of the IP address for any given name:
SELECT NAME, MIN(IP) AS IP
FROM TABLENAME
GROUP BY NAME
If you need the IP address corresponding to the first record found for that name (ie, the one on the record with the lowest ID):
SELECT NAME, IP
FROM TABLENAME TN
WHERE ID = (
SELECT MIN(ID)
FROM TABLENAME TN1
WHERE TN1.IP = TN.IP
AND TN1.NAME = TN.NAME
)

SQL SELECT Query

Suppose I have a SQL table "Company" with three columns: "department_id", "employee", "job". Something like this:
DEPARTAMENT_ID | EMPLOYEE | JOB
--------------------------------------
1 | Mark | President
1 | Robert | Marketing Manager
1 | Rose | Administration Assitant
2 | Anna | Programmer
2 | Michael | Programmer
2 | Celia | Sales Manager
3 | Jhon | Sales Manager
3 | Donna | Programmer
3 | David | Marketing Manager
I would like to write a query that returns the departments id where at least 50% of their jobs are the same.
Result i need in my example would be just:
DEPARTAMENT_ID |
--------------------------------------
2 |
How do I write this SQL query? I think i tried all kind of stuff but i dont get it :(.
This is a bit tricky. You need to compare the total number of people on a job in a department to the total number. So, one method uses two aggregations:
select department_id
from (select department_id, count(*) as numemp
from t
group by department_id
) d join
(select department_id, max(numemp) as numemp
from (select department_id, job, count(*) as numemp
from t
group by department_id, job
) d
group by department_id
) dj
on d.numemp <= 2 * dj.numemp;
You might get duplicates if you have one department that is exactly split between two jobs. In that case, use select distinct.

SQL Using results of query to create another table

(SELECT childinfo.first,childinfo.last,COUNT(clubinfo.club) AS clubs_per_student FROM clubinfo
LEFT JOIN childinfo
ON childinfo.child_id=clubinfo.child_id
GROUP BY concat(studentinfo.first,' ',studentinfo.last)
)
Above is the query that I have written to combine the tables childinfo (containing the columns child_id, first name, and last name) and clubinfo (containing columns child_id and club). The above query would produce a table that is something like this:
first | last | sports_per_child
Sally | Jones | 2
Phil | Jones | 1
Jane | Doe | 1
John | Doe | 1
What I am looking to do is use the results of that query to produce a report of the total number of students who are in a particular number of clubs. So, for example, the table above would produce a report that there are 3 students that are in 1 club and 1 student that is in 2 clubs.
Any ideas on how to write a query that uses my previously written query as a subquery to accomplish this?
You just need to get a count and group by sports_per_child:
SELECT sports_per_child, count(*) FROM
(subquery) AS S
group by sports_per_child

Filter duplicates from mysql result depending on column value

I'm having some problems working out how to return the desired rows from the following mysql table:
first_name last_name collection
==========================================
Bob Jones 1
Ted Jones 1
Bob Jones 1
Bob Jones 2
Ted Baker 2
I want to return the count of names based on columns 'first_name' and 'last_name'. Ordinarily it would just be a simple case of using 'group by first_name, last_name', so we would have the following result: a count of 3 for Bob Jones, 1 for Ted Jones, and 1 for Ted Baker.
However, the difficulty is the third column 'collection'. I need to exclude duplicate names between collections, but not within collections. So we'd include all names in the count for the first collection, but only include names in the count from the second collection if they do NOT occur in the first collection.
So, the desired result would be as follows: a count of 2 for Bob Jones, 1 for Ted Jones, and 1 for Ted Baker.
first_name last_name collection included?
========================================== ===============
Bob Jones 1 Yes
Ted Jones 1 Yes
Bob Jones 1 Yes
Bob Jones 2 No
Ted Baker 2 Yes
I have really tried to get my head around this but I am starting to run out of ideas. Any help would be hugely appreciated... thanks!
Perhaps
SELECT first_name, last_name, COUNT( DISTINCT collection) AS cnt
FROM yourtable
GROUP BY first_name, last_name
The COUNT DISTINCT would eliminate the duplicate Bob/Jones/1 records.
Is this what you are expecting?
SELECT u1.first_name, u1.last_name, u1.collection FROM users u1
WHERE u1.collection = (SELECT min(u2.collection)
FROM users u2 WHERE u1.first_name = u2.first_name
AND u1.last_name = u2.last_name);
http://www.sqlfiddle.com/#!2/bd086/7