I'm building a dashboard in Grafana. One of my graphs is based on this query:
SELECT
users.name as Name,
users.last_name as Last,
users.email as Mail,
sum(orders.total_amount) as Sales
FROM orders
INNER JOIN users ON orders.user_id=users.id
WHERE orders.status=4
GROUP BY 1,2,3;
So I'm looking for the top clients based on how much they spent. What I need now is that the total sales change with the Time Ranges of the dashboard. Right now I have the same result with every Quick Range I use (it returns me the total sales in all the database and not in the time range I request). Any help?
Check used Grafana datasource doc (MySQL, PostgreSQL). There are available macros, which you will use in your SQL WHERE time condition.
http://docs.grafana.org/features/datasources/postgres/#macros
http://docs.grafana.org/features/datasources/mysql/#macros
Related
I'm trying display the user growth per day using Grafana Time Series with SQL Server. However I found the documentation to be unhelpful and my queries are incorrect.
The following returns a constant value of 1 for every day. What do I need to change to display the number of new users created per day?
Thank you very much in advance.
SELECT
$__timeGroup([created_at],'1d') as time,
COUNT(id) as value,
'users' as metric
FROM [db].[user]
WHERE $__timeFilter([created_at])
GROUP BY [created_at]
ORDER BY 1
This works for me:
SELECT
$__timeGroup(created_at, '1d') AS time,
COUNT(id) as 'New Users'
FROM [db].[user]
GROUP BY $__timeGroup(created_at, '1d')
ORDER BY 1
I am using Google Big Query to query the daily Google analytics reports for my website. I am running queries on 7 tables (the 7 daily reports) at a time, because I want to use weekly results.
I would like to run a query that shows "Users with >= x sessions and with >= y page views". I am having difficulties framing this query.
The resulting table should show the fullVisitorId, totals.visits (The number of sessions), totals.pageviews (Total number of pageviews within the session). Should I use a subquery, or is there some other method?
Please use the following link if you'd like to have a look at the complete scheme: https://support.google.com/analytics/answer/3437719?hl=en
A basic query would look like:
SELECT
fullVisitorId,
SUM(totals.visits) as visits,
SUM(totals.pageviews) as pageviews,
FROM
TABLE_DATE_RANGE([google.com:analytics-bigquery:LondonCycleHelmet.ga_sessions_],
TIMESTAMP('2013-09-10'),
TIMESTAMP('2013-09-17'))
GROUP BY
fullVisitorId
HAVING visits>0 and pageviews>0
To run this query on a sample database visit: https://support.google.com/analytics/answer/3416091?hl=en
I make a database for theatre tickets selling system. There are tables with theatres, spectacles and sessions of spectacles (one spectacle can be showed many times). In table with sessions there is a date field. I need to make a form, where user can choose a theatre and a month, and will get a report with a list of sessions in chosen theatre for chosen month. Does anyone know how to do it?
i guess you are searching for the Month Function.So when you select a month you need to perform this on the query :
WHERE Month(DateOfSession) = " & me.cboSelectMonth
Since bigquery is append-only, I was thinking about stamping each record I upload to it with an 'effective date' similar to how peoplesoft works, if anybody is familiar with that pattern.
Then, I could issue a select statement and join on the max effective date
select UTC_USEC_TO_MONTH(timestamp) as month, sum(amt)/100 as sales
from foo.orders as all
join (select id, max(effdt) as max_effdt from foo.orders group by id) as latest
on all.effdt = latest.max_effdt and all.id = latest.id
group by month
order by month;
Unfortunately, I believe this won't scale because of the big query 'small joins' restriction, so I wanted to see if anyone else had thought around this use case.
Yes, adding a timestamp for each record (or in some cases, a flag that captures the state of a particular record) is the right approach. The small side of a BigQuery "Small Join" can actually return at least 8MB (this value is compressed on our end, so is usually 2 to 10 times larger), so for "lookup" table type subqueries, this can actually provide a lot of records.
In your case, it's not clear to me what the exact query you are trying to run is.. it looks like you are trying to return the most recent sales times of every individual item - and then JOIN this information with the SUM of sales amt per month of each item? Can you provide more info about the query?
It might be possible to do this all in one query. For example, in our wikipedia dataset, an example might look something like...
SELECT contributor_username, UTC_USEC_TO_MONTH(timestamp * 1000000) as month,
SUM(num_characters) as total_characters_used FROM
[publicdata:samples.wikipedia] WHERE (contributor_username != '' or
contributor_username IS NOT NULL) AND timestamp > 1133395200
AND timestamp < 1157068800 GROUP BY contributor_username, month
ORDER BY contributor_username DESC, month DESC;
...to provide wikipedia contributions per user per month (like sales per month per item). This result is actually really large, so you would have to limit by date range.
UPDATE (based on comments below) a similar query that finds "num_characters" for the latest wikipedia revisions by contributors after a particular time...
SELECT current.contributor_username, current.num_characters
FROM
(SELECT contributor_username, num_characters, timestamp as time FROM [publicdata:samples.wikipedia] WHERE contributor_username != '' AND contributor_username IS NOT NULL)
AS current
JOIN
(SELECT contributor_username, MAX(timestamp) as time FROM [publicdata:samples.wikipedia] WHERE contributor_username != '' AND contributor_username IS NOT NULL AND timestamp > 1265073722 GROUP BY contributor_username) AS latest
ON
current.contributor_username = latest.contributor_username
AND
current.time = latest.time;
If your query requires you to use first build a large aggregate (for example, you need to run essentially an accurate COUNT DISTINCT) another option is to break this query up into two queries. The first query could provide the max effective date by month along with a count and save this result as a new table. Then, could run a sum query on the resulting table.
You could also store monthly sales records in separate tables, and only query the particular table for the months you are interested in, simplifying your monthly sales summaries (this could also be a more economical use of BigQuery). When you need to find aggregates across all tables, you could run your queries with multiple tables listed after the FROM clause.
I need a report that has office, date and order count. I need the total count of orders per month, but only 1 order count per day.
e.g.
West 1/1/2009 1 order
West 1/1/2009 1 order
West 1/2/2009 1 order
on my report I would see
West 1/1/2009 1 order
West 1/2/2009 1 order
and my total orders would be 2.
This would be really easy with SQL, I know, but I do not have access.
Are you just looking for this?
SELECT DISTINCT Office, Date, OrderCount FROM YourTable
This would duplicate your results, but the data set is too small to know for sure if this is what you're trying to accomplish. Using the DISTINCT clause would return only unique combinations of Office, Date, and OrderCount - in this case, one line per day/office.
UPDATE: Ah - I didn't read the part where you don't have SQL access. You still have two choices:
In Crystal Reports Designer, in the "Database" menu, check the "Select Distinct Records" option at the bottom of the menu.
Edit the SQL query directly - Database menu -> Database Expert -> Under "Current Connections", click "Add new command" and type your SQL command. Modify the one I provided above to meet your needs, and it should do the trick.
You can create three groups, one for office, one for date, and one for order. Then put the fields in the day group footer and suppress the other sections. This will cause the report to show a new section for each day, but only show one row for each order. Then you can add your running total to the section. Set the running total up to sum the field you want, evaluate on change of day group and then reset on change of month (you'll need to set a formula up for this one to evaluate the month).
This should group and order the report like you are looking for and will have a running total that will run along side which will reset per month. Hope this helps.