# Basic SQL query question

**URL:** <https://discourse.pi-hole.net/t/basic-sql-query-question/63566>\
**Category:** Community Help\
**Created:** [June 24, 2023, 5:39am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566 "2023-06-24T05:39:02Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 24, 2023, 5:39am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/1 "2023-06-24T05:39:02Z")

</div>

Hello,

I need to know every day a specific client queried a specific domain.

I know from a search in the UI that the client performed multiple queries every day that it queried the domain, but I only want one result per day. I need the output in a format I can put in a spreadsheet, so had been trying to find a good `sqllite3` query but I'm struggling. Could someone please help?

Thank you!

PS: I've read the documentation at [Redirecting...](https://docs.pi-hole.net/database/ftl/) If you're strong on this stuff it would be awesome if you could please add some examples to that page please!

---

<div class="post-metadata">

**Author:** ![yubiuser](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/yubiuser/32/12100_2.png) [@yubiuser](https://discourse.pi-hole.net/u/yubiuser)\
**Post date:** [June 24, 2023, 8:41am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/2 "2023-06-24T08:41:04Z")

</div>

Try something like

```auto
pihole-FTL sqlite3 /etc/pihole/pihole-FTL.db "Select domain, count(domain) from queries where client='IP' and domain='DOMAIN' and timestamp > strftime('%s','now','-1 days');"

```

Adjust the time/date modifier to your needs

> **[Date And Time Functions](https://www.sqlite.org/lang_datefunc.html)**

---

<div class="post-metadata">

**Author:** ![yubiuser](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/yubiuser/32/12100_2.png) [@yubiuser](https://discourse.pi-hole.net/u/yubiuser)\
**Post date:** [June 24, 2023, 8:42am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/3 "2023-06-24T08:42:06Z")

</div>

To export to cvs adjust like shown here: [Long Term Query Log -\> Excel Pivot Chart - #7 by yubiuser](https://discourse.pi-hole.net/t/long-term-query-log-excel-pivot-chart/46422/7)

---

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 24, 2023, 11:07am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/4 "2023-06-24T11:07:51Z")

</div>

Thank you @yubiuser !

That gets me a count of the queries, but sorry I didn't ask my question clearly.

I need to know the date every time this DNS query was made. It will have occurred multiple times on many dates, but I can deduplicate those if needed.

Basically I need to know when I was working from home for tax purposes! I don't visit the specific site unless doing work.

---

<div class="post-metadata">

**Author:** ![jfb](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/jfb/32/4332_2.png) [@jfb](https://discourse.pi-hole.net/u/jfb)\
**Post date:** [June 24, 2023, 12:46pm UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/6 "2023-06-24T12:46:03Z")

</div>

Make a cron to run this query every day at 2359. If the result is not blank, you worked from home that day.

---

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 24, 2023, 10:04pm UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/7 "2023-06-24T22:04:51Z")

</div>

Thanks @jfb but I asked for a query as I need historic data

---

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 24, 2023, 10:07pm UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/8 "2023-06-24T22:07:13Z")

</div>

How about I explain the original question clearer.

Which SQL query could I use to return the date/time of every query for one specific domain please,

Thank you!

---

<div class="post-metadata">

**Author:** ![rdwebdesign](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/rdwebdesign/32/51363_2.png) [@rdwebdesign](https://discourse.pi-hole.net/u/rdwebdesign)\
**Post date:** [June 24, 2023, 10:10pm UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/9 "2023-06-24T22:10:00Z")

</div>

There is no field with formatted Date/Time output, but there is a `timestamp` field.

Just add the field to your query:

```auto
SELECT timestamp, domain, count(domain) FROM queries ...

```

---

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 24, 2023, 10:14pm UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/10 "2023-06-24T22:14:36Z")

</div>

Thank you @rdwebdesign!

I'll give that a try when I can in a few hours.

---

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 25, 2023, 4:36am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/11 "2023-06-25T04:36:42Z")

</div>

Thanks so much for your answers everyone!

I got what I needed with this query:

```auto
nice pihole-FTL sqlite3 /etc/pihole/pihole-FTL.db "SELECT timestamp, domain FROM queries where client='10.10.10.10' and domain='domain.i.am.looking.for.com' and timestamp > strftime('%s','now','-9999 days');"

```

...which gives me an output like

```auto
1660884263|domain.i.am.looking.for.com
1660894170|domain.i.am.looking.for.com
1660894480|domain.i.am.looking.for.com
1660895720|domain.i.am.looking.for.com

```

...and I can then use a simple shell script with `date -d @<date string>` to convert those in to time and date strings. I'll then dedupe them so I get a list of each day the domain was queried at least once.

---

<div class="post-metadata">

**Author:** ![yubiuser](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/yubiuser/32/12100_2.png) [@yubiuser](https://discourse.pi-hole.net/u/yubiuser)\
**Post date:** [June 25, 2023, 9:40am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/12 "2023-06-25T09:40:45Z")

</div>

Try

```auto
pihole-FTL sqlite3 /etc/pihole/pihole-FTL.db "SELECT date(timestamp,'unixepoch', 'localtime'), domain FROM queries where client='IP' and domain='DOMAIN' and timestamp > strftime('%s','now',' -999 days') group by date(timestamp,'unixepoch','localtime');"

```

I think you could even remove the timestamp selection as it seems you try to just get "all".

---

<div class="post-metadata">

**Author:** ![gravity](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/gravity/32/15779_2.png) [@gravity](https://discourse.pi-hole.net/u/gravity)\
**Post date:** [June 29, 2023, 3:22am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/13 "2023-06-29T03:22:40Z")

</div>

Thanks, but I'm afraid that particular query gives me no results.

I've resolved this for now with the previous post, even if I had to do some manipulation of the output in a shell, it's all good thank you!

---

<div class="post-metadata">

**Author:** ![rdwebdesign](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/rdwebdesign/32/51363_2.png) [@rdwebdesign](https://discourse.pi-hole.net/u/rdwebdesign)\
**Post date:** [June 29, 2023, 4:24am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/14 "2023-06-29T04:24:03Z")

</div>

> [@gravity](#):
>
> and I can then use a simple shell script with `date -d @<date string>` to convert those in to time and date strings.

You can avoid a script with `date` if you change your SQL query to use the SQLite3 [`datetime()` function](https://www.sqlite.org/lang_datefunc.html) or `strftime()` if you need a different format:

```sql
-- Old query:
SELECT timestamp, domain FROM queries WHERE ...

-- New query using datetime() function
SELECT datetime(timestamp,'unixepoch', 'localtime'), domain FROM queries WHERE ...

```

---

<div class="post-metadata">

**Author:** ![yubiuser](https://discourse-cdn.pi-hole.net/user_avatar/discourse.pi-hole.net/yubiuser/32/12100_2.png) [@yubiuser](https://discourse.pi-hole.net/u/yubiuser)\
**Post date:** [June 29, 2023, 7:14am UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/15 "2023-06-29T07:14:55Z")

</div>

That's unexpected as I only formatted the timestamp output to be human-readable and grouped by date...

---

<div class="post-metadata">

**Author:** ![system](https://discourse.pi-hole.net/uploads/default/original/3X/7/c/7c8792f649eeb921c5d2b4c41564ffa873d9a2b8.png) [@system](https://discourse.pi-hole.net/u/system)\
**Post date:** [July 6, 2023, 4:23pm UTC](https://discourse.pi-hole.net/t/basic-sql-query-question/63566/21 "2023-07-06T16:23:37Z")

</div>

Closed on July 6, 2023, 4:23pm UTC

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