How to do sql loop - mysql

Table works(emNo, comNo, salary).
I can get distinct comNo using "select distinct comNo from works". Suppose it gives me a column with 5 rows. How do I count "emNo`" for each of those rows?

You can use GROUP BY to aggregate per type of comNo.
SELECT
comNo,
count(emNo)
FROM
works
GROUP BY
comNo
This will return one row per distinct value of comNo along with the count of records per group.
Demo: http://www.sqlfiddle.com/#!2/4f5df/1

Related

Different output when I include more columns to the select statement in MySQL

Why do I not get the same results when running the two queries? If I run the second one I get the course with the smallest amount of credits and when I run the first one I get the courses ordered by courseid
select min(credits), title, courseid
from course
group by title, courseid
select min(credits)
from course
An aggregation query is any query that has a group by or an aggregation function in the select.
An aggregation query returns one row per group, where a "group" is defined as the unique combination of values of the keys in the group by clause. If there is no group by clause, then all rows are taken to be a single group and one row is returned.
So, your first query returns one row for each combination of title and courseid in the course table. That row contains the minimum value of credits for that combination. If the course table has only one row per courseid, then the results are very similar to the contents of the table.
The second query returns one row overall, with the minimum number of credits of all rows.
If you want to get one row from with the minimum number of credits, then you don't want an aggregation query. Instead, you can use:
select c.*
from course c
order by c.credits
limit 1;
When you use a group by, you are using a sort of "filter", in the first query you group by title, then all the same titles are grouped by courseid, in the second you only select the minimum value of credits without filtering.
Take a look at a group by doc maybe with some graphical examples like this:
https://www.geeksforgeeks.org/sql-group-by/

MySQL get all records in each group, using group by

Is there anyway to use GROUP BY in queries to grouping all records in each group?
This is my query:
SELECT * FROM `betmatches` WHERE `status`='paid' GROUP BY `cpn`
but it results in getting only the first record in each group. I need all records per each group
If you want all the records, then you will have to add the column names in the GROUP BY clause.
Example:
Assume that betmatches have 5 columns a,b,c,status and cpn.You want the output with 3 columns a,b and cpn,Then add that 3 columns to GROUP BY and change * to the columns you want in the result set.
SELECT a,b,cpn FROM `betmatches` WHERE `status`='paid' GROUP BY `cpn`,a,b

Count same date result as one

I have a MySQL table :
when i count the entries of month January using the query :
SELECT COUNT(*) AS entries FROM daily_call_reports WHERE Month(datetime_in)='01' AND emp_id='E0001'
I got the result 5.
But i want to count same date rows as one.. in the table there are two row of same date 2016-01-21. Now how to count these two row as one..
try this
select count(*)
from
(SELECT distinct id,client_id,emp_id,...., CAST(re.datetime_in AS DATE) AS DATE_PURCHASED
FROM daily_call_reports re
WHERE Month(datetime_in)='01' AND emp_id='E0001')
In a table, a column may contain many duplicate values; and sometimes
you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different)
values.
You have to use DISTINCT for counting the different rows.
SELECT COUNT(DISTINCT DATE_FORMAT(`datetime_in`, '%Y-%m-%d')) as entries FROM daily_call_reports WHERE month(datetime_in)='01' AND emp_id='E0001'
Try this one and let me know.

Return all records in a table with count > 1 for specific field in mysql

I know how to count records in a table based on a distinct value in a field but I am trying now to return all the records in the table that have duplicate values in a given field (without seeing the records that do not) so I can do some analysis of those records. Can I use count this way? I tried
where count (distinct FIELD_A)>1
but it said I was using group incorrectly.
Try this:
SELECT
FIELD_A, count(*) no_of_records
FROM table
GROUP BY FIELD_A
HAVING count(*) > 1;
To restrict / filter the query output after aggregation has been performed, use a HAVING clause.
The WHERE clause is used to restrict / filter the query output BEFORE aggregation is performed (so if you had some rows you did not want to include in the COUNT, you can use the WHERE clause to filter them out).
Also you should not be using DISTINCT here, it will REMOVE all the duplicates before calculating the COUNT.
Use having statement
Select Field_Blah, Count(Distinct Field_A)
From table_a
group by Field_Blah
having Count(Distinct Field_A) > 1

SQL how to count all rows up to a max value

I am having trouble counting the number of rows until it reaches a certain PK.
My PK is called id and I want to count all rows until i reach a specified id
I have tried using this query but it doesn't work probably becuase I am using a MySQL table
select max(count(*)) from news where id=18 group by id
I get this error
Invalid use of group function
select count(*) from news where id<=18
I would use the following:
select count(id) from news where id <= 18
This will be more efficient as you are only returning one column in a row as opposed to all of them.