subquery with group by issue [message #649687] |
Mon, 04 April 2016 02:15 ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
![](http://www.gravatar.com/avatar/ba15a4e5d9b4e67dd5abe118387e9c99?s=64&d=mm&r=g) |
rlicata
Messages: 4 Registered: April 2016
|
Junior Member |
|
|
Hello All,
as a newbie, I would like to execute a query based on 1 table : T_XMON_ALARMS
CREATE TABLE "XXXX"."T_XMON_ALARMS"
( "ID" NUMBER NOT NULL ENABLE,
"ALARM_KEY" VARCHAR2(50 BYTE) NOT NULL ENABLE,
...
"LAST_UPDATED_AT" TIMESTAMP (6),
"STATUS" NUMBER DEFAULT 0 NOT NULL ENABLE,
CONSTRAINT "T_XMON_ALARMS_PK" PRIMARY KEY ("ID")
DATA
ID ALARM_KEY SEVERITY LAST_UPDATED_AT
1 A001 1 30-MAR-16 09.08.51.421000000 AM
2 A001 2 07-MAR-16 09.08.59.707000000 AM
3 A002 3 16-MAR-16 09.09.04.730000000 AM
4 A002 1 12-MAR-16 09.09.12.446000000 AM
6 A002 2 31-MAR-16 09.06.43.229000000 AM
7 A004 2 31-MAR-16 09.21.34.638000000 AM
8 AA003 1 16-MAR-16 09.09.12.446000000 AM
9 AA003 1 14-MAR-16 09.09.12.446000000 AM
10 A000 1 16-MAR-16 09.09.12.446000000 AM
11 AA003 1 16-MAR-16 09.09.12.446000000 AM
5 A001 2 30-MAR-16 09.09.19.700000000 AM
the query must returns the ALARM_KEY and a counter but ordered by the LAST_UPDATED_AT DESC. (get the newest alarm as first row)
So I tried the following query but the order is not correct.
SELECT ALARM_KEY, COUNT(*) AS CPT FROM (SELECT * FROM T_XMON_ALARMS WHERE STATUS = 0 ORDER BY LAST_UPDATED_AT DESC)
GROUP BY ALARM_KEY;
I would like to get :
A004 1
A002 3
A001 3
Someone can help?
Thanks & regards
Raph
[mod-edit: code tags added by bb]
[Updated on: Mon, 04 April 2016 14:41] by Moderator Report message to a moderator
|
|
|
|
|
Re: subquery with group by issue [message #649690 is a reply to message #649688] |
Mon, 04 April 2016 02:41 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
cookiemonster
Messages: 13925 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
John Watson wrote on Mon, 04 April 2016 08:18You cannot ORDER BY a column that you are not projecting.
Generally speaking you can order by a column you're not selecting, but not if you're using group by like the OP is.
|
|
|
|
|
Re: subquery with group by issue [message #649693 is a reply to message #649690] |
Mon, 04 April 2016 02:49 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
John Watson
Messages: 8938 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
cookiemonster wrote on Mon, 04 April 2016 08:41John Watson wrote on Mon, 04 April 2016 08:18You cannot ORDER BY a column that you are not projecting.
Generally speaking you can order by a column you're not selecting, but not if you're using group by like the OP is.
So you can! It had never occurred to me that one can do that. Looking at the execution plan details, the sort column does in fact get projected along with the named columns. That makes sense when one considers the basic SQL execution cycle:
1. select the rows
2. do the aggregation
3. project the columns
4. perform the sort
|
|
|
|
Re: subquery with group by issue [message #649696 is a reply to message #649694] |
Mon, 04 April 2016 03:56 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
cookiemonster
Messages: 13925 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Good to know.
@rlicata - since there are multiple values of last_updated_at per alarm key you're going to need an inner query that gets the max(last_updated_at) for each alarm_key along with the count then an outer query that selects alarm_key and count and orders by the max(last_updated_at).
[Updated on: Mon, 04 April 2016 03:56] Report message to a moderator
|
|
|
|
|
Re: subquery with group by issue [message #649699 is a reply to message #649697] |
Mon, 04 April 2016 04:19 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
John Watson
Messages: 8938 Registered: January 2010 Location: Global Village
|
Senior Member |
|
|
That is not a correct solution. I think it relies on Cost Based Optimizer choosing to use a nested loop join with your tab2 as the outer row set. If it happens to choose, for example, a sort merge join the final ordering will be on alarm_key, not lastupdated. Hint it, and you will see.
|
|
|
Re: subquery with group by issue [message #649700 is a reply to message #649699] |
Mon, 04 April 2016 05:13 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) ![Go to next message Go to next message](/forum/theme/orafaq/images/down.png) |
cookiemonster
Messages: 13925 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
I was thinking more like this:
SELECT alerm_key, countx
FROM (SELECT alarm_key, max(last_updated_at) AS lastUpdated, count(*) AS countx
FROM t_xmon_alarms
WHERE status = 0
GROUP BY alarm_key
)
ORDER BY lastUpdated DESC;
|
|
|
|
|
Re: subquery with group by issue [message #649711 is a reply to message #649700] |
Mon, 04 April 2016 09:09 ![Go to previous message Go to previous message](/forum/theme/orafaq/images/up.png) |
Solomon Yakobson
Messages: 3275 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
No need for in-line view. ORDER BY accepts aggredations:
SELECT alarm_key,
count(*) AS countx
FROM t_xmon_alarms
WHERE status = 0
GROUP BY alarm_key
ORDER BY max(last_updated_at) DESC;
For example:
SQL> select deptno,
2 count(*) cnt
3 from emp
4 group by deptno
5 order by max(hiredate) desc
6 /
DEPTNO CNT
---------- ----------
20 5
10 3
30 6
SQL>
SY.
|
|
|