| Grouping with sum and retain first row [message #645337] |
Thu, 03 December 2015 13:50  |
 |
bamboo123
Messages: 1 Registered: December 2015
|
Junior Member |
|
|
Hi,
I have this table:
COMPANY LOT GROUP_CODE LOCATION HOURS
ab1 1 xt mars 2
ab2 2 xt mars 3
ab3 3 xt mars 4
db1 1 ls earth 1
db2 2 ls earth 1
db3 3 ls earth 5
I want to group by group_code and get the first row with sum(hours) like below:
COMPANY LOT GROUP_CODE LOCATION HOURS
ab1 1 xt mars 9
db1 1 ls earth 7
I tried the code below, but couldn't get the sumary to work. Thanks,
select company,lot,group_code,location,sum(hours) from (
SELECT company
,lot
,group_code
,hours
,location
,ROW_NUMBER () OVER (PARTITION BY group_code ORDER BY company) AS rk
FROM ad
) where rk = 1 group by company, lot, group_code,location
|
|
|
|
|
|
|
|