# Coalesce in query with timeGroupAlias and timeFilter

**URL:** <https://community.grafana.com/t/coalesce-in-query-with-timegroupalias-and-timefilter/17439>\
**Category:** Grafana\
**Created:** [May 20, 2019, 1:56am UTC](https://community.grafana.com/t/coalesce-in-query-with-timegroupalias-and-timefilter/17439 "2019-05-20T01:56:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vicnickw](https://avatars.discourse-cdn.com/v4/letter/v/48db29/32.png) [@vicnickw](https://community.grafana.com/u/vicnickw)\
**Post date:** [May 20, 2019, 1:56am UTC](https://community.grafana.com/t/coalesce-in-query-with-timegroupalias-and-timefilter/17439/1 "2019-05-20T01:56:03Z")

</div>

I’m trying to query from a Postgres table and display resulting metrics in a Graph panel.

The Postgres table has log entries with two date columns per row: `CreationDate` and `LastLogDate`. `CreationDate` always has the first logged date, and `LastLogDate` has the date of the last occurrence of the same log type.

With the Grafana query builder, I get the following query:

```auto
SELECT
  $__timeGroupAlias("CreationDate",$__interval,NULL),
  "ApplicationName" AS metric,
  max("DuplicateCount")
FROM "Errors"
WHERE
  $__timeFilter("CreationDate")
GROUP BY 1,2
ORDER BY 1,2

```

It works fine, except that I want to group & filter by `LastLogDate` if it’s not null, instead of `CreationDate`, but I’m not sure how to specify a coalesce (take first non-null value) function along with `$ __timeGroupAlias` and `$__ timeFilter` functions. It this possible somehow?
