Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

SQL and Group by

582588Aug 27 2008 — edited Aug 27 2008
Hi all,

I have a query
SELECT schedule_id ,COLUMN_VALUE,player_team_coach,current_player_team_id
FROM stat_totals where SCHEDULE_ID = '20040809NFL--WASHINGTON0' AND COLUMN_ID ='FPLKICK_PR_YARD';

this returns me
schedule_id column_value player_team_coach team_id
20040809NFL--WASHINGTON0 11 13373 8831
20040809NFL--WASHINGTON0 15 1953480 8831
20040809NFL--WASHINGTON0 46 2026423 8810

for a given schedule there are only two teams playing.whenever team_id = 8831, i need 8810 column_values which is equal to 46
and whenever team_id = 8810 , i have to sum up all the column_values of 8831 which is 11+ 15=26 .
My output must look like the one below.
schedule_id sum( column_value) player_team_coach
8831 46 13373
8831 46 1953480
8810 26 2026423
This post has been answered by Satyaki_De on Aug 27 2008
Jump to Answer

Comments

Satyaki_De
I'm not sure about your requirement. But, at least you can provide your table structure along with some sample test data so that we can try your expected output.

Regards.

Satyaki De.
582588
Hi,
I have got three tables named stat_totals,schedules, teams
stat_totals has the following columns schedule_id,stat_type_id ,column_id, player_team_coach(player_id),league_id,column_value,current_player_team_id
schedules has the following columns schedule_id,schedule_date,home_team_id,visitor_team_id,league_id,season_id
teams has the following columns team_id,team_name,location_id,division_id,league_id

select player_team_coach,current_player_team_id current_team_id,HOME_TEAM_ID,VISITOR_TEAM_ID,c.team_id,
SUM(to_number(column_value, '9999')) pr_yard,
0 avg_yds_unit from stat_totals a, schedules b, teams c
where a.schedule_id = b.schedule_id and a.stat_type_id = 'PPG'
and a.column_id = 'FPLKICK_PR_YARD' and b.league_id = 'NFL'
and a.SCHEDULE_ID = '20040809NFL--WASHINGTON0'
-- and b.schedule_date between '01-DEC-2007' and '31-DEC-2007'
and b.game_status_id = 143
and a.current_player_team_id in (
select CASE WHEN a.current_player_team_id = HOME_TEAM_ID THEN visitor_team_id
when a.current_player_team_id = visitor_team_id then HOME_TEAM_ID
else 0 end team_id from stat_totals a, schedules b, teams c
where a.schedule_id = b.schedule_id and a.stat_type_id = 'PPG'
and a.column_id = 'FPLKICK_PR_YARD' and b.league_id = 'NFL'
and a.SCHEDULE_ID = '20040809NFL--WASHINGTON0'
-- and b.schedule_date between '01-DEC-2007' and '31-DEC-2007'
and b.game_status_id = 143 and a.current_player_team_id = c.team_id
)
and a.current_player_team_id = c.team_id -- and c.primary_conf_id = 5
group by player_team_coach, current_player_team_id,HOME_TEAM_ID,VISITOR_TEAM_ID,c.team_id





player_id current_team_id home_team_id visitor_team_id c.team_id column_value
*13373 8831 8831 8810 8831 11*


*2026423 8810 8831 8810 8810 46*


*1953480 8831 8831 8810 8831 15*

whenever current_player_team_id = home_team_id then i need to total the opposing teams column_values
in the above example for current_team_id = 8831 ,the opposing team is 8810, so we need to sum up all the column_nalues of 8810 which is equivalent to 46.
similarly for current_team_id = 8810 the opposing team is 8831 , so we need to sum up all the column_nalues of 8831 which is equivalent to 26.

My output has to look like this
player_id current_team_id home_team_id visitor_team_id c.team_id column_value
*13373 8831 8831 8810 8831 46*


*2026423 8810 8831 8810 8810 26*


*1953480 8831 8831 8810 8831 46*
Frank Kulash

Hi,

Here's one way:

WITH  oq  AS
(   -- Begin sub-query oq: original query
    SELECT schedule_id ,COLUMN_VALUE,player_team_coach,current_player_team_id 
    FROM stat_totals where SCHEDULE_ID = '20040809NFL--WASHINGTON0' AND COLUMN_ID ='FPLKICK_PR_YARD';
)   -- End sub-query oq: original query
SELECT  team_id
,       (   -- Begin scalar sub-query
            SELECT  SUM (column_value)
            FROM    oq
            WHERE   team_id  != m.team_id
        )   AS sum_column_value      -- End scalar sub-query
,       player_team_coach
FROM    oq      m       -- m for main
;
Satyaki_De
Answer
Here is the sample code that you can try -
satyaki>
satyaki>select * from v$version;

BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - Prod
PL/SQL Release 10.2.0.3.0 - Production
CORE    10.2.0.3.0      Production
TNS for 32-bit Windows: Version 10.2.0.3.0 - Production
NLSRTL Version 10.2.0.3.0 - Production

Elapsed: 00:00:00.10
satyaki>
satyaki>
satyaki>with stat_totals
  2  as
  3    (
  4      select '20040809NFL--WASHINGTON0' schedule_id, 11 column_value, 13373 player_team_coach, 8831 team_id from dual
  5      union all
  6      select '20040809NFL--WASHINGTON0', 15, 1953480, 8831 from dual
  7      union all
  8      select '20040809NFL--WASHINGTON0', 46, 2026423, 8810 from dual
  9    )
 10  select team_id,
 11         sumX - sumR sumN,
 12         player_team_coach
 13  from (  
 14          select team_id,
 15                sum(column_value) over(partition by team_id) sumR,
 16                sum(column_value) over() sumX,
 17                player_team_coach
 18          from stat_totals
 19       );

   TEAM_ID       SUMN PLAYER_TEAM_COACH
---------- ---------- -----------------
      8810         26           2026423
      8831         46             13373
      8831         46           1953480

Elapsed: 00:00:00.01
satyaki>
satyaki>
Regards.

Satyaki De.

N.B.: My query is based on the first post by o/p

Edited by: Satyaki_De on Aug 28, 2008 1:30 AM
Marked as Answer by 582588 · Sep 27 2020
Satyaki_De
Oops!

I skipped this part ->
SELECT schedule_id ,COLUMN_VALUE,player_team_coach,current_player_team_id
FROM stat_totals where SCHEDULE_ID = '20040809NFL--WASHINGTON0' AND COLUMN_ID ='FPLKICK_PR_YARD';
I need to sleep.

Perhaps Frank has given you the right solution.

Regards.

Satyaki De.
582588
Got the problem solved, been very quick in replying .

Thanks
Sneha
656883
Hi,

Are analytical functions permitted?

create table user579585 (
player_id number,
current_team_id number,
home_team_id number,
visitor_team_id number,
team_id number,
column_value number
);

insert into user579585 values (13373, 8831, 8831, 8810, 8831, 11);
insert into user579585 values (2026423, 8810, 8831, 8810, 8810, 46);
insert into user579585 values (1953480, 8831, 8831, 8810, 8831, 15);
commit;

select b.*,
sum(column_value) over (partition by home_team_id) -
sum(column_value) over (partition by current_team_id) aa
from user579585 b;


Regards,

Melvin
1 - 7
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Sep 24 2008
Added on Aug 27 2008
7 comments
225 views