Count same date result as one - mysql

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.

Related

How to join 2 table same name rows into a column

Maybe this question has been answered somewhere else but I didn't know how can I apply that query in my working project and I am a beginner(new) to using MySQL.
I have 2 tables in my Phpmyadmin.
table-1 looks like this :
and table-2 looks like this
By using the below query I am able to join both tables together but in different column names.
SELECT DISTINCT `a`.`subject`,`b`.`subject`, `a`.`date`, `a`.`start_time`, `a`.`end_time`, `b`.`date`, `b`.`start_time`, `b`.`end_time`
from `time-table` as a
inner join `oral_time-table` as b on a.oral_token=b.token and a.class='EIGHTH' and a.medium='English' and a.faculty='GENERAL' and a.exam='Half-Yearly'
The below query results in this :
I want both data date name column in ascending order in both the tables. Therefore I need to join both the tables one by one.
With only one common column name both the table rows are joined. How can I achieve this and thanks in advance...
Desired Result :
date column ascending order.
You can achieve the desired result using UNION.
SELECT subject, `date`, start_time,
end_time
FROM TABLE-1
UNION
SELECT subject, `date`, start_time,
end_time
FROM TABLE-2
ORDER BY `date` ASC
Don't use date as any column name as it is a reserved keyword and is
not a good practice.

Query for Access Table results with no duplicate Records counted

I am currently trying to query a table based off two fields, one with a date and the other with records to be counted. The results of the query would list the months as numbers (January = 1, Feb = 2, etc) in one column and the other would list the total number of the individual UNIQUE records. The following query I have;
SELECT DISTINCT Month([Date Received]) AS [Month], Count([1 TeamTracking].[Asset #]) AS [CountOfAsset #]
FROM [1 TeamTracking]
GROUP BY Month([Date Received]);
To test out and make sure I have the right numbers I exported the data to an Excel file and removed duplicates based off of the date and Asset # field. There can be multiple duplicate Asset #'s in the month and there can be the same Asset # in other months. I only want to count the Asset # once per month and not any duplicates in that month. It should be counted again if it is in a different month. For example this is the results I would expect with the query:
1 Team Tracking Table:
Results:
I've tried and just don't get the right numbers. I've tried both in the property field setting the unique values and records to yes and no luck there either. Any assistance would be appreciated. Thank you.
This needs a nested subquery that first consolidates data to unique records. That query then is source for outer query that counts records.
Assuming dates are always first of each month and data is multi-year, consider:
SELECT DateReceived, Count(*) AS Count FROM (
SELECT DISTINCT DateReceived, IDnum FROM Tracking)
GROUP BY DateReceived;
If dates are not always first of month and data is multi-year, consider:
SELECT YearMo, Count(*) AS Count FROM (
SELECT DISTINCT Format(DateReceived, "yyyymm") AS YearMo, IDnum FROM Tracking)
GROUP BY YearMo;
If data is not multi-year:
SELECT Month, Count(*) AS Count FROM (
SELECT DISTINCT Month(DateReceived) AS Month, IDnum FROM Tracking)
GROUP BY Month;
Strongly advise not to use spaces nor punctuation/special characters in naming convention, nor reserved words as names. If you do, must enclose in [ ].

Count how many rows have a specified value when the value changes with each row

When I was querying a table by a specific id I would also count how many rows in that table shared the same name:
SELECT *, COUNT(name) FROM table WHERE id = 24601
Now I'm no longer querying a table by a specific id, instead getting all of the table's output. Is there a way I could still get that count for each row?
SELECT *, COUNT(this specific row's name) FROM table
If you want to get the count of rows per name, then use GROUP BY:
SELECT name, COUNT(*) AS count
FROM table
GROUP BY name
You'll get one row per distinct value of name, with a second column that has the integer count.
If you also need the other columns for each row, you should write that as a separate query. Don't combine aggregated results with non-aggregated results in the same query.

Group by only returns one row

I am using the following sql in mySQL.
I don't understand why I get only one row as output.
select category, count(*) as counts
from table_1
where date_add between '2016-07-01' and '2016-07-31'
group by category
How many categories do you have in that date range would be the first thing you'd have to look for since you're grouping by categories. If you have 'n' number of categories within the specified date period, ideally you result set should have 'n' rows.
There is nothing wrong with your query per se. I tried the query with two categories and I do get the expected result set.
If you want all the categories in the result set irrespective of whether they are in the result set or not, then you can something of this sort :
select category, count(*) as counts
from tab
where date_add between '2016-07-01' and '2016-07-31'
group by category
union
select category, 0 as counts
from tab
where date_add not between '2016-07-01' and '2016-07-31'
group by category
Mind you this is a simple way of doing this but certainly not the best
This will show all the categories irrespective of the date peroids. You can see this here -> http://sqlfiddle.com/#!9/a3a9b/2
See this here -> http://sqlfiddle.com/#!9/bdb979/1
If you modify the data set in the fiddle to have three categories, then again, the result set returns the expected 3 rows.
See this here -> http://sqlfiddle.com/#!9/ec7a8/1
Again, its important to note that the categories have to be present within that date period for them to show up in the result set.
Hope this helps!!!
Just do some changes and try :
select category, count(*) as counts
from table_1
group by category
Use above code and see if it return more than one row , it means that you query is correct but only one row is matching below criteria ie:
where date_add between '2016-07-01' and '2016-07-31'
group by category
So Query returning one row, when your database table will have more data match this criteria htne it will return more row. thanks.
You are clearly only getting one row because only one category matches the where condition.
If you want all categories with the count of adds in that range, then use conditional aggregation:
select category,
sum(date_add between '2016-07-01' and '2016-07-31') as counts
from table_1
group by category;

How to do sql loop

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