How to change the mysql query to retrieve data (pivot view) - mysql

I have a dataset as follows,
I need to get the average xdr_count of each table_name for 7 past days..So I tried as below.
SELECT
t1.table_date,
t1.table_name,
t1.xdr_count,
t2.AVG_XDR,
t2.Threshold
FROM stat t1
INNER JOIN
(
SELECT table_name, AVG(xdr_count) AS AVG_XDR,AVG(xdr_count)*.8 as Threshold
FROM stat
where table_date >= DATE(NOW() - INTERVAL 7 DAY)
GROUP BY table_name
) t2
ON t2.table_name = t1.table_name
where table_date >= DATE(NOW() - INTERVAL 7 DAY);
This works fine and I could get the output as below.
But I need to rearrange this table to a pivot view(hope this can called as pivot view) such as below.
I need to mention the table name,Avg(XDR) and dates with appropraite xdr_count values.
Can someone help me to change the view of output result..
Note: Threshold value will be use later.

I'm not entirely sure how you want to calculate the avg_xdr columns. But, you can try something like this.
SELECT table_name,
MAX(CASE WHEN table_date = '2022-08-21' then avg_xdr else 0 END) AS avg_xdr,
MAX(CASE WHEN table_date = '2022-08-21' then xdr_count else 0 END) AS '2022-08-21',
MAX(CASE WHEN table_date = '2022-08-22' then xdr_count else 0 END) AS '2022-08-22',
MAX(CASE WHEN table_date = '2022-08-23' then xdr_count else 0 END) AS '2022-08-23',
MAX(CASE WHEN table_date = '2022-08-24' then xdr_count else 0 END) AS '2022-08-24'
FROM tableA
Group by table_name
db fiddle

Related

How to generate the following statistic from a table?

I have a table named incident_summary that structure and data as following:
month,system_id,s_count
202104,1,50
202104,2,6
202105,1,14
202105,2,4
202106,1,1
202106,2,1
I would like to generate the following statistic:
s_count_on_202106,s_count_before_202106
2,74
where
s_count_on_202106 is sum of s_count value on 202106
s_count_before_202106 is sum of s_count value before 202106
I have tried the following SQL:
select
sum(case when month<202106 then s_count else 0 end)
sum(case when month=202106 then s_count else 0 end)
from incident_summary
group by month
However, it does not work, would you help to me to solve the problem?
Try the following Query.
May be it helps you.
SELECT
t1.s_count_on_202106,
t2.s_count_before_202106
FROM
(
SELECT
sum(s_count) AS s_count_on_202106
FROM
incident_summary
WHERE
month = 202106
) AS t1,
(
SELECT
sum(s_count) AS s_count_before_202106
FROM
incident_summary
WHERE
month < 202106
) AS t2
Sum again on your result set.
SELECT SUM(s_count_before_202106)s_count_before_202106, SUM(s_count_on_202106)s_count_on_202106
FROM
(
select
sum(case when month<202106 then s_count else 0 end)s_count_before_202106 ,
sum(case when month=202106 then s_count else 0 end)s_count_on_202106
from incident_summary
group by month
)T;

MySQL : using sum in( case when ) statement shows 0 as result

new to MySQL..so pls help me out with this basic code..
i have a query something like this...
select weekofyear(id_time),
(id),
#Tat1:=exp1,
#Tat2:=exp2,
#check1:=exp3,
#check2:=exp4,
(case when #check2=0 then
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+10))) then 1 else 0 end)
else
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+20))) then 1 else 0 end)
end) as BO
from datb
where cid=18
and id_time between '2019-11-01 06:00:00' and '2019-11-25 06:00:00'
and it gives correct results as--here
however i want to use sum after case when statement so that I can get total values where BO=1 and group by week of year , so i made following changes-
select weekofyear(id_time),
count(id),
#Tat1:=exp1,
#Tat2:=exp2,
#check1:=exp3,
#check2:=exp4,
sum(case when #check2=0 then
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+10))) then 1 else 0 end)
else
(case when (#Tat1>(#Tat2+30) or (#check1=1 and (#Tat1>#Tat2+20))) then 1 else 0 end)
end) as BO
from datb
where cid=18
and id_time between '2019-11-01 06:00:00' and '2019-11-25 06:00:00'
group by weekofyear(id_time)
but it always returns 0 as output.
Output --here 2
Please help , I don't know what am I doing wrong here.
Thanx !
As others have already said, session variables can be unpredictable (especially when aggregation gets mixed in). That said, it doesn't look like you're using the session variables to carry over values from one row to the next (as is often done), but to just make aliases of sorts for calculations you don't want to repeat.
A better way to handle that is just through subqueries.
SELECT woy, id, Tat1, Tat2, check1, check2
, CASE
WHEN check2=0 THEN (
CASE
WHEN (Tat1>(Tat2+30) OR (check1=1 AND (Tat1>Tat2+10))) THEN 1
ELSE 0
END
)
ELSE (
CASE WHEN (Tat1>(Tat2+30) OR (check1=1 AND (Tat1>Tat2+20))) THEN 1
ELSE 0
END
)
END AS BO
FROM (
SELECT WEEKOFYEAR(id_time) AS woy
, id
, exp1 AS Tat1
, exp2 AS Tat2
, exp3 AS check1
, exp4 AS check2
FROM datb
WHERE cid=18
AND id_time BETWEEN '2019-11-01 06:00:00' AND '2019-11-25 06:00:00'
) AS subQ
;
You can then tweak the above query for aggregation, or use it as a subquery for an aggregating outer query.

Count different values in a column, while doing total and group by different column values

We have a table with data from different nodes and one of the column will have status report as "compliant or non-compliant", sample data as below
I want to filter the table in such a way that if any of the checks on a node shows non compliant, it should be flagged as non-compliant and rest as compliant. Using below query i am able to do it
SELECT COUNT(*) AS total_nodes,
SUM(fully_compliant = 0) AS Non_compliant_nodes,
SUM(fully_compliant = 1) AS compliant_nodes
FROM (
SELECT Node, CASE WHEN SUM(Status = 'Compliant') = COUNT(*) THEN 1 ELSE 0 END AS fully_compliant
FROM your_table GROUP BY Node
)
Now, i want to group and split the result by dept as below, how can i achieve this
I think you're looking for this:
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
sum(case when 'Non-Compliant' then 1 else 0 end) as non_compliant_chk
from your_table
group by dept,
node
) v
group by dept;
With few modifications to what Brian suggested, I am able to get the desired result
select dept,
count(*) as total_nodes,
sum(case when non_compliant_chk = 0 then 1 else 0 end) as compliant_nodes,
sum(case when non_compliant_chk > 0 then 1 else 0 end) as non_compliant_nodes
from (
select dept,
node,
COUNT(CASE WHEN Compliance-Status = 'Non-Compliant' THEN 1 END) 'non_compliant_chk'
from table WHERE DOR >= DATE(NOW()) - INTERVAL 7 DAY
group by Dept,
Node
) v
group by Dept;

display column in horizontal format

I have a table as follows.
[Date] [Test_Item] [Result]
1/2/2014 A 1.1
2/2/2014 B 31.1
3/2/2014 C 20
5/2/2014 A 44
i would like display in the following format
[Test_Item] 1/2/2014 2/2/2014 3/2/2014 5/2/2014
A 1.1
B 31.1
C 20
A 44
How can i achieve this? please suggest the query in this case.
This is a basic use of the case statement:
select test_item,
(case when `date` = '1/2/2014' then result end) as `1/2/2014`,
(case when `date` = '2/2/2014' then result end) as `2/2/2014`,
(case when `date` = '3/2/2014' then result end) as `3/2/2014`,
(case when `date` = '5/2/2014' then result end) as `5/2/2014`
from table t;
You don't mention anything about types. If date is actually stored as a date or date/time (as it should be), then you should use ISO standard date formats for comparison, which assuming your format is d/m/yyyy, would be:
select test_item,
(case when `date` = '2014-02-01' then result end) as `1/2/2014`,
(case when `date` = '2014-02-02' then result end) as `2/2/2014`,
(case when `date` = '2014-02-03' then result end) as `3/2/2014`,
(case when `date` = '2014-02-05' then result end) as `5/2/2014`
from table t;

MySQL using Sum and Case

I'm trying to create a GridView with ASP.NET connecting to a MySQL database. The data appears like below.
BusinessUnit OrderDate Canceled
UnitA 1/15/2013 N
UnitA 10/1/2013 N
UnitB 10/15/2013 N
UnitB 10/22/2013 N
UnitB 10/22/2013 N
Based on the records above, I'd like the result to appear like below
BusinessUnit TodaysOrders ThisMonthsOrders ThisYearsOrders
UnitA 0 1 2
UnitB 2 3 3
My current code is below. It's giving me error (something about DatabaseName.sum does not exist. Check the
Function Name Parsing and Resolution' section... )
Select
SUM (CASE WHEN (OrderDate)=DATE(NOW()) THEN 1 ELSE 0 END) AS TodaysOrders,
SUM (CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) AND MONTH(OrderDate) = MONTH(CURDATE()) THEN 1 ELSE 0 END) AS ThisMonthsOrders,
SUM (CASE WHEN YEAR(main_order_managers.creation_date) = YEAR(CURDATE()) THEN 1 ELSE 0 END) AS ThisYearsOrders
code continues
FROM OrderTable WHERE OrderTable.Canceled. <> 'Y';
Is Sum Case the best use here?
The error is caused by the space between function name and parenthesis
SUM (CASE WHEN ...
^^
Read more Function Name Parsing and Resolution
Try
SELECT BusinessUnit,
SUM(CASE WHEN OrderDate = CURDATE() THEN 1 ELSE 0 END) TodaysOrders,
SUM(CASE WHEN DATE_FORMAT(OrderDate, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') THEN 1 ELSE 0 END) ThisMonthsOrders,
SUM(CASE WHEN YEAR(OrderDate) = YEAR(CURDATE()) THEN 1 ELSE 0 END) ThisYearsOrders
FROM OrderTable
WHERE Canceled <> 'Y'
GROUP BY BusinessUnit
Here is SQLFiddle demo