What am I supposed to put in <Series name column>?
I'm trying to do a Pie chart dashboard and I don't know where can I see information for that.
SELECT
UNIX_TIMESTAMP(<time_column>) as time_sec,
<value column> as value,
<series name column> as metric
FROM <table name>
WHERE $__timeFilter(time_column)
ORDER BY <time_column> ASC
For example, you have a table which contains columns 'server_name', 'cpu_load', 'reported_time'. So you can query this using
SELECT
UNIX_TIMESTAMP(reported_time) as time_sec,
cpu_load as value,
server_name as metric
FROM cpu_measurements
WHERE $__timeFilter(reported_time)
ORDER BY time_sec ASC
Related
I have a table:
CREATE TABLE IF NOT EXISTS "case_closed" (
"case_id" varchar(256) NOT NULL,
"closed_at" TIMESTAMP WITH TIME ZONE,
"disposition" VARCHAR(128),
PRIMARY KEY ("case_id")
);
And in Grafana, I need to display more than one graph, one per each 'disposition' value (I have 2 different disposition values at the moment).
I'm trying this query:
SELECT
$__timeGroupAlias(closed_at, $__interval),
disposition AS "metric",
COUNT(*) AS "value"
FROM case_closed
WHERE
$__timeFilter(closed_at)
GROUP BY 1,2
ORDER BY 1
And it gives me this ugly picture with only one single graph:
I searched here and from all I can see my query seems to be okay, but it still doesn't work. Maybe I'm missing something and it's not the query but some settings??
Solved! There was a small thing in the Query Builder (not sure why I didn't see it in any documentation):
I am using postgres alongside sequelize. I have encountered a case where I need to write a coustom query which groups the records are a particular field. I know for the remaning columns that are not used for grouping, I need to use a aggregate function like SUM. But the problem is that for some columns I need to get the one what is the latest one (DESC sorted by created_at). I see no function in sql to do so. Is my only option to write subqueries or is there a better way? Thanks?
For better understanding, If you look at the below picture, I want the group the records with address. So after the query there should only be two records, one with sydney and the other with new york. But when it comes to the distance, I want the result of the query to contain the distance form the row that was most recently created, i.e with the latest created_at.
so the final two query results should be:
sydney 100 2022-09-05 18:14:53.492131+05:45
new york 40 2022-09-05 18:14:46.23328+05:45
select address, distance, created_at
from(
select address, distance, created_at, row_number() over(partition by address order by created_at DESC) as rn
from table) x
where rn = 1
I need to group by the product name and by the date.
so from the above example, I am expecting to see the result as below. Is it possible . Can some one help me out please.Thanks!
Maybe I'm missing something in your question, but this is as simple as:
select product_name,
date,
count(*) as cnt
from the_table
group by product_name,
date
order by product_name
Btw: date is a horrible name for a column. First because it's a reserved word but more importantly because it does not document what you store in the column. It could be a "purchase_date", a "sold_date", an "expiry_date", ... ?
I have a report in JasperReports Server 5.2.0, in this report I have two input control
first one is:
COUNTRY (value column- COUNTRY_ID, visible column- COUNTRY_NAME)
and another one is
STATES (value column- STATE_ID and visible column - STATE_NAME).
These two parameters I am passing to the report, now at the end of the report I want to show the input parameter selection value so user can see what he has selected. But I am not able to get the visible values of parameters. I can print only value column value.
So my question is there any way to print the visible column values of input control in reports?
Till now , Visible Columns are not passed,where Value Columns are passed to Jrml File in Jasper.
To answer you question There is a way to print the visible column values.
It can be done using Sub-queries, by using 'Value Column' passed to the JRML file.
Then add the subquery to your main query and it`s done!!
In the above scenario You can display
State Name
SELECT States.STATE_NAME from States WHERE state_id = $P{STATE_ID} - If it is Single Select input control
SELECT GROUP_CONCAT(States.STATE_NAME) from States WHERE state_id = $X{IN,state_id,STATE_ID} - If it is Multi-Select input control
Country Name
SELECT Country.COUNTRY_NAME from COUNTRY WHERE country_id= $P{COUNTRY_NAME} - If it is Single Select input control
SELECT GROUP_CONCAT(Country.COUNTRY_NAME) from COUNTRY WHERE country_id = $X{IN,state_id,STATE_ID} - If it is Multi-Select input control
Suppose this your main query where Country and State are missing..
SELECT
`id`,
`product-id`,
`name`,
`description`,
`branch`,
`stock`,
`price`
FROM
`products`
WHERE
`name` LIKE "%car%"
The modified query will looks like..
SELECT
`id`,
`product-id`,
`name`,
`description`,
`branch`,
`stock`,
`price`,
(SELECT States.STATE_NAME from States WHERE state_id = $P{STATE_ID}) as state,
(SELECT Country.COUNTRY_NAME from COUNTRY WHERE country_id=P{COUNTRY_NAME}) as country
FROM
`products`
WHERE
`name` LIKE "%car%"
I create a table in HIVE.
It has the following columns:
id bigint, rank bigint, date string
I want to get avg(rank) per month. I can use this command. It works.
select a.lens_id, avg(a.rank)
from tableA a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
However, I also want to get date information. I use this command:
select a.lens_id, avg(a.rank), a.date_saved
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
It complains: Expression Not In Group By Key
The full error message should be in the format Expression Not In Group By Key [value].
The [value] will tell you what expression needs to be in the Group By.
Just looking at the two queries, I'd say that you need to add a.date_saved explicitly to the Group By.
A walk around is to put the additional field in a collect_set and return the first element of the set. For example
select a.lens_id, avg(a.rank), collect_set(a.date_saved)[0]
from lensrank_archive a
group by a.lens_id, year(a.date_saved), month(a.date_saved);
This is because there is more than one ‘date_saved’ record under your group by. You can turn these ‘date_saved’ records into arrays and output them.