Grafana bar chart - bar-chart

I am trying to create a bar chart on grafana in which it shows latency for two categories from a field which I query from the db. There are total three fields an hour field, category and an aggregate calculation. Basically I want hour field on X-axis and two bars to show aggregation for each category and aggregate field on Y-axis.
Currently it looks like this:
In the image below those marked in Red are the two categories. Lets say A,B. The latency is my aggregation marked in blue and the green mark is hour.
Basically, I want to achieve something like below image: Category A,B on x-axis wrt hour, and latency on y-axis.
This is my query:
select * from (
select extract(hour from event_datetime) Hour, category, (source_timedelta_s_ + pipeline_timedelta_s_) Latency from <table>
where extract(date from event_datetime) = '2022-12-13' and extract(hour from event_datetime) > 12
)
PIVOT (
avg(Latency)
FOR category IN ('A','B')
) t
order by t.Hour
Your help will be appreciated thanks. Sorry If my explanation is confusing.

Related

How to show all legend keys for 0 or null value results, in SSRS?

I'm trying to build a bar chart that displays Events per Year. Each year divided into 4 quarters.
The problem arises in case that one, or more, quarter doesn't have any events in any year. In this case the legend key for this quarter doesn't appear.
For example, if no events took place in 1st quarter, then the legend show keys for 2nd, 3rd and 4th quarters only.
I need to make the legend always shows all keys, even if one, or more, doesn't have any values.
How can I accomplish this??
A similar question is here but the answer doesn't solve the problem.
Any help is appreciated.
Applying #alejandro answer, I get this result.
The missing quarter, Quarter 3, is renamed to Series1 and located at the first place. Any suggestions?
There is no way to build the legends and labels based on data that is not present in your fields.
You can create a table or a CTE with all quarters, then use LEFT JOIN operator to relate each row in your data with the corresponding quarter, if a quarter doesn't match any row in your data it will return null for each column in your data but will include the quarter, which lets SSRS build the legend.
WITH quarters
AS (SELECT 1 [Quarter]
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4)
SELECT a.[quarter],
b.*
FROM quarters a
LEFT JOIN YourDataTable b
ON a.[quarter] = b.[quarter]
After that you can use Quarter field in your Chart and it will show all quarters in the legend even if there is no data in one or more quarters.
Let me know if this helps.

Difference between rows Mysql Query

I have one table which is having four fields:
trip_paramid, creation_time, fuel_content,vehicle_id
I want to find the difference between two rows.In my table i have one field fuel_content.Every two minutes i getting packets and inserting to database.From this i want to find out total refuel quantity.If fuel content between two packets is greater than 2,i will treat it as refueling quantity.Multiple refuel may happen in same day.So i want to find out total refuel quantity for a day for a vehicle.I created one table schema&sample data in sqlfiddle. Can anyone help me to find a solution for this.here is the link for table schema..http://www.sqlfiddle.com/#!2/4cf36
Here is a good query.
Parameters (vehicle_id=13) and (date='2012-11-08') are injected in the query, but they are parameters to be modified.
You can note that have I chosen an expression using creation_time<.. and creation_time>.. in instead of DATE(creation_time)='...', this is because the first expression can use indexes on "creation_time" while the second one cannot.
SELECT
SUM(fuel_content-prev_content) AS refuel_tot
, COUNT(*) AS refuel_nbr
FROM (
SELECT
p.trip_paramid
, fuel_content
, creation_time
, (
SELECT ps.fuel_content
FROM trip_parameters AS ps
WHERE (ps.vehicle_id=p.vehicle_id)
AND (ps.trip_paramid<p.trip_paramid)
ORDER BY trip_paramid DESC
LIMIT 1
) AS prev_content
FROM trip_parameters AS p
WHERE (p.vehicle_id=13)
AND (creation_time>='2012-11-08')
AND (creation_time<DATE_ADD('2012-11-08', INTERVAL 1 DAY))
ORDER BY p.trip_paramid
) AS log
WHERE (fuel_content-prev_content)>2
Test it:
select sum(t2.fuel_content-t1.fuel_content) TotalFuel,t1.vehicle_id,t1.trip_paramid as rowIdA,
t2.trip_paramid as rowIdB,
t1.creation_time as timeA,
t2.creation_time as timeB,
t2.fuel_content fuel2,
t1.fuel_content fuel1,
(t2.fuel_content-t1.fuel_content) diffFuel
from trip_parameters t1, trip_parameters t2
where t1.trip_paramid<t2.trip_paramid
and t1.vehicle_id=t2.vehicle_id
and t1.vehicle_id=13
and t2.fuel_content-t1.fuel_content>2
order by rowIdA,rowIdB
where (rowIdA,rowIdB) are all possibles tuples without repetition, diffFuel is the difference between fuel quantity and TotalFuel is the sum of all refuel quanty.
The query compare all fuel content diferences for same vehicle(in this example, for vehicle with id=13) and only sum fuel quantity when the diff fuel is >2.
Regards.

MySQL SUM column1 if column2 equals today and column3 equals specific test

I was cruising along setting up a production schedule at work, but I have been stuck on something that seems like it should be so easy.
I have a table called orders with columns for date, item and quantity.
I am trying to total the quantity of each item for a specific date.
I've been stuck for hours trying all sorts of things. Not sure if I am even close.
For example:
SELECT * FROM orders WHERE date_prod='2011-10-01' AS 'today';
SELECT item, date_prod, SUM( quantity )
FROM today
GROUP BY item
HAVING date_prod = '2011-10-01'
LIMIT 0 , 30
Tried playing around a bunch already. VIEW is not a practical way for me to do this because I want to be able to query a specific date far into the future and see what the total quantity is for each item ordered that day.
Something tells me this should be easy but I'm pretty new at this.
Thanks in advance!
What about:
SELECT item, SUM( quantity ) AS total
FROM orders
WHERE date_prod = '2011-10-01'
GROUP BY item

Group results by period

I have some data which I want to retrieve, but I want to have it grouped by a specific number of seconds. For example if my table looks like this:
| id | user | pass | created |
The created column is INT and holds a timestamp (number of seconds from 1970).
I would want the number of users that are created between last month and the current date, but show them grouped by let's say 7*24*3600 (a week). So if in the range there are 1000 new users, have them show up how many registered each week (100 the first week, 450 the second, 50 the third and 400 the 4th week -- something like this).
I've tried grouping the results by created / 7*24*3600, but that's not working.
How should my query look like?
You need to use integer division div otherwise the result will turn into a real and none of the weeks will resolve to the same value.
SELECT
(created div (7*24*60*60)) as weeknumber
, count(*) as NewUserCount
FROM users
WHERE weeknumber > 1
GROUP BY weeknumber
See: http://dev.mysql.com/doc/refman/5.0/en/arithmetic-functions.html
You've got to keep the integer part only of that division. You can do it with the floor() function.
Have you tried select floor(created/604800) as week_no, count(*) from users group by floor(created/604800) ?
I assume you've got the "select users created in the last month" part sorted out.
Okay here are the possible options you may try:
GROUP BY DAY
select count(*), DATE_FORMAT(created_at,"%Y-%m-%d") as created_day FROM widgets GROUP BY created_day
GROUP BY MONTH
select count(*), DATE_FORMAT(created_at,"%Y-%m") as created_month FROM widgets GROUP BY created_month
GROUP BY YEAR
select count(*), DATE_FORMAT(created_at,"%Y") as created_year FROM widgets GROUP BY created_year

Query by month from date field

I have a set of Access d/b's grouped already by year. within a given year, I have a field caleld REPORTDATE which is a standard mm/dd/yyyy field. However, I need to produce queries that return data by the month. For example, I just want to see records for Jan, recs for Feb, Recs for March, etc., so that I can sum them and work wwith thm.
Do I use an expression in the query design view Criteria field?
Thanks in advance.
I just want to see records for Jan, recs for Feb, Recs for March, etc., so that I can sum them and work wwith thm.
You can do all of that in one sql statement:
select month(reportdate), sum( the column you wish to sum )
from tablename
group by month(reportdate);
BUT WAIT THERE'S MORE!
Further say that there are several salepersons selling stuff, and you wish to show each salesperson's sales by month
select month(reportdate), salesperson, sum( the column you wish to sum )
from tablename
group by month(reportdate), salesperson;
That shows the sum per month per salesperson.
You know the Germans always make good stuff!
What it you wanted to see the same sums, but rtaher than comparing salespeople against each other in each month, you wanted to compare, for each salesperson, how they did from one month to another?
Just reverse the order of the group by:
select month(reportdate), saleperson, sum( the column you wish to sum )
from tablename
group by salesperson, month(reportdate);
Tacos, Fettuccini, Linguini, Martini, Bikini, you're gonna love my nuts!
The power of SQL! As seen on TV! Order now!
"select month(reportdate), sum( the column you wish to sum )from tablenamegroup by month(reportdate);" THIS IS VERY HELPFUL, THANK YOU. AND YOU ARE HILARIOUS. HOWEVER, can you clarify for me where the heck this code goes?! In the expresison Builder or what? Thank you SO much. – rick (19 mins ago)
In Access, I think from the graphical Query Builder thing's menu, select edit|SQL, and just type. And never go back to graphical!
You're a hard-charging forward-thinking entrepreneurially-minded man on the move! This is not your father's Oldsmobile! You wouldn't use an on-screen keyboard to type a document, dragging and dropping letters on the page, would you?! So why do that to build a SQL Query? Get into SQL! AS SEEN ON TV! All the cool kids and hep cats are doin' it! Order NOW!
You can use format, for example:
Format([REPORTDATE],"mmm yy")
Or Month:
SELECT * FROM Table WHERE Month([REPORTDATE]) = 10
An outline of query that may suit, paste this into the SQL view of
the query design window, changing table to the name of your table:
SELECT Format([REPORTDATE],"yyyy mm"), Count([ReportDate])
FROM Table
GROUP BY Format([REPORTDATE],"yyyy mm")
I wouldn't do this in the report's recordsource. I'd make the recordsource a regular SELECT statement and use the report's sorting/grouping. If you group on a date field (one that is really date type), you get the choice to GROUP ON:
Each Value (default)
Year
Qtr
Month
Week
Day
Hour
Minute
I think this is faster than a GROUP BY on a function, but someone who was interested should actually try it.
Certainly if your SELECT with GROUP BY has no WHERE clause, it's going to be a lot more efficient if you run the report with filtered values.