Im trying to show a Count of the tickets responded by my team and somehow in the codeing it is not just showing of the last "updated_on" by that person, sometime it shows more than an "updated_on" or other cases it shows the "updated_on" but the ticket was not solve trully by him.
My final goal with this was just a dashboard in grafana here it shows all the tickets each member of my team has responded.
My Code:
Select vgtuser, count(*)
From (
SELECT DISTINCT ON(a.ticket_id) a.ticket_id, max(a.updated_on), b.name as vgtuser
FROM ticket_messages a
INNER JOIN admins b ON a.admin_id=b.admin_id
INNER JOIN ticket_status_history c ON a.ticket_id=c.ticket_id
WHERE c.status_id IN(50, 60, 65) AND a.updated_by LIKE 'Ana Monteiro' or a.updated_by LIKE 'Nuno Gonçalves' or a.updated_by LIKE 'Henrique Espinha' or a.updated_by LIKE 'Ricardo Sousa' or a.updated_by LIKE 'João Fernandes' or a.updated_by LIKE 'Pedro Pereira' or a.updated_by LIKE 'Luis Moreno' or a.updated_by LIKE 'Gonçalo Rodrigues' or a.updated_by LIKE 'Nuno Coelho'
GROUP BY a.ticket_id, b.name
) as media
group by 1
ORDER BY 2 DESC
Try this Altered Query :
Select vgtuser,max(updt), count(*)
From (
SELECT DISTINCT ON(a.updated_on) a.ticket_id, max(a.updated_on) as updt, b.name as vgtuser
FROM ticket_messages a
INNER JOIN admins b ON a.admin_id=b.admin_id
INNER JOIN ticket_status_history c ON a.ticket_id=c.ticket_id
WHERE c.status_id IN(50, 60, 65) AND (
a.updated_by LIKE 'Ana Monteiro' or
a.updated_by LIKE 'Nuno Gonçalves' or
a.updated_by LIKE 'Henrique Espinha' or
a.updated_by LIKE 'Ricardo Sousa' or
a.updated_by LIKE 'João Fernandes' or
a.updated_by LIKE 'Pedro Pereira' or
a.updated_by LIKE 'Luis Moreno' or
a.updated_by LIKE 'Gonçalo Rodrigues' or
a.updated_by LIKE 'Nuno Coelho')
GROUP BY a.ticket_id, b.name,a.updated_on
) as media
group by 1
ORDER BY 2 DESC
if you change the order at the end of the query you will not get the latest updated on , you will get the most updated by.
Related
I have a simple question about 'max' and 'count' functions.
I read some posts already but they don't satisfy me or i cannot understand them well enough to use them
I tried to refer to this
Here is my query:
select wojewodztwo, count(*) as liczba from wojewodztwa
inner join powiaty on wojewodztwa.klwoj = powiaty.klwoj
inner join gminy on powiaty.klpow = gminy.klpow
inner join miejscowosci on gminy.klgm = miejscowosci.klgm
where miejscowosc = 'Nowy Dwór'
group by wojewodztwo order by count(*) desc
And the result of this query looks like this
Now i want to get only max value rows as the answer. I don't want to use Limit 2 I thought of doing something like max(count) but it seems that aggregate functions cannot be stacked on one of eachother
Using a windowed function and a CTE will be quicker and more elegant:
WITH
T AS
(
SELECT wojewodztwo, COUNT(*) as liczba, MAX(COUNT(*)) OVER() AS MAX_liczba
FROM wojewodztwa
INNER JOIN powiaty
ON wojewodztwa.klwoj = powiaty.klwoj
INNER JOIN gminy
ON powiaty.klpow = gminy.klpow
INNER JOIN miejscowosci
ON gminy.klgm = miejscowosci.klgm
WHERE miejscowosc = 'Nowy Dwór'
GROUP BY wojewodztwo
)
SELECT wojewodztwo, liczba
FROM T
WHERE liczba = MAX_liczba;
It sounds like you want to put FETCH FIRST 1 ROW WITH TIES at the end of your query. This WITH TIES feature was introduced in v13.
Before that, I think you would need to wrap another query using window functions around your existing query (or do it on the client side). To use the window function, you would actually have to wrap it in two layers of select, as you can't use window functions in a where clause, so you need to define it on one level and use it on the outer level.
So it would look something like this:
select wojewodztwo, liczba from (
select wojewodztwo, liczba, max(liczba) over () as _max from (
select wojewodztwo, count(*) as liczba from wojewodztwa
inner join powiaty on wojewodztwa.klwoj = powiaty.klwoj
inner join gminy on powiaty.klpow = gminy.klpow
inner join miejscowosci on gminy.klgm = miejscowosci.klgm
where miejscowosc = 'Nowy Dwór'
group by wojewodztwo
) f
) ff where _max=liczba
I had a query similar to the following and was wondering that DB2 complained about the correlation use in the ORDER BY clause. It errored with something like
[42703][-206] "A.ID" is not valid in the context where it is used..
SQLCODE=-206, SQLSTATE=42703
I was able to rewrite the query to avoid the correlation usage but I couldn't find a reference in the documenation about this. Is this a bug or am I just not able to find details on the expected behavior?
SELECT a.id
FROM A a
ORDER BY (
SELECT COUNT(*)
FROM B b
WHERE b.id = a.id
)
You can't use correlated query in order by clause. However there is many ways to get same result, for example
select count(*) as count_num ,a.ID
from
a join b on a.ID=b.ID
GROUP BY a.ID
order by 1 DESC
solution 1:
SELECT a.id, (select count(*) from B where B.id=a.id) nbOFB
FROM A
order by 2
solution 2:
select * from (
SELECT a.id, (select count(*) from B where B.id=a.id) nbOFB
FROM A
) tmp
order by nbOFB
Solution 3:
SELECT a.id, c.nb
FROM A
inner join lateral
(
select count(*) nb from B where B.id=a.id
) c on 1=1
order by c.nb
Solution 4 :
SELECT a.id, ifnull(c.nb, 0) nb
FROM A
left outer join
(
select b.id, count(*) nb from B group by b.id
) c on a.id=c.id
order by ifnull(c.nb, 0)
Solution 5:
with c as (
select b.id, count(*) nb from B group by b.id
)
SELECT a.id, ifnull(c.nb, 0) nb
FROM A left outer join c on a.id=c.id
order by ifnull(c.nb, 0)
I have this SQL:
select * from `posts`
where `posts`.`deleted_at` is null
and `expire_at` >= '2017-03-26 21:23:42.000000'
and (
select count(distinct tags.id) from `tags`
inner join `post_tag` on `tags`.`id` = `post_tag`.`tag_id`
where `post_tag`.`post_id` = `posts`.`id`
and (`tags`.`tag` like 'PHP' or `tags`.`tag` like 'pop' or `tags`.`tag` like 'UI')
) >= 1
Is it possible order the results by number of tags in posts?
Maybe add there alias?
Any information can help me.
Convert your correlated subquery into a join:
select p.*
from posts p
join (
select pt.post_id,
count(distinct t.id) as tag_count
from tags t
inner join post_tag pt on t.id = pt.tag_id
where t.tag in ('PHP', 'pop', 'UI')
group by pt.post_id
) pt on p.id = pt.post_id
where p.deleted_at is null
and p.expire_at >= '2017-03-26 21:23:42.000000'
order by pt.tag_count desc;
Also, note that I changed the bunch of like and or to single IN because you are not matching any pattern i.e. there is no % in the string. So, better using single IN instead.
Also, if you have defined your table names, column names etc keeping keywords etc in mind, you shouldn't have the need to use the backticks. They make reading a query difficult.
I need to identify any objects in a database containing "scenarioID" that has hard coded value.
I am looking to identify following cases:
scenarioId = XX (two digit value)
scenarioId=XX (two digit value)
scenarioId= XX (two digit value)
Below query I wrote seems to be pulling objects containing "scenarioid", but is giving me more than above cases.
SELECT DISTINCT a.[name]
FROM sysobjects a
INNER JOIN syscomments b on a.id = b.id
WHERE b.[text] LIKE '%scenarioId = __[^0-9]%'
I realized my mistake. I was using [^0-9] instead of using [0-9].
SELECT DISTINCT a.[name]
FROM sysobjects a
INNER JOIN syscomments b on a.id = b.id
WHERE b.[text] LIKE '%scenarioId = [0-9][0-9]%'
or b.[text] like '%scenarioId=[0-9][0-9]%'
or b.[text] like '%scenarioId =[0-9][0-9]%'
or b.[text] like '%scenarioId = [0-9][0-9]%'
or b.[text] like '%scenarioId = [0-9][0-9]%'
I have a query similar to this simplified example:
select u.id, sq.score
from usr as u,
(select user_id, score FROM score WHERE bar = ?) as sq
where u.id = sq.user_id
I would like the join (u.id = sq.user_id) to be an outer join. I can't figure out how to use JOIN in the from clause with a 'select' like this.
I know I could do this example without having to use a select in the from clause but thats not what I need in my application.
Something like this:
select u.id, sq.score
from usr as u
left join ( -- or right/full join as per your needs
select user_id, score FROM score WHERE bar = ?
) as sq
on u.id = sq.user_id