oracle11g - Oracle SQL - SUM total of similar rows while only displaying first occurrence of each -


the title little tricky here accomplish. using oracle sql developer, 11g express. have table users enter data on period of time. able pull data country_code , date range. here table (return_statistics):

return_id   country_code  code    description   return total   date 1           1             0001    example 1      1             12-aug-2012 2           1             0002    example 2      1             13-aug-2012 3           1             0003    example 3      1             14-aug-2012 4           1             0004    example 4      1             13-aug-2012 5           1             0005    example 5      1             11-aug-2012 6           1             0006    example 6      1             13-aug-2012 7           2             0001    example 1      1             12-aug-2012 8           2             0002    example 2      1             13-aug-2012 9           2             0003    example 3      1             14-aug-2012 10          2             0004    example 4      1             13-aug-2012 11          2             0005    example 5      1             11-aug-2012 12          2             0006    example 6      1             13-aug-2012 ...        ...            ....    ......         ..            ...... 300         1             0004    example 4      1             10-aug-2013 500         1             0001    example 1      6             12-aug-2014 ....        ...           .....   .......        ...           ..... 

i want query country_code , date range , return 1 occurrence each code overall return_total code displayed. here want output country_code = 1, date range aug 9, 2012 - aug 15, 2014.

code      description                                        return_total --------- -------------------------------------------------- ------------ 0001      example 1                                               7 0002      example 2                                               1  0003      example 3                                               1  0004      example 4                                               2  0005      example 5                                               1       0006      example 6                                               1  

notice example 1 7 (6+1) , example 4 2 (1+1). here sql tried , result. know wrong sql don't know how fix it.

select distinct     code,     description,  (select sum(return_total) return_statistics            country_code = 1 , date between to_date ('08/09/2012 10:10:00', 'mm/dd/yyyy hh:mi:ss') , to_date ('08/15/2014 12:10:00', 'mm/dd/yyyy hh:mi:ss') , code = 0001) return_total return_statistics   country_code = 1 order code 

result:

code      description                                        return_total --------- -------------------------------------------------- ------------ 0001      example 1                                               7 0002      example 2                                               7  0003      example 3                                               7  0004      example 4                                               7  0005      example 5                                               7       0006      example 6                                               7 

is there way total each code? know i'm setting 0001 there way make use each code reference return_total? perhaps table join, still don't know how correctly? please help! thank you!

analytic functions go long way here. can use row_number() on (... order newid()) pick random description:

select  *    (         select  row_number() on (                     partition code                     order newid()) rn         ,       sum(return_total) on (partition code) return_total         ,       code         ,       description            return_statistics         ) subqueryalias   rn = 1 -- 1 row per code 

Comments

Popular posts from this blog

java - How to specify maven bin in eclipse maven plugin? -

single sign on - Logging into Plone site with credentials passed through HTTP -

php - Why does AJAX not process login form? -