# Wish list: SQL for canned reports

**URL:** https://meta.discourse.org/t/wish-list-sql-for-canned-reports/245938
**Category:** Data & reporting
**Tags:** sql-query, dashboard-reports
**Created:** [November 17, 2022, 5:23pm UTC](https://meta.discourse.org/t/wish-list-sql-for-canned-reports/245938 "2022-11-17T17:23:12Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ganncamp](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ganncamp/32/106199_2.png) [@ganncamp](https://meta.discourse.org/u/ganncamp)
#### Post date: [November 17, 2022, 5:23pm UTC](https://meta.discourse.org/t/wish-list-sql-for-canned-reports/245938/1 "2022-11-17T17:23:12Z")

</div>

I semi-regularly find myself wanting to take one of the standard reports (today it’s Consolidated Pageviews) and go a little farther with it.

Rather than having to start from scratch, it would be an amazing 🎄 🎁 if I could get from the report to its SQL to iterate.

---

<div class="post-metadata">

### Author: ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)
#### Post date: [November 17, 2022, 7:32pm UTC](https://meta.discourse.org/t/wish-list-sql-for-canned-reports/245938/4 "2022-11-17T19:32:48Z")

</div>

It’s not built with SQL, but that’s ok, we can derive it …

Here’s the overall guide on Meta:

> [@Admin dashboard report reference guide](https://meta.discourse.org/t/admin-dashboard-report-reference-guide/240233):
>
> bookmark This is a reference guide for describing how the Admin Dashboard Reports function, the data they’re displaying, the corresponding [Data Explorer](https://meta.discourse.org/t/32566?silent=true) SQL queries, and where to find the Ruby code for each report. person_raising_hand Required user level: Staff Discourse contains several built-in admin dashboard Reports that can be useful for exploring stats about a community. To access these reports, you can visit discourse.example.com/admin/dashboard/reports on your site ( or click the…

Specifically: [discourse/app/models/concerns/reports/consolidated\_page\_views.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/main/app/models/concerns/reports/consolidated_page_views.rb)

It’s built using [a Rails Model, `ApplicationRequest `](https://github.com/discourse/discourse/blob/main/app/models/application_request.rb), e.g.

```plaintext
[3] pry(main)> ApplicationRequest.last(10)
=> [#<ApplicationRequest:0x000055c8edcf99f8 id: 4798, date: Thu, 17 Nov 2022, req_type: "http_total", count: 3080>,
 #<ApplicationRequest:0x000055c8edcf9ac0 id: 4799, date: Thu, 17 Nov 2022, req_type: "http_background", count: 1014>,
 #<ApplicationRequest:0x000055c8edcf9bb0 id: 4800, date: Thu, 17 Nov 2022, req_type: "page_view_crawler", count: 539>,
 #<ApplicationRequest:0x000055c8edcf9c78 id: 4801, date: Thu, 17 Nov 2022, req_type: "http_2xx", count: 1929>,
 #<ApplicationRequest:0x000055c8edcf9d68 id: 4802, date: Thu, 17 Nov 2022, req_type: "http_4xx", count: 52>,
 #<ApplicationRequest:0x000055c8edcf9e30 id: 4803, date: Thu, 17 Nov 2022, req_type: "http_3xx", count: 85>,
 #<ApplicationRequest:0x000055c8edcf9f20 id: 4804, date: Thu, 17 Nov 2022, req_type: "page_view_anon", count: 40>,
 #<ApplicationRequest:0x000055c8edcf9fe8 id: 4805, date: Thu, 17 Nov 2022, req_type: "page_view_logged_in", count: 148>,
 #<ApplicationRequest:0x000055c8edcfa0d8 id: 4806, date: Thu, 17 Nov 2022, req_type: "page_view_logged_in_mobile", count: 134>,
 #<ApplicationRequest:0x000055c8edd00a00 id: 4807, date: Thu, 17 Nov 2022, req_type: "page_view_anon_mobile", count: 2>]

```

The corresponding table you need is `application_requests` (but I don’t see this exposed in [Data Explorer](https://meta.discourse.org/t/32566?silent=true), maybe I missed it?)

The req\_type(s) in this report are:

```plaintext
  page_view_logged_in
  page_view_anon
  page_view_crawler

```

It is regular a sum and group by, so probably, you could start off with, on the Rails console:

```plaintext
ApplicationRequest.where(req_type:["page_view_logged_in", "page_view_anon", "page_view_crawler"]).where('date BETWEEN ? AND ?', '11/14/2022', '11/17/2022')

```

for example

or the SQL equivalent:

```plaintext
SELECT * FROM application_requests WHERE req_type IN (7, 8, 6) AND (date BETWEEN '11/14/2022' AND '11/17/2022')

```

Some of the aggregation is already done for you as the data is in a count column.

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [December 17, 2022, 7:33pm UTC](https://meta.discourse.org/t/wish-list-sql-for-canned-reports/245938/5 "2022-12-17T19:33:06Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
