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`
Related
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;
I'm trying to count the number of sales orders has been canceled in a time period. But I run into the problem that it doesn't return results that are zero
My table
+---------------+------------+------------------+
| metrausername | signupdate | cancellationdate |
+---------------+------------+------------------+
| GLO00026 | 2017-06-22 | 2017-03-20 |
| GLO00055 | 2017-06-22 | 2017-04-18 |
| GLO00022 | 2017-06-27 | NULL |
| GLO00044 | 2017-06-24 | NULL |
| GLO00005 | 2017-06-26 | NULL |
+---------------+------------+------------------+
The statment i'm trying to count with
SELECT metrausername, COUNT(*) AS count FROM salesdata2
WHERE cancellationdate IS NOT NULL
AND signupDate >= '2017-6-21' AND signupDate <= '2017-7-20'
GROUP BY metrausername;
Let me know if any additional information would help
If the metrausername is filtered out by the where, it won't appear. Left join to the aggregation to get round this:
select distinct a1.metrausername, coalesce(a2.counted,0) as counted -- coalesce replaces null with a value
from salesdata2 a1
left join
(
SELECT metrausername, COUNT(*) AS counted
FROM salesdata2
WHERE cancellationdate IS NOT NULL
AND signupDate >= '2017-6-21' AND signupDate <= '2017-7-20'
GROUP BY metrausername
) a2
on a1.metrausername = a2.metrausername
I would just do this by moving the filtering clause to the select. Assuming you really do want the date range (as opposed to having users outside the range), then:
SELECT metrausername, COUNT(cancellationdate ) AS count
FROM salesdata2
WHERE signupDate >= '2017-06-21' AND signupDate <= '2017-07-20'
GROUP BY metrausername;
COUNT(<colname>) counts the non-NULL values, so this seems like the simplest approach.
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;
I have table something like this:
date|status|value
date is date,
status is 1 for pending, 2 to confirmed
and value is value of order
I want to get 3 columns:
date|#status pending|#status pending+confirmed
example of data:
+------------+-----------------+-----------------+
| date | status | value |
+------------+-----------------+-----------------+
| 2015-11-17 | 1 | 89|
| 2015-11-16 | 1 | 6 |
| 2015-11-16 | 2 | 16 |
| 2015-11-16 | 2 | 26 |
| 2015-11-15 | 2 | 26 |
| 2015-11-14 | 2 | 24 |
+------------+-----------------+-----------------+
example of what I want:
+------------+-----------------+-----------------+
| date | confirmed |confirmed+pending|
+------------+-----------------+-----------------+
| 2015-11-17 | 0 | 1 |
| 2015-11-16 | 2 | 3 |
| 2015-11-15 | 1 | 1 |
| 2015-11-14 | 1 | 1 |
+------------+-----------------+-----------------+
I am trying to do:
SELECT array1.DATE
,array1.confirmed
,array2.total
FROM (
SELECT DATE (DATE) AS DATE
,count(value) AS confirmed
FROM Orders
WHERE STATUS = '2'
GROUP BY DATE (DATE) DESC limit 5
) AS array1
INNER JOIN (
SELECT DATE (DATE) AS DATE
,count(value) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5
) AS array2
But I get 4 results per date with repeated confirmed value and different total transactions.
If I try separated, I can get both correct informations:
will list only sum of confirmed orders of last 5 days:
SELECT array1.DATE
,array1.confirmed
,array2.total
FROM (
SELECT DATE (DATE) AS DATE
,count(valor) AS confirmed
FROM Orders
WHERE STATUS = '2'
GROUP BY DATE (DATE) DESC limit 5;
)
will list sum of all orders of last 5 days:
SELECT DATE (DATE) AS DATE
,count(valor) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5
I observed at least one big problem:
Sometimes we will have one day with a lot of not confirmed orders and zero confirmed, so probably inner join will fail.
You can use CASE WHEN, To get the expected output,you have given.
SELECT `date`,
(SUM(CASE WHEN `status`=1 THEN 1 ELSE 0 END)) AS Confirmed,
(SUM(CASE WHEN `status`=1 OR `status`=2 THEN 1 ELSE 0 END)) AS Confirmed_Pending
FROM
table_name
GROUP BY DATE(`date`) DESC
Hope this helps.
You are missing an ON clause in your INNER JOIN. Or, since in your case the column you join on is the same on both sides, you can use USING:
SELECT array1.DATE
,array1.confirmed
,array2.total
FROM (
SELECT DATE (DATE) AS DATE
,count(value) AS confirmed
FROM Orders
WHERE STATUS = '2'
GROUP BY DATE (DATE) DESC limit 5
) AS array1
INNER JOIN (
SELECT DATE (DATE) AS DATE
,count(value) AS total
FROM Orders
GROUP BY DATE (DATE) DESC limit 5
) AS array2
USING (DATE)
An easier approach could be to use a case expression to evaluate whether the status is something you'd like to count, and apply the count function to that:
SELECT DATE (`date`) AS `date`,
COUNT(CASE status WHEN 2 THEN 1 END) AS `confirmed`,
COUNT(CASE WHEN status IN (1, 2) THEN 1 END) AS `pending and confirmed`,
FROM orders
GROUP BY DATE (`date`) DESC
Simple question: Why does the following query not output perday?
SELECT FROM_UNIXTIME(`date`,"%Y-%m-%d") AS `perday`, COUNT(*) AS `count`
FROM `data`
WHERE `group` = 1
GROUP BY `perday`
Count gets outputted correctly, but perday stays empty.
The data table is like:
| id | group | date |
------------------------------------------------
| 1 | 1 | 2013-04-13 06:01:02 |
| 2 | 1 | 2013-04-13 14:24:18 |
| 3 | 2 | 2012-01-21 21:33:03 |
Ect.
Thanks!
EDIT:
Expected output:
| perday |
--------------
| 2013-04-13 |
| 2012-01-21 |
remove WHERE clause,
SELECT FROM_UNIXTIME(date,'%Y-%m-%d') AS perday,
SUM(`group` = 1) AS `count`
FROM data
GROUP BY FROM_UNIXTIME(date,'%Y-%m-%d')
if date is formatted as 2013-04-13 06:01:02, then why use FROM_UNIXTIME? Isn't it DATE_FORMAT instead?
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS perday,
SUM(`group` = 1) AS `count`
FROM data
GROUP BY DATE(date)
ORDER BY date
This will display all available dates in the table.
SQLFiddle Demo
But if you want the selected group only,
SELECT DATE_FORMAT(date, '%Y-%m-%d') AS perday,
COUNT(*) AS `count`
FROM data
WHERE `group` = 1
GROUP BY DATE(date)
ORDER BY date
you may looking for this
SELECT DATE_FORMAT(`date`,"%Y-%m-%d") AS `perday`, COUNT(*) AS `count`
FROM `data`
WHERE `group` = 1
GROUP BY `perday`
DEMO HERE
MySQL DATE() takes the DATE part out from a DATETIME expression.As your Expected output this query is fine.
SELECT
DATE(`date`) AS `perday`,
COUNT(*) AS `count`
FROM `data`
GROUP BY `perday`