Count unrepeated elements on mysql group by - mysql

I have the following table:
| id | metrictimestamp | vehicleid |
+----+-----------------+-----------+
| 1 | 20180201025934 | 33089 |
| 2 | 20180201025955 | 34489 |
| 3 | 20180201025959 | 32040 |
I need to group by date(metrictimestamp) and count how many unrepeated "vehicleid" there is for each day, any sugestions?

You can use DISTINCT in your query:
SELECT COUNT(DISTINCT metrictimestamp) FROM yourTable

First you need to convert your metrictimestamp field into a date type that mysql understands:
STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')
next you need to extract the date portion of that field and give it an alias (date):
DATE(STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')) date
finally you need to group by the resultant date and the vehicleid and filter by repeated records (so only include singletons), so putting it all together:
select DATE(STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')) date, vehicleid from group_test_table group by date, vehicleid having count(vehicleid) = 1;
If I misunderstood your question and you only want the unique vehicleids for any date then:
select distinct DATE(STR_TO_DATE(metrictimestamp, '%Y%m%d%h%i%s')) date, vehicleid from group_test_table group by date, vehicleid;

Related

Group together rows with similar date in SQL

In MySQL, I have a query that simplifies a table. Here is the query and the result:
Query:
SELECT
test.`ASSEMBLING-TAG`.`Name 5` AS tag,
DATE(test.`ASSEMBLING-TAG`.datetimetemp) AS date
FROM
test.`ASSEMBLING-TAG`
GROUP BY `Name 5` , datetimetemp
ORDER BY `Name 5`
Result:
From this result, I would like to group the rows by tags with the same date and count them, like so:
Expected output:
| tag | date | count |
|----------------|------------|-------|
| G_GoodPieces_R | 2016-01-01 | 10 |
| G_GoodPieces_R | 2016-01-02 | 1 |
How can I achieve that ?
Then simply group by the date of the datetime.
And count them.
SELECT
t.`Name 5` AS tag,
DATE(t.datetimetemp) AS date,
COUNT(*) AS Total
FROM test.`ASSEMBLING-TAG` t
GROUP BY t.`Name 5`, DATE(t.datetimetemp)
ORDER BY t.`Name 5`

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;

Query to Find Duplicate entries

I am looking for an SQL query to give me a list of duplicate entries in a table. However, there are 3 different columns to take into account. First is an ID, Second is a Name, and third is a Date. The situation is that there are multiple Names that are assigned with the same ID, and there are multiple records of those in a day, which makes THOUSANDS of different records per day.
I already filtered it so that only results for the past 7 days will show, but the amount of records is still too much for me to extract. I just want to decrease the number of rows in the output order to properly extract the results.
Sample
|--id-|--name--|-------date------|
| 1 | a |5-9-2015, 10:00am|
| 1 | a |5-8-2015, 10:02am|
| 1 | a |5-8-2015, 11:00am|
| 1 | b |5-8-2015, 10:00am|
| 1 | b |5-8-2015, 10:02am|
| 1 | c |5-8-2015, 10:00am|
| 2 | d |5-8-2015, 10:00am|
expected output
|--id-|--name--|
| 1 | a |
| 1 | b |
| 1 | c |
| 2 | d |
Inclusion of entries without any duplicates are fine. The important thing is to only return a single record of a unique id-name combination for a day.
Thanks in advance for any help that you can give.
You can get the combinations as:
select distinct id, name
from sample;
If you want duplicates, using group by and having:
select id, name
from sample
group by id, name
having count(*) > 1;
EDIT:
If you want this by date, then add date(date) to the group by and perhaps select clauses.
To return single id-name data per day you can use this:
select id, name
from tab
group by id, name, date(date)
The DATE() function extracts the date part of a date or date/time expression.
select id,name
from sample
group by id,name,DATE(date)
having count(*)>1;

MAX function in MySQL does not return proper key value

I have a table called tbl_user_sal:
| id | user_id | salary | date |
| 1 | 1 | 1000 | 2014-12-01 |
| 2 | 1 | 2000 | 2014-12-02 |
Now I want to get the id of the maximum date. I used the following query:
SELECT MAX(date) AS from_date, id, user_id, salary
FROM tbl_user_sal
WHERE user_id = 1
But it gave me this output:
| id | user_id | salary | from_date |
| 1 | 1 | 2000 | 2014-12-02 |
Which is correct as far as the max date being 2014-12-02, but the corresponding id is not correct. This happens for other records as well. I used order by to check but that was not successful either. Can anyone shed some light on this?
Note: Its not necessary that max date will have max id, according to my needs. Records can have max date but id may be older.
If you only want to retrieve that information for a single user, which you seem to, because of your WHERE clause, just use ORDER BY and LIMIT:
SELECT *
FROM tbl_user_sal
WHERE user_id = 1
ORDER BY date DESC
LIMIT 1
If you want to do that for every user, however, you will have to get a little bit fancier. Something like that should do it:
SELECT t2.id, user_id, date
--find max date for each user_id
FROM (SELECT user_id, MAX(date) AS date
FROM tbl_user_sal
GROUP BY user_id) AS t1
--join ids for each max date/user_id combo
JOIN tbl_user_sal AS t2
USING (user_id, date)
--limit to 1 id for every user_id
GROUP BY
user_id
You are missing group by clause Try this:
select max(awrd_date) as from_date,awrd_id
from tbl_user_sal
where awrd_user_id = 106
group by awrd_id
What I believe you should do here is have a subquery that pulls the max date, and your outer query looks for the row with that date.
It looks like this:
SELECT *
FROM myTable
WHERE date = (SELECT MAX(date) FROM myTable);
Additional things may need to be added if you want to search for a specific user_id, or get the largest date for each user_id, but this gives your expected results for this example here.
Here is the SQL Fiddle.

How to retrieve corresponding value of a filed with MAX on other fields in MySQL 4.0.x

To start with, I am using MySQL 4.0.27 and need solution for this
version only.
I am using MAX() in SELECT statement with other fields and need to retrieve the value of other fields which is corresponding to the value of MAX field.
Assume below data from table Orders:
--------------------------------------------------------------
Product | CategoryID | Date | OrderBy
--------------------------------------------------------------
TV | 1 | 2011-11-27 | John
Pen | 1 | 2011-11-29 | David
Mouse | 2 | 2011-11-30 | Mike
Printer | 1 | 2011-10-19 | Rozi
HDD | 2 | 2011-11-02 | Peter
----------------------------------------------------------------
My requirement is to retrieve count of orders in each category with name of individuals with recent Order, which means I need following result:
--------------------------------------------------------------------------
CategoryID | OrderBy | Order_Count | Date
-------------------------------------------------------------------------
1 | John | 3 | 2011-11-29
2 | Peter | 2 | 2011-11-30
If I use below SQL:
SELECT CategoryID, OrderBy, COUNT(OrderID) AS Order_count, MAX(Date)
FROM Orders
GROUP BY CategoryID
I am not getting desired result. I am getting some other name in OrderBy instead of the same name which is falling against extracted date.
Can anyone suggest how to achieve this in MySQL 4.0.x where we have limitation of not using inner query or functions like GROUP_CONCAT.
Thanks in advance.
Try:
SELECT CategoryID, OrderBy, COUNT(OrderID) AS Order_count, Date
FROM Orders
GROUP BY CategoryID
ORDER BY Date Desc
- assuming you want the OrderBy value corresponding to the maximum date.
Try:
SELECT a.CategoryID, b.OrderBy, COUNT(DISTINCT a.id), MAX(a.Date)
FROM Orders a
INNER JOIN Orders b ON a.CategoryID = b.CategoryID
GROUP BY a.CategoryID
ORDER BY a.Date DESC,b.Date ASC
I always find it a great help to check in the manual (although, despite what it says there, if the value to find the MAX for is not indexed, then this is more efficient than a sub-select)