SQL query to show multiple counts over the last day - mysql

How do i formulate a sql query where i can display the number of counts of success and errors and warning for the tables for latest day . So I can create a visualization in dashboard. The ultimate goal is for a user to know on a current day how many healthy pipelines are there and how many unhealthy pipelines are there.
i tried something like this, but i complicated it.
SELECT status, COUNT(*) as Total, cast(cast(time as Timestamp) as date) as time,
SUM(CASE WHEN Status = 'Success' THEN 1 ELSE 0 END) as Success,
SUM(CASE WHEN Status = 'Warning' THEN 1 ELSE 0 END) as Warning,
SUM(CASE WHEN Status = 'Error' THEN 1 ELSE 0 END) as Error
FROM table-name
where (cast(cast(time as Timestamp) as date) in ({{ Time }}) or {{ Time }} = 'All')
GROUP BY 1, cast(cast(time as Timestamp) as date)
order by cast(cast(time as Timestamp) as date) desc
lets say this is the given table

How do i formulate a sql query where i can display the number of counts of success and errors and warning for the tables for latest day .
You can use something like this:
SELECT DATE(time) as date, COUNT(*) as Total, ,
SUM(Status = 'Success') as Success,
SUM(Status = 'Warning') as Warning,
SUM(Status = 'Error') as Error
FROM table-name
WHERE date(time) = (SELECT MAX(date(time)) FROM table-name)
GROUP BY 1;

Related

Sum on same column with two different conditions mysql

I have a table named Order with schema as
user_id, state amount
11 success 100
11 FAILED 10
11 FAILED 10
11 success 17
state can have two values (Success/Failed).
I want to fetch sum(amount) when state = "SUCCESS" - sum(amount) when state = "FAILED"
means difference total amount when success - total amount when failed.
I can solve this problem in 2 queries.
A = select id, sum(amount) when state = "SUCCESS"
B = select id, sum(amount) when state = "FAILED"
And solution will be A-B.
Is there any way I can achieve this in single sql query?
use case when
select user_id,sum(case when state = 'SUCCESS' then amount else 0 end)-sum(case when state = 'FAILED' then amount else 0 end)
from table group by user_id
Use conditional aggregation:
select id,
sum(case when state = 'SUCCESS' then amount else - amount end) as total
from t
where state in ('SUCCESS', 'FAILED')
group by id;
I assume that you want this sum per id and not overall in the table.
select sum(case when state = "SUCCESS" then amount else o end) -
sum(case when state = "FAILED" then amount else o end)
from tbl
group by userid

Grouping by months and by column with diferent values

I am having problems trying to create a query that allow me to group by months and by a column that has different values.
The following is a small representation of the table and columns I need to query.
Table name is requests and has two columns date and status. Status may have the values pending, attended, absent and canceled.
I want to get a query that looks like this
Right now i am trying a subquery on status for each possible value. It works but is a very slow query. It takes arround 48s for 8000 rows.
SELECT
MONTHNAME(date)
(SELECT count(status) FROM requests WHERE status = "pending"),
(SELECT count(status) FROM requests WHERE status = "attended"),
(SELECT count(status) FROM requests WHERE status = "absent"),
(SELECT count(status) FROM requests WHERE status = "canceled")
FROM request
GROUP BY 1;
Any recommendations on how to get the result efficiently? Thank you very much
You could use case whene on status instead of several subselect
select
MONTHNAME(date)
, sum( case when status = "pending" then 1 else 0 end) pending
, sum( case when status = "attended" then 1 else 0 end) attended
, sum( case when status = "absent" then 1 else 0 end) absent
, sum( case when status = "canceled" then 1 else 0 end) canceled
FROM request
GROUP BY MONTHNAME(date) ;

sql group by date gives same value everyday

So I want to group by days for statistics so I have data on each day (type 1 is bought and 0 is sold) but the query gives me the same result everyday and that is not correct can someone help me with this code?
SELECT
DATE(from_unixtime(credit_transaction_time)) AS data_date,
total_spend AS credits_spend,
total_bought AS credits_bought
FROM credit_transactions
JOIN (SELECT SUM(`credit_transaction_amount`) AS total_spend FROM credit_transactions WHERE `credit_transaction_type` = 0 GROUP BY DATE(from_unixtime(credit_transaction_time))) AS spend
JOIN (SELECT SUM(`credit_transaction_amount`) AS total_bought FROM credit_transactions WHERE `credit_transaction_type` = 1 GROUP BY DATE(from_unixtime(credit_transaction_time))) AS bought
GROUP BY DATE(from_unixtime(credit_transaction_time))
Use conditional aggregation:
SELECT DATE(from_unixtime(credit_transaction_time)) AS data_date,
SUM(CASE WHEN credit_transaction_type = 0 THEN credit_transaction_amount ELSE 0 END) as credits_spend,
SUM(CASE WHEN credit_transaction_type = 1 THEN credit_transaction_amount ELSE 0 END) as credits_bought
FROM credit_transactions
GROUP BY DATE(from_unixtime(credit_transaction_time));
Your query doesn't work because you don't have an ON condition. In most databases, this would result in a syntax error, but MySQL allows this syntax.

Transform Statement from Access to DB2

I have the following query that I am trying to understand and convert
it to a db2 format:
TRANSFORM Sum(Cases) AS SumOfCases
SELECT Process, Sum(Cases) AS total
FROM tbl
GROUP BY Process
PIVOT tbl.STATUS;
Table has data like:
Process Status Cases
a Cancelled 14
a Closed 179
b Cancelled 20
b Closed 30
b Pending 10
How can I write that query to db2?
I tried the following query:
SELECT Process
, MAX(CASE WHEN STATUS = 'Cancelled' THEN CASES END) "Cancelled"
, MAX(CASE WHEN STATUS = 'Closed' THEN CASES END) "Closed"
, MAX(CASE WHEN STATUS = 'Pending' THEN CASES END) "Pending"
FROM tbl
GROUP BY Process;
Since I do not have MS Access hence I am not confident that if I had done the right thing in db2 or not.
Would appreciate if I could get some advice on this.
Your DB2 query works correctly in replicating Access's crosstab query except you missed the Total column. By the way any aggregate function would work for your CASE/WHEN statements: MIN(), MAX(), MEDIAN(), AVG(), even SUM():
SELECT Process
, SUM(CASES) AS "Total"
, MAX(CASE WHEN STATUS = 'Cancelled' THEN CASES END) AS "Cancelled"
, MAX(CASE WHEN STATUS = 'Closed' THEN CASES END) AS "Closed"
, MAX(CASE WHEN STATUS = 'Pending' THEN CASES END) AS "Pending"
FROM tbl
GROUP BY Process;

MySql count in group_concat

I'm trying to create a query that would return a table as followed:
dayName, #ofStatus1,#ofStatus2,#ofStatus3
dayName, #ofStatus1,#ofStatus2,#ofStatus3
dayName, #ofStatus1,#ofStatus2,#ofStatus3
dayName, #ofStatus1,#ofStatus2,#ofStatus3
dayName, #ofStatus1,#ofStatus2,#ofStatus3
dayName, #ofStatus1,#ofStatus2,#ofStatus3
dayName, #ofStatus1,#ofStatus2,#ofStatus3
Basically I have a table where users get to enter cases. Each case has the following fields(datedCreated, status).
There are three (3) status possible (new, progress, closed)
So I would like to retrieve the number of each status for each day.
Each line would look something like this:
Monday, 8,3,2
Tuesday, 8,3,2
Wednesday, 8,3,2
...
I have something that looks like this but can't seem to get it to work.
SELECT DAYNAME(dateAdded)AS Date, COUNT(status) AS Count, GROUP_CONCAT(CONCAT(status, ',', "54") SEPARATOR ',' ) AS Text FROM clients GROUP BY DATE(dateAdded) ORDER BY dateAdded LIMIT 7
I know right now I have three colomns, but I need it to be in one columns.
Thank your for the help in advance.
Try:
SELECT concat(
DAYNAME(dateAdded),', '
sum(case when status = 'new' then 1 else 0 end),','
sum(case when status = 'progress' then 1 else 0 end),','
sum(case when status = 'closed' then 1 else 0 end)) as single_col
FROM clients
GROUP BY DATE(dateAdded)
ORDER BY dateAdded
Why not just group concat the results?
SELECT
GROUP_CONCAT(t_date, t_new, t_progress, t_closed) AS 'Status Per Day'
FROM
( SELECT
DAYNAME(dateAdded)AS t_date,
SUM(status = 'new') t_new,
SUM(status = 'progress') t_progress,
SUM(status = 'closed') t_closed
FROM clients
GROUP BY DATE(dateAdded)
ORDER BY dateAdded LIMIT 7
) t
GROUP BY t_date