Select by Sum
Hi all,
I am attempting to query back records based on whether the sum of a collumn is above 600 but get a "ORA-00934 group function is not allowed here" error. I have a sneaky feeling its because of the join Im using but I'm a beginner so not sure?
Any assistance would be greatly appreciated.
This is the query I am using:-
Regards
LJE
SELECT STAFF.PID,PERSON.FIRSTNAME,PERSON.LASTNAME,RATING_RECORD.RATE
FROM
STAFF INNER JOIN
CLIENT ON STAFF.PID = CLIENT.PID INNER JOIN
PERSON ON PERSON.PID = STAFF.PID AND PERSON.PID = CLIENT.PID INNER JOIN
INSURED_BY ON CLIENT.CID = INSURED_BY.CID INNER JOIN
POLICY ON POLICY.PNO = INSURED_BY.PNO INNER JOIN
COVERAGE ON POLICY.PNO = COVERAGE.PNO INNER JOIN
RATING_RECORD ON COVERAGE.COID = RATING_RECORD.COID
WHERE
SUM(RATING_RECORD.RATE) < 600
You need to aggregate the fields then use the HAVING clause, ie;
select staff.pid, person.firstname, person.lastname, sum(rating_record.rate)
from staff inner join client on staff.pid = client.pid
inner join person on person.pid = staff.pid and person.pid = client.pid
inner join insured_by on client.cid = insured_by.cid
inner join policy on policy.pno = insured_by.pno
inner join coverage on policy.pno = coverage.pno
inner join rating_record on coverage.coid = rating_record.coid
group by staff.pid, person.firstname, person.lastname
having sum (rating_record.rate) < 600
Similar Messages
-
Hi,
I am solving much more complex problem but I am stuck on a simple issue:
I am getting ORA-00936 missing expression all the time in this sql:
SELECT (SUM(SELECT my_function (table.column) from dual)) AS my_sum, table.anothercolumn FROM table
I get the very same thing if I just do something as simple as:
SELECT id, (SUM(SELECT 1+1 from dual) as my_sum from dual) FROAM table
What is wrong here?! I know it is something with the sum and the select following it but what is it??? Please it drives me nuts :)
Thank you very much
Edited by: koszta5 on Nov 11, 2010 5:15 AMMy scenario is kinda crazy - we have a server with working Core app and a devel server with new Core version. The new Core also works with new db model. Now the new Core is NO WAY finished but there is some functionality that somebody did as a project that allows neat exports from new Core. My task is to "reprogram" this export in a way that code doesnt change - I basically need to make it think it works with new Core (not the old one) and new db model (but in fact it works with the old db model) ... Here is the final SQL that worked for me (yeah I know it is slow but what can you do concerning the given task)
select sezeni, sum ((select format_binary_odpoved(O_moznosti_otazky.poradi) from dual)) as odpovedi, O_SEZNAM_OTAZEK.id from O_SEZNAM_OTAZEK_MATRIX inner join O_MOZNOSTI_OTAZKY on O_SEZNAM_OTAZEK_MATRIX.otazka= O_MOZNOSTI_OTAZKY.OTAZKA inner join O_ODPOVEDI on O_ODPOVEDI.MATRIX=O_SEZNAM_OTAZEK_MATRIX.id inner join O_SEZNAM_OTAZEK on O_SEZNAM_OTAZEK.id=O_MOZNOSTI_OTAZKY.otazka where arch=1010 and rownum<100 group by sezeni,O_seznam_otazek.id order by O_SEZNAM_OTAZEK.id,sezeni
It is still not complete (will have to add column1+column2 ) but that is the last thing... after that it should do what I want it to :) -
hi
i have to fo select from table in data base (ztrm_1)that have field like
(p01 p02 p03 p04 p05 p06 p07 p08 p09 p10 p11 p12)
and i have to sum all content of this field into one field (sum_last)
what is the <b>best</b> way to do that?
i reward
RegardsHello tal_s
data : it_ztrm_1 type table of ztrm_1,
p0 type ztrm_1-p01
selct * from ztrm_1 into table it_ztrm_1.
loop at it_ztrm_1 into wa_ztrm_1
do 12 times varying p0 FROM wa_ztrm_1-p01 NEXT wa_ztrm_1-p02.
w_sum = w_sum + p0
enddo.
append w_sum to it_sum " if multiple records.
endloop.
if you have single record then no need of outer loop directly use do enddo statement on your wrk area
hope p01 p02 ,p03 ... are of same type & length.
this should solve your problem
reward points if helpful -
How to select and sum internal table records
Dear Friends
I kindly ask you if we have select statement
if s_mtart = 'z003'
select single pvprs from ckmlcr into ckmlcr-pvprs
where poper EQ s_poper and
kalnr = itab2-kalnr and
bdatj = itab2-bdatj and
curtp = itab2-curtp.
like this how can I calculate how many record it got and I want to get summation of this field(pvprs).And for all poper's must contain.
Please Let me remind you my itab is already open I didn't put any thing for this situationit seems to be you written this SELECT in a loop. if so,
instead of pushing the values into ckmlcr-pvprs ,create an internal table
data : begin of itab,
pvprs type ckmlcr-pvprs ,
end of itab.
then just after that SELECT SINGLE,
select single pvprs from ckmlcr <b>into ITAB-pvprs</b>
where poper EQ s_poper and
kalnr = itab2-kalnr and
bdatj = itab2-bdatj and
curtp = itab2-curtp.
IF SY-SUBRC = 0.
APPEND ITAB.
here either you can use APPEND OR COLLECT.
If you use COLLECT,all the values will get summed up and final sum will be in the table ITAB-pvprs.
ENDIF.
After all loops your itab will have the totals.
DESCRIBE TABLE ITAB LINES V_LINES.
V_LINES Will have total no of lines.
Regards
srikanth -
How can I select count/sum/avg by sliding window ???
Hi, Can you help me somebody?
For example I have table with DateTime field. And now, I want to find which 1 hour interval contains maximum of records. Resolution is 1 minute. This mean that there is 1380 intervals in one day (00:00-01:00, 00:01-01:01, ... ... 22:59-23:59,23:00-00:00)
There is problem using analytic functions with window clausule because in table there is not record for every minute.
How can I do it?
PS: Sorry my poor englishStill not very clear, but this time my guess is you mean this:
SQL> create table mytable
2 as
3 select date '2007-07-18' + round(dbms_random.value(0,1440))/1440 mydate
4 from dual
5 connect by level <= 2000
6 /
Tabel is aangemaakt.
SQL> with all_minutes as
2 ( select date '2007-07-18' + (level-1)/1440 mytime
3 from dual
4 connect by level <= 1440
5 )
6 select a.mytime
7 , sum(count(t.mydate)) over (order by mytime range between current row and interval '1' hour following) number_of_records
8 from all_minutes a
9 , mytable t
10 where a.mytime = t.mydate(+)
11 group by a.mytime
12 /
MYTIME NUMBER_OF_RECORDS
18-07-2007 00:00:00 93
18-07-2007 00:01:00 94
18-07-2007 00:02:00 92
18-07-2007 00:03:00 91
18-07-2007 00:04:00 92
18-07-2007 00:05:00 90
18-07-2007 00:06:00 89
18-07-2007 23:32:00 36
18-07-2007 23:33:00 32
18-07-2007 23:34:00 32
18-07-2007 23:35:00 32
18-07-2007 23:36:00 32
18-07-2007 23:37:00 30
18-07-2007 23:38:00 29
18-07-2007 23:39:00 29
18-07-2007 23:40:00 25
18-07-2007 23:41:00 25
18-07-2007 23:42:00 23
18-07-2007 23:43:00 22
18-07-2007 23:44:00 20
18-07-2007 23:45:00 20
18-07-2007 23:46:00 19
18-07-2007 23:47:00 18
18-07-2007 23:48:00 18
18-07-2007 23:49:00 17
18-07-2007 23:50:00 15
18-07-2007 23:51:00 15
18-07-2007 23:52:00 14
18-07-2007 23:53:00 14
18-07-2007 23:54:00 14
18-07-2007 23:55:00 10
18-07-2007 23:56:00 7
18-07-2007 23:57:00 5
18-07-2007 23:58:00 3
18-07-2007 23:59:00 1
1440 rijen zijn geselecteerd.Regards,
Rob. -
Oracle 11g SUM function on the result of inner Select
The inner Select returns rows with the values. The outer select running on tthe result of inner select SUM the result of the inner select.
Although there are valid rows of the inner select the SUM in the outer SELECT return NVL. As a comment any aggregate function on the outer select returns NVL.
The query is below.
select a.yil,
a.ay,
sum(brut_prim),
sum(kom),
from (SELECT oid_pol_kesilen_acente,
to_char(pp.zeyil_tanzim_tar, 'yyyy') yil,
to_char(pp.zeyil_tanzim_tar, 'mm') ay,
sum(decode(substr(pp.no, 1, 1),
'T',
pp.zeyil_brut_prim_fark / 2,
pp.zeyil_brut_prim_fark)) -
sum(pp.zeyil_gv_fark) brut_prim,
sum(pp.zeyil_acente_kom_fark) kom
from pol_police pp, org_acente oa
WHERE SUBSTR(PP.NO, 1, 1) != 'D')
group by oid_pol_kesilen_acente,
to_char(pp.zeyil_tanzim_tar, 'yyyy'),
to_char(pp.zeyil_tanzim_tar, 'mm')) a
group by a.yil, a.ay, oid_pol_kesilen_acente
Edited by: [email protected] on May 7, 2010 2:09 AM
Edited by: [email protected] on May 7, 2010 2:31 AMHi,
please try the below sql:
select a.yil, a.ay, sum(brut_prim), sum(kom),
from (SELECT oid_pol_kesilen_acente,to_char(pp.zeyil_tanzim_tar, 'yyyy') yil, to_char(pp.zeyil_tanzim_tar, 'mm') ay,
sum(decode(substr(pp.no, 1, 1), 'T',nvl(pp.zeyil_brut_prim_fark,0) / 2,nvl(pp.zeyil_brut_prim_fark,0))) - sum(nvl(pp.zeyil_gv_fark,0)) brut_prim,
sum(nvl(pp.zeyil_acente_kom_fark,0)) kom
from pol_police pp, org_acente oa
WHERE SUBSTR(PP.NO, 1, 1) != 'D'
group by oid_pol_kesilen_acente,
to_char(pp.zeyil_tanzim_tar, 'yyyy'),
to_char(pp.zeyil_tanzim_tar, 'mm')
) a
group by a.yil, a.ay, oid_pol_kesilen_acente ;
Thanks,
Siva -
Select statment using FOR ALL ENTRIES not allow to do sum,
Hi All,
SELECT DELIV_NUMB
SUM( GRS_WGT_DL )
UNIT_OF_WT
FROM /BIC/AGSSD000600
INTO TABLE I_GROSS
FOR ALL ENTRIES IN I_LAYTWO
WHERE DELIV_NUMB = I_LAYTWO-DELIV_NUMB
GROUP BY DELIV_NUMB UNIT_OF_WT.
While compiling it shows the error
The addition "FOR ALL ENTRIES" excludes all aggregate functions with
the exception of "COUNT( * )", as the single element of the SELECT
clause.
Please give some solution to do sum
with regards,
Thambe .You could just select all entries into an internal table and then loop through and use COLLECT or similar to get your sum values.
Gareth. -
SUM of "SELECT originator, COUNT(*) as Inductions"
Hi,
I'm trying to SUM the results of "*SELECT originator, COUNT(*) as Inductions*" but am having trouble finding a solution?
Can this be done and cause it to create the SUM in a row below the created Inductions column?
Thanks in Advance.
James955600 wrote:
Hi,
I'm trying to SUM the results of "*SELECT originator, COUNT(*) as Inductions*" but am having trouble finding a solution?
Can this be done and cause it to create the SUM in a row below the created Inductions column?
Thanks in Advance.
James
17:33:30 SQL> select deptno, sum(sal) from emp group by deptno;
DEPTNO SUM(SAL)
30 9400
20 18650
10 8750
17:34:02 SQL> How do I ask a question on the forums?
SQL and PL/SQL FAQ -
How can I get this result without using 2 queries to sum buys and sells?
BANK TOTAL_SHARES
BANK1 30
BANK2 25
BANK4 20
BANK5 20
BANK3 15
Script to test
create table test (BUYER varchar2(5), SELLER varchar2(5), shares numeric(10));
insert into test values ('BANK1','BANK1',10);
insert into test values ('BANK1','BANK2',10);
insert into test values ('BANK3','BANK2',15);
insert into test values ('BANK4','BANK5',20);
commit;
select bank, sum(shares) total_shares from
select buyer bank, sum(shares) shares from test group by buyer
union
select seller bank, sum(shares) shares from test group by seller
group by bank
order by total_shares desc, bank;So .. you have 2 records for BANK1:
insert into test values ('BANK1','BANK1',10);
insert into test values ('BANK1','BANK2',10);
You want to add that first 10 , twice .. and the 2nd 10 once?
BANK1 30
So you want to double add some of those? -
"Select sum(field)" not summing
I have a PLSQL statement like:
select sum(field1) sum1,
sum (field2) sum2
into v_sum1,
v_sum2
where something = somethingElse;
How come my variable are being assigne a single row's (the first) field1 and field2 values rather than the actual sums?
If I do a SQLPLUS version of this (without the "into" part), I get the correct sums returned in a single row.
Thanks,I just tried your query and it should work if you only want a sum for a specific where clause. For example one employee's salary given the employee's id. If you want a list of salaries for every employee in a specific department, you need to put this into a cursor and loop through it.
This will look at all the rows based on the where statement and sum them all up. The reason it works in SQL*PLUS is that an implicit cursor is used (loops through automatically).
ex:
DECLARE
CURSOR C_SUM is
select ename,
sum(sal) sum1,
sum(comm)sum2
from emp
where deptno = 30;
BEGIN
FOR R_SUM IN C_SUM
LOOP
DBMS_OUTPUT.PUT_LINE(r_sum.ename ||' '||
r_sum.sum1 ||' '||
r_sum.sum2);
END LOOP;
END;
Make sure if you use DBMS_OUTPUT you have serveroutput on. Hope this helps. If it doesnt, post the actual code you are having problems with. -
Hi all,
I have an internal table consisting of three fields. I want to populate the internal table using a select statment for where I sum all records returned by material. Here is my query without the select:
SELECT PRINS PRLAB PREIN
INTO TABLE GT_MSPR_FIELDS
FROM MSPR
FOR ALL ENTRIES IN GT_SUPPLY_DEMAND
WHERE MATNR EQ GT_SUPPLY_DEMAND-MATNR
AND WERKS EQ GT_SUPPLY_DEMAND-WERKS
AND PSPNR IN S_PSPNR.
I tried adding a SUM statment to each field but the select poops. Is there a way to do this?
regards,
MatHi Bjorn,
I have an itab that includes matnr prins prlab prein
SELECT MATNR sum(PRINS) sum(PRLAB) sum(PREIN)
INTO TABLE GT_MSPR_FIELDS
FROM MSPR
FOR ALL ENTRIES IN GT_SUPPLY_DEMAND
WHERE MATNR EQ GT_SUPPLY_DEMAND-MATNR
AND WERKS EQ GT_SUPPLY_DEMAND-WERKS
AND PSPNR IN S_PSPNR.
that is what I had in mind....thanks much. -
Problem in sum a field through select command
Dear Sir(s),
I want to sum kbetr (no. of packing) for same knumv(invoice no.) in table konv.
e.g.
invoice no. no. of packing
(knumv) (kbetr)
001 2
001 3
002 4
002 5
result should be -
invoice no no. of packing
001 5
002 9
code-
select knumv kschl sum(distinct kbetr)
from konv
into table it_konv
for all entries in it_vbrk
where kschl = 'ZFQT'
and knumv = it_vbrk-knumv
group by knumv kbetr.
error showing - unkown col. sum(distinct field list.
Please help me immediately.
thanks,
R.KapoorHi..
Change ur Select statement as below .
<b>select knumv sum(kbetr)</b>
from konv
into table it_konv
for all entries in it_vbrk
where kschl = 'ZFQT'
and knumv = it_vbrk-knumv
<b>group by knumv .</b>
Here you need not to Retrieve the Field KSCHL since it is anyway given in the WHERE clause.
Let me know incase of any other issue.
<b>Reward if Helpful.</b> -
Cumulative sum column in SELECT
Is there a function in Orace which van cumulative sum a column
in a select?
Form example: a table with colomn X with the values 1,3,7,10
will return from select as 1,4,11,22?
Thanks in advance,
Tom.This is an interesting version of the Select statement. Could
you tell me what version of Oracle it is. I tried it at work
but I got an error message, ORA-00923: FROM keyword not found
where expected.
Thanks
SELECT X,
SUM(X) OVER
(ORDER BY ROWNUM RANGE BETWEEN ROWNUM PRECEDING AND CURRENT
ROW) cum_sum
FROM table_name
SURESH VEMULAPALLI -
my internal table it_mara conatins matnr and other fields.
I want to select matnr ,omeng from vbbe into another internal table.
i need sum of omeng field based on matnr .
i wrote like this
SELECT matnr sum( omeng ) FROM vbbe INTO TABLE it_vbbe
FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr .
but iam getting error.
tell me best way of writing the select statement.
thanks in advance.
Regards,
Sureshi dont think we cannot use SUM clause with for all entries.
instead,
SELECT matnr omeng FROM vbbe INTO TABLE it_vbbe
FOR ALL ENTRIES IN it_mara WHERE matnr = it_mara-matnr .
declare a same table like it_vbbe.
then
loop at it_vbbe.
move-corresponding it_vbbe to it.
collect it.
endloop.
now it will have the sum values..
there are some more methos to do this...above one is a ex. -
I need to choose between value1 and value2 within a select statement.
TableA
Flag (char)
Value1
Value2
ItemDesc
pseudo code
select ItemDesc, (if Flag = 'y' then Value1 else Value2) as Price from TableA;
How can this be done?select itemdesc,
sum (case
when flag = 'y'
then (case
when value1 < 10
then value1 * 1.2
else value1
end)
else value2
end
) as price
from tablea
group by itemdesc;
Maybe you are looking for
-
Can't seem to connect one of my ipads to our wireless network at home - every other device in the house is connected? any suggestions?
-
Hi Mates, Throught webi if i try to connect to a bex query im getting the attached error message, any idea what to do.. 2 days back im able to connect to bex query frm webi..frm yesterday its not happening regards, mahi
-
Error conneting to BI query in BO
Hi, Dashboard i have used sap net weaver connection ,it is showing input and output values then it is giving following error
-
How can I update ati radeon hd 5750 1024 mb on my mac os lion 10.7.5
How can I update my grafic card Ati Radeon 5750 1024 mb ? I recently updated my Aerofly 5 sim software , which now does not work because ,so I am told , the grafic card has to be updated ! Thanks for any help .
-
App broke over night (XML not parsing)
I had a nicely working app that used an external xml file as its data source. I woke up early this morning excited to be able to move on and add simple functionality to my app. But it when I reopened and compiled it, it had stopped parsing the xml. T