• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4375
  • Last Modified:

ORACLE SQL - to get the first_value and the last_value using OVER PARTITION BY

Hi Folks,

I have a table cnam_load which is Oracle table and the columns of the table are :-

mdn
action_type

I have to use the over partition by command to get the first value and the last value of the action_type and it should be grouped by mdn and count(*) > 1. I never used over partition by command and I am not sure how to build this query. I would appreciate your advice.

Thanks
0
pagesheetal
Asked:
pagesheetal
  • 2
1 Solution
 
sdstuberCommented:
I'm assuming first and last are determined by order of the action type itself.

select * from
(select mdn, first_value(action_type) over (partition by mdn order by action_type) first_action,
last_value(action_type) over(partition by mdn over action_type) last_action,
count(*) over (partition by mdn) cnt
from cnam_load
) where cnt > 1
0
 
sdstuberCommented:
that will  give you duplicate rows, one row for each action type of each mdn,
so just add distinct

select distinct mdn, first_action,last_action, cnt from (select mdn, first_value(action_type) over (partition by mdn order by action_type) first_action,
last_value(action_type) over(partition by mdn over action_type) last_action,
count(*) over (partition by mdn) cnt
from cnam_load
) where cnt > 1
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now