Trying to get the count of items per day with SQL query - mysql

I have a db with several days of data and I want to get the count of each record with Status=0, Status=1...Status=8 for each day. I was to see something like.
DateAndTime State_0 State_1 State_2
2014-08-15 5 8 9
2014-08-16 2 5 6
2014-08-17 4 2 3
I was trying this:
SELECT DISTINCT DATEADD(dd,(DATEDIFF(dd,0,DateAndTime)),0) AS DateAndTime,
SUM( Case When State=0 Then 1 Else 0 End) AS State_0,
SUM( Case When State=1 Then 1 Else 0 End) AS State_1,
SUM( Case When State=2 Then 1 Else 0 End) AS State_2
FROM [DB_002].[dbo].[MyDb]
Group By DateAndTime
Order by DateAndTime
But it keeps adding rows for each state that I add. That is with 3 states I'm getting 4 rows for each date. Any suggestions on how to do this would be greatly appreciated.

You are grouping on the DateAndTime field, which contains also the time component. That makes each record practically unique, so there will be a single record in each group.
Group on the date only:
Group By DATEADD(dd,(DATEDIFF(dd,0,DateAndTime)),0)
Order by DATEADD(dd,(DATEDIFF(dd,0,DateAndTime)),0)

You are confused because you have changed a column and given it the same name as the original column. When you say group by DateAndTime, that is referring to the column in the table. Assuming this is SQL Server, I would write the query as something like this:
SELECT cast(d.DateAndTime as Date) as DateAndNoTime,
SUM(Case When d.State = 0 Then 1 Else 0 End) AS State_0,
SUM(Case When d.State = 1 Then 1 Else 0 End) AS State_1,
SUM(Case When d.State = 2 Then 1 Else 0 End) AS State_2
FROM [DB_002].[dbo].[MyDb] d
Group By cast(d.DateAndTime as Date)
Order by DateAndNoTime;
This changes the name of the alias in the select to avoid confusion. It uses the alias for column references, to also clarify the meaning of the query. And, it uses cast() instead of the old datedd/datediff trick.

Related

Count within a SELECT Query [duplicate]

in my mysql database i have instagram_actions_histories table with two important column as action_type and action_name, now i want to use count and case statement to get count of ids column, for example:
count of all ids when action_type equal 1 and action_name equal 'like'
select `account_id`,
count(case when action_type = 1 and action_name='like' then id else 0 END) as `like`,
count(case when action_type = 1 and action_name='superLike' then id else 0 END) as `superLike`,
count(case when action_type = 2 then id else 0 END) as `follow`,
from `instagram_actions_histories`
where `instagram_actions_histories`.`account_id` in (1)
group by `account_id`
unfortunately i get wrong result as all of outputs are same, for example:
account_id like superLike follow
1 1282 1282 1282
correct result was should be:
account_id like superLike follow
1 1282 20 10
You should count 1 for a match, and count NULL when there is no match:
SELECT
account_id,
COUNT(CASE WHEN action_type = 1 AND action_name = 'like' THEN 1 END) AS `like`,
COUNT(CASE WHEN action_type = 1 AND action_name = 'superLike' THEN 1 END) AS superLike,
COUNT(CASE WHEN action_type = 2 THEN 1 END) AS follow
FROM instagram_actions_histories
WHERE account_id IN (1)
GROUP BY account_id;
The problem with the current logic of your CASE expressions is that COUNT will count any non null value as one count. So zero also would be counted.
Note that your current logic would have worked using SUM to take the conditional aggregations, e.g.
SUM(CASE WHEN action_type = 1 AND action_name = 'like'
THEN 1 ELSE 0 END) AS `like`
In this case, to turn off the aggregation for non matching records, we in fact can use zero, because summing zero does not affect the sum.

want to get all dates but when there is one or more TDBUY = 1 then this date#TDBUY=0 not

I have a table with a date column and a column named TDBUY (which can be 0 or 1). Now I do following:
SELECT tradedate,aktienstat.TDBuyPerfection,count(*) as cc from
aktienstat group by TradeDate,TDBuyPerfection HAVING cc >= '0' ORDER
BY TradeDate desc limit 100;
And get:
I don`t want to display f.e. the 2018-02-08 or 2018-02-07 with 0 count (line 1 and line 3) because there are 1 count with 1 each. But if no TDBUY then the date should be displayed with 0 count.
Can anyone here tell me please how to do it?
THANKS
Edit: It works also fine with SUM instead of Count.
SELECT tradedate,aktienstat.TDBuyPerfection,sum(aktienstat.TDBuyPerfection) as summe from
aktienstat group by TradeDate ORDER
BY TradeDate desc limit 100;´
We can try doing this via a pivot query:
SELECT
tradedate,
CASE WHEN cnt_1 = 0 THEN 0 ELSE 1 END AS TDBuyPerfection,
CASE WHEN cnt_1 = 0 THEN cnt_0 ELSE cnt_1 END AS cnt
FROM
(
SELECT
tradedate,
COUNT(CASE WHEN TDBuyPerfection = 0 THEN 1 END) AS cnt_0,
COUNT(CASE WHEN TDBuyPerfection = 1 THEN 1 END) AS cnt_1
FROM aktienstat
GROUP BY tradedate
) t;
The pivot trick works here because it brings the zero and one counts into a single record. There, it is relatively easy to test both counts. In your current form, it is much harder to check the counts.
The demo below shows that zero buy dates appear with only the zero count, while dates having a buy count show only the buy data.
Demo

How to use multiple COUNT in SQL

In my table I have task, every task have state.
1 - planned
2 - executing
3 - finished
I want get count of all planned, executing and finished task.
I can write three queries like this:
SELECT COUNT(*) FROM `task` WHERE state = 1
SELECT COUNT(*) FROM `task` WHERE state = 2
SELECT COUNT(*) FROM `task` WHERE state = 3
So, my question is: It is possible (and how?) get this data in one query?
Thank you for any help.
another approach is to use group by, like this:
select state, count(*)
from task
group by state
you use SUM
select state,
sum(case when state = 1 then 1 else 0 end) state1count,
sum(case when state = 2 then 1 else 0 end) state2count,
sum(case when state = 3 then 1 else 0 end) state3count
from task

SQL query to count total ocurrences of values in one column and relative occurrence in another column

This is my first post, so any general corrections to format/content are also welcome. I'm relatively new to SQL.
Say I have a database which collects test results from an classification evaluation. I know what the expected outcome is for each test. I also have a column indicating whether the test was successful, ie the expected value returned matched the expected value. It looks something like this:
Expected_Result Result Success
A A True
A B False
B B True
A A True
B A False
I know I can return the total occurrences of each expected type withSELECT Expected_Result, COUNT(Expected_Result) FROM Evaluation_Results GROUP BY Expected_Result.
I know how to count the number of false detections for a specific expected outcome with SELECT COUNT(*) FROM Evaluation_Results WHERE Success = 'True' AND Expected_Result = 'A'
Where I'm struggling is combining the two. I would like the query to return a list of all distinct expected results, the total of each, the count of successful results, and the percentage of the total, like so:
Expected_Result Total Num_Successful Success_Rate
A 3 2 66.67
B 2 1 50.00
You could use a CASE expression to perform a condition check during aggregation. A case statement identifies a conditional outcome. For instance you could use:
select evaluation_result
, count(*) AS total
, sum(case when success='true' and result='a' then 1 else 0 end) AS num_successful
, sum(case when success='true' and result='a' then 1 else 0 end)/count(*) AS success_rate
from evaluation_results group by evaluation_result;
Basically what's happening there is you're taking a count(*) of all grades, a sum() of a 1 or 0 based on a conditional outcome, then performing the ratio math. There's no need for a join here. The CASE Expression is a powerful conditional statement which can be used in so many diverse ways.
Or for a more flexible solution have a look at this:
select evaluation_result
, count(*) AS total
, sum(case when success='true' and result=evaluation_result then 1 else 0 end) AS num_successful
, sum(case when success='true' and result=evaluation_result then 1 else 0 end)/count(*) AS success_rate
from evaluation_results group by evaluation_result;
You can use self join if table is same like.
SELECT distinct e.Expected_Result, COUNT(Expected_Result), sum(e1.columns name), avg(e1.column name)
FROM Evaluation_Results e
left join Evaluation_Results e1 on e1.col=e.col
GROUP BY e.Expected_Result
Use this simple Query and check for the result..
select Expected_Result, count(Expected_Result) Total,
sum(IF ('True' = Success, 1, 0) ) Num_Successful,
avg(IF ('True' = Success, 1, 0 )) Success_Rate
from Evaluation_Results group by Expected_Result

Mysql query with joins

I am not able to make a query to get some crossed values. My knowledge of mysql is not so good to make this kind of things.
I have two tables, signedup and signedupLocal. Both with same fields: name, surname1, surname2, NIF, year,city, status and dateSigned.
I want to get all values from both tables together when the status is 0 in both ordered by surname1 and surname2 (A to Z). ALso I want to have a new field called for example "app" in which it says yes or no depending on which table the data came. If signedpupLocal, yes else no.
After that, I want to do the same thing when status is 1 ordered by dateSigned (older first).
I haven't tried to go so far to test a complete query because I have problems with just part of it.
I already tried something like:
SELECT signedupLocal.name,
signedupLocal.surname1,
signedupLocal.surname2,
signedupLocal.NIF,
signedupLocal.year,
signedupLocal.city,
signedup.name,
signedup.surname1,
signedup.surname2,
signedup.NIF,
signedup.year,
signedup.city
FROM signedup, signedupLocal
WHERE signedup.id_Event = 78
AND signedupLocal.id_Event = 78
AND signedupLocal.status = 0
AND signedup.status = 0
But it fails.
SELECT name, surname1, surname2, NIF, year, city, status, 'no' as app,
case when status = 1 then datesigned else '2099-12-31' end as datesigned_sort,
case when status = 0 then surname1 else '' end as surname1_sort,
case when status = 0 then surname2 else '' end as surname2_sort
FROM signedup
WHERE id_Event = 78 AND status in(0,1)
UNION ALL
SELECT name, surname1, surname2, NIF, year, city, status, 'yes' as app,
case when status = 1 then datesigned else '2099-12-31' end as datesigned_sort,
case when status = 0 then surname1 else '' end as surname1_sort,
case when status = 0 then surname2 else '' end as surname2_sort
FROM signedupLocal
WHERE id_Event = 78 AND status in(0,1)
ORDER BY 7 asc, 9 desc, 10 asc, 11 asc
Note that when using UNION, you need to use column numbers, not column names in the ORDER BY. And I used UNION ALL as it is more efficient than UNION, as the latter remove duplicates - which causes effort for the database engine. So if you know there are none, you can avoid this.
For the sorting, I use a constant for the cases where the record should not be sorted by a sort column.
Use a UNION of 2 queries to concatenate the results.
SELECT * FROM (
SELECT *, "signedup" AS tablename FROM signedup WHERE …
UNION
SELECT *, "signedupLocal" AS tablename FROM signedupLocal WHERE …
) U
ORDER BY U.dateSigned DESC