How do I count the number of rows that have different dates - mysql

I want to count the total numbers of row in SQL database.
"tableA" :
id | date |
---+------------+
1 | 2019-09-03 |
2 | 2019-09-03 |
3 | 2019-09-04 |
4 | 2019-09-05 |
I want to execute it as new column name "total" that should have like this :
total
-----
3
because they are 3 different dates.
I know that the result must use 2 query like this:
SELECT date
FROM tableA
GROUP BY date AS total;
SELECT COUNT(total)
FROM tableA;
How to combine 2 queries like that or there is another way?

Use count(distinct date)
SELECT COUNT(distinct date) FROM tableA

select count( distinct date) as total from tableA;

Related

Find multiple totals by adding values from mysql table

I need to create a number adding all the values i can find in the db related to a specific customer.
Ex.
| Cust. | Value |
| 1 | 3 |
| 2 | 1 |
| 1 | 1 |
| 2 | 1 |
| 3 | 5 |
The result i want is : Customer #1 = 4, Customer #2 = 2; Customer #3 = 5.
There is a way to do that right into the mysql query?
Try Below query.
Select CONCAT('Customer #' , cust) as customer , sum(Value)
FROM customer_table
Group By cust
You want to SUM the values with a specific GROUP BY clause. Think of the GROUP BY as dividing rows into buckets and the SUM as aggregating the contents of those buckets into something useful.
Something like:
SELECT SUM(Value) FROM table GROUP BY Cust

MySql value and sum(value) in same row without group

I want to get the current row quantity with COUNT(*) and the total row quantity over all rows as column in every row (needed for a report trying to avoid scripting it outside the sql).
I can't use SUM(qty) because i don't want to group my result by reasons and when i use a parameter with := i only get the total qty in the last row.
My current Query looks something like
SET #sumTotal:=0;
SELECT reason, qty, (#sumTotal := #sumTotal + qty) AS total_qty
FROM
(
SELECT reason, COUNT(*) AS qty
FROM someTable
--Imagine a huge amount of joins here
GROUP BY someTableId
)base
The table someTable looks like
----------------------
projectid | reason
----------------------
1 | reason11
1 | reason12
2 | reason21
2 | reason22
2 | reason23
3 | reason31
.
.
.
3 | reason35
----------------------
The result should look something like
----------------------------
reason | qty | totalqty
----------------------------
reason1 | 2 | 10
reason2 | 3 | 10
reason3 | 5 | 10
----------------------------
Am i maybe thinking in the wrong direction and there is a easy way to fix this?
Use auxiliary SELECT query to count the number of someTable rows.
SELECT reason, qty, total_qty
FROM
(
SELECT reason, COUNT(*) AS qty
FROM someTable
GROUP BY someTableId
),
(
SELECT count(*) AS total_qty
FROM someTable
)
This produces a Cartesian product between the derived tables of subqueries. Second subquery will consist of single row with total quantity. Thus, the total quantity will be added to the first subquery.

MySQL - count records and list people

I have table with results:
date | user_id | content
-----------------------------------------
2017-01-14 | 1 | lorem
2017-01-02 | 2 | dsfdf
2017-01-02 | 1 | asfds dsfsda
2017-01-27 | 3 | sdfdfds fsdf
And I want count row for all users and receive result like this:
user_id | count
-----------------------------------------
1 | 2
2 | 1
3 | 1
I try:
select distinct(user_id), count(*) from aso_repairs where date like '2017-01-%'
But this don't work ;-( Any help?
use the GROUP BY clause:
SELECT
user_id,
count(user_id)
FROM aso_repairs
WHERE
date LIKE '2017-01-%'
GROUP BY user_id
You shouldn't use count(*), at least in MySQL 3.23 this is far more expensive than count(somecolumn) although it yields the same result.
Add the missing group by clause:
SELECT
user_id, COUNT(*)
FROM
aso_repairs
WHERE
date LIKE '2017-01-%'
GROUP BY user_id;
Also, the like operator won't let you use any index if there was one on the date column. Consider providing the actual date boundaries:
SELECT
user_id, COUNT(*)
FROM
aso_repairs
WHERE
date BETWEEN '2017-01-01' AND '2017-01-31'
GROUP BY user_id;

MySQL - Getting count of distinct values from 3 tables

I have created 3 tables named complete_shifts,incomplete_shifts and incomplete_shift_register. All three tables include 2 columns called employee_no and date. I want to get count of distinct values in employee_no column from all three tables on a specified date.
Ex: if I specify "date = 2013-06-13" then count shuld be 3. (employee no : 0008,0019,0035)
complete_shifts
shift_id | date | employee_no | sign_in_at | sign_out_at
1 |2013-06-13| 0008 | 7:05 | 16:05
2 |2013-06-13| 0008 | 7:10 | 16:05
3 |2013-06-14| 0025 | 7:11 | 16:10
incomplete_shifts
shift_id | date | employee_no | sign_in_at |sign_out_at
1 | 2013-06-13| 0019 | 7:08 |
incomplete_shift_register
shift_id | date |employee_no | sign_in_at
1 | 2013-06-13| 0008 | 7:08
2 | 2013-06-13| 0035 | 7:09
3 | 2013-06-14| 0060 | 7:11
i don't know how to write the SQL syntax for above situation... plezzz help me..
thank you.
How about something like
SELECT COUNT(DISTINCT employee_no) cnt_employee_no
FROM (
SELECT employee_no
FROM complete_shifts
WHERE date = '2013-06-13'
UNION
SELECT employee_no
FROM incomplete_shifts
WHERE date = '2013-06-13'
UNION
SELECT employee_no
FROM incomplete_shift_register
WHERE date = '2013-06-13'
)
Using
UNION Syntax
UNION is used to combine the result from multiple SELECT statements
into a single result set.
and
COUNT(DISTINCT expr,[expr...])
Returns a count of the number of rows with different non-NULL expr
values.
SELECT count(*) from
(
SELECT 1 from complete_shifts WHERE date="2013-06-13"
UNION ALL
SELECT 1 from incomplete_shifts WHERE date="2013-06-13"
UNION ALL
SELECT 1 from incomplete_shift_register WHERE date="2013-06-13"
)t
Just UNION the tables together and COUNT. The UNION will remove duplicates, so a simple COUNT will do the trick - a COUNT DISTINCT won't be needed. This will work if you want the count only; if you want other values the query gets slightly more involved:
SELECT COUNT(*) FROM (
SELECT employee_no
FROM complete_shifts
WHERE date = '2013-06-13'
UNION SELECT employee_no
FROM incomplete_shifts
WHERE date = '2013-06-13'
UNION SELECT employee_no
FROM incomplete_shift_register
WHERE date = '2013-06-13'
) AllShifts

Mysql multi count in one query

How can I count records for two columns in one table using different query criteria?
Table looks like:
user_id | date | status
------------------------------
1 | 2011-01-02 | 1
2 | 2011-01-03 | 1
3 | 2011-01-02 | 0
4 | 2011-01-03 | 1
1 | 2011-01-02 | 1
I want to count two values in one query. The first one is number of user_id group by status and the second is count of user_id group by date.
How can I do that?
You can't have different GROUP BY clauses in the same query -- each count will have to be in an independent query.
But you can return the output in a single query/resultset using subselects (subquery in the SELECT clause):
SELECT COUNT(a.user_id) AS numUsersPerStatus,
(SELECT COUNT(b.user_id)
FROM YOUR_TABLE b
GROUP BY b.date) AS numUsersPerDate
FROM YOUR_TABLE a
GROUP BY a.status
You don't.
You should use two queries. There's no advantage to doing this with a single query.
If you really want to do it you can try this:
SELECT 'date' AS grptype, date AS grp, COUNT(DISTINCT user_id) AS cnt
FROM yourtable
GROUP BY date
UNION ALL
SELECT 'status' AS grptype, status AS grp, COUNT(DISTINCT user_id) AS cnt
FROM yourtable
GROUP BY status
Result:
grptype grp cnt
date 2011-01-02 2
date 2011-01-03 2
status 0 1
status 1 3
However I would strongly advise against doing this. You want two different and unrelated result sets so you should use two separate queries.