Write mysql custom query in query builder of grafana - grafana

I made a time-series graph screenshot attached and the generated query is
SELECT
UNIX_TIMESTAMP(date_of_sign_up) DIV 600 * 600 AS "time",
status AS "borrower_status",
count(id) AS "Number Of Borrowers"
FROM borrowers
WHERE
date_of_sign_up BETWEEN FROM_UNIXTIME(1631158800) AND FROM_UNIXTIME(1643881661) AND
status IN ('APPROVED','INACTIVE','BASIC') AND
client_id = 130454654
GROUP BY 1, status
ORDER BY UNIX_TIMESTAMP(date_of_sign_up) DIV 600 * 600
Now i want to pass from and to from url in mysql query builder, below is the query i tried to write but it did not work
SELECT
$__timeGroupAlias(date_of_sign_up,10m),
status AS "borrower_status",
count(id) AS "Number Of Borrowers"
FROM borrowers
WHERE
date_of_signup between $from AND $to AND
status IN (${BorrowerStatus}) AND
client_id = [[tenant]]
GROUP BY 1, status
ORDER BY $__timeGroup(date_of_sign_up,10m)
What is the correct way of passing &from and &to from URL params in MySQL query builder date_of_signup between

date_of_signup between $__timeFrom() AND $__timeTo()

Related

Postgres get related post with fallback

I wanted to get related posts from my table.So I tried some code like below
"SELECT post,category FROM library WHERE title LIKE '%" + query + "%' LIMIT 20"
Now sometimes it returns 20 results , but sometimes less than 20 , So when response is less than 20 , I need to fill with random posts which has category I mention , For example something like below
SELECT post,category FROM library WHERE category = 'php' OFFSET floor(random()*20) LIMIT 20;
For example if my search query returns 5 results , it should get random 15 posts based on my 2nd query.
Perhaps you can use UNION ALL?
SELECT * FROM (
(SELECT post,category
FROM library
WHERE title LIKE '%" + query + "%' LIMIT 20)
UNION ALL
(SELECT post,category
FROM library
WHERE category = 'php' OFFSET floor(random()*20) LIMIT 20)
) LIMIT 20;

How to find the page number a record is in, in a set of paged records

I have paged SQL query that is like this
SELECT cistranm.transactionid AS transactionid
,cistranm.TransactionGroupId AS TransactionGroupId
,cisdata.id AS dCPrimaryKey
FROM dbo.cismiscalculations AS calc
INNER JOIN dbo.cismisdata AS cisdata ON calc.cismisdataid = cisdata.id
INNER JOIN dbo.cistransactionmessage AS cistranm ON cisdata.cistransactionmsgid = cistranm.id
INNER JOIN dbo.cistransactionmessagestatus AS cistrans ON cistranm.id = cistrans.cistransactionmessageid
WHERE convert(DATE, cistranm.transactioncompletiontimestamp) = convert(DATE, #LastRunDateTime)
ORDER BY cistranm.transactioncompletiontimestamp
,calc.id
,calc.earneddate offset(#PageNumber - 1) * #RowsOfPage rows
Here I supply the PageNumber and the expected rows per page (Page number can be 1 through 10, expected rows is always 1000)
Lets say on page 5 I have a transactionid as abc123. How can modify this query to return me the page number "5" if I supply the transactionId ?
Thanks in advance
For those looking but not sure what/where to ask this is how you can find what page a paged record might be in
FLOOR(CAST((CAST(T.row_num as decimal) / CAST(#RowsOfPage as decimal)) as decimal)) as page_num
FROM
(
SELECT...

Code igniter adds an "Order by" to custom query

On a simple custom query, CodeIgniter adds an "Order by" clause. The query is
$sql = "SELECT city_location
FROM admin_users
WHERE id = " . $_SESSION['admin']['user_id'] . "
ORDER BY id asc
LIMIT 1";
$query = $this->db->query($sql);
The result is after I run $query = $this->db->query($sql) is:
ERROR: column "name" does not exist LINE 4: ORDER BY "name" ASC ^
SELECT "city_location"
FROM "admin_users"
WHERE "id" = '61'
ORDER BY "name"
ASC LIMIT 1
Questin is: why codeigniter adds a default ORDER BY to my query. The model does not contain any default ordering.
Thank you
You will try this cod$this->db->select('city_location');// Column name
$this->db->from('admin_users');// Table name
$this->db->where('id' , $_SESSION['admin']['user_id']);// Your condition Here.First parameter is columnname and second one is value
$this->db->order_by('id');

For each loop for a table in OpenEdge 10.2b takes more time

Below for each loop takes more time and i cant able to trace index usage using XREF as the table uses Oracle Schema.Please Help me.
Need to generate report for different Report Type ( Report type is the input parameter in my code ).Single report type may contain more than 50,000 records how to access all the record within minute.Index detail also mentioned below for each loop.
FOR EACH Report
FIELDS(EXTRACTDATE STATUS MailingType ReportType ReportNumber
RequestID CustID)
WHERE Report.EXTRACTDATE < Today
AND Report.ReportType = 'Customer Report'
AND Report.STATUS = 'Pending'
AND (Report.MailingType = "LETTER"
OR Report.MailingType = "Mail") NO-LOCK:
< Statements >
.
.
END.
**Index Detail**
CREATE INDEX "TYPE_IDX1" ON "Report" ("EXTRACTDATE" DESC, "ReportTYPE", "STATUS", "MailingTYPE", "PROGRESS_RECID")
CREATE INDEX "REQ_IDX1" ON "Report" ("REQUESTID", "PROGRESS_RECID")
CREATE INDEX "RTTYP_IDX1" ON "Report" ("ReportTYPE","PROGRESS_RECID")
The "OR" at the end will slow things down considerably - the AVM does better if you split it up into two sets of AND statements and OR the result, like so:
WHERE (Report.EXTRACTDATE < Today
AND Report.ReportType = 'Customer Report'
AND Report.STATUS = 'Pending'
AND Report.MailingType = "LETTER")
OR
(Report.EXTRACTDATE < Today
AND Report.ReportType = 'Customer Report'
AND Report.STATUS = 'Pending'
AND Report.MailingType = "Mail")

mysqli - fetch several rows applying several where conditions

With only one query I want to fetch specific rows that contains a range of several string values. Is it possible to modify my WHERE to do this?
This is the code for one country but I want to select several rows for several countries. For example: Jamaica, Portugal and Dominica.
// Select countries to show
$specific_country = Dominica;
// Select and write SPECIFIC ROWS data
$result = mysqli_query($con, "SELECT * FROM $table WHERE Country='$specific_country'");
This will only get the rows of Dominica but I wanted to use something similar to get Jamaica, Portugal and Dominica but all on the same query.
You can use IN()
SELECT *
FROM table_name
WHERE Country IN('Jamaica', 'Portugal', 'Dominica')
Here is SQLFiddle demo
In php
$countries = array('Jamaica', 'Portugal', 'Dominica');
$sql = "SELECT * FROM table_name WHERE Country IN('". implode("','", $countries) . "')";
$result = mysqli_query($con, $sql);
...