GROUP FUNCTION
Hi ,
How can i check if the sales is 0 for all last 4 days ?
Sales has got 2 columns SALES_DATE, ITEM, SALES_AMOUNT . I want to get all the items that have sold 0(zero) for last consecutive four days.
Thanks
Feroz
HERE IS A SAMPLE
1* select * from b order by 2 ,4,1 desc
SQL> /
DATES ITEM SALES STORES
21-JUL-03 1 100 3
20-JUL-03 1 0 6
19-JUL-03 1 0 6
18-JUL-03 1 0 6
17-JUL-03 1 0 6
21-JUL-03 2 100 3
20-JUL-03 2 0 3
19-JUL-03 2 0 3
18-JUL-03 2 10 3
17-JUL-03 2 0 3
21-JUL-03 2 0 4
DATES ITEM SALES STORES
20-JUL-03 2 0 4
19-JUL-03 2 0 4
18-JUL-03 2 0 4
17-JUL-03 2 0 4
21-JUL-03 2 100 5
20-JUL-03 2 0 5
19-JUL-03 2 0 5
18-JUL-03 2 0 5
17-JUL-03 2 0 5
20 rows selected.
SQL>
THE QUERY SHOULD RETURN ONLY THE ITEM 2 AND STORE NUMBER 4. COZ THE ITEM 4 HAS SOLD 0 ITEMS IN STORE 4 FOR LAST 4 CONSECUTIVE DAYS .
THANKS
Similar Messages
-
Error while using group function
Oracle forms6i
Hai
While i am compile my coding it compile successfully, but when i tried to executes i shows error in group function
my coding is
if (cnt<>0 ) then
select BARCODE,INTIME,OUTTIME into today_bar,today_in,today_out from dail_att where BARCODE= :Barcode
and ATTEND_DATE = :bardate;
update dail_att set outtime = max(:bartime) where barcode= :barcode
and ATTEND_DATE = :bardate;
else
if (cnt2<>0 ) then
select INTIME,OUTTIME into yest_in,yest_out from dail_att where BARCODE= :Barcode
and ATTEND_DATE = :bardate-1;
if(yest_in is not null and yest_out is null) then
update dail_att set outtime =max(:bartime) where barcode= :barcode
and ATTEND_DATE = :bardate-1;
else
insert into dail_att(barcode,intime,attend_date)
values(:barcode,min(:bartime),:bardate);
end if;
else
if :bartime between 0100 and 0630 then
insert into dail_att(barcode,intime,attend_date)
values(:barcode,min(:bartime),:bardate-1);
update dail_att set outtime = max(:bartime) where barcode= :barcode
and ATTEND_DATE = :bardate-1;
else
insert into dail_att(barcode,intime,attend_date)
values(:barcode,:min(bartime),:bardate);
end if;
end if;
end if;
while i am trying to this groupfunction it throws error while i use having tell me how to use group function and where
to use
Regadrs
Srikkanth.MHai sir
I had a table that contain fields
EMPCODE NUMBER
EMPNAME VARCHAR2(25)
BARCODE VARCHAR2(25)
INTIME VARCHAR2(25)
OUTTIME VARCHAR2(25)
INTRTIMEIN VARCHAR2(25)
INTROUTTIME VARCHAR2(25)
PERTIMEIN VARCHAR2(25);
PERTIMEOUT VARCHAR2(25);
ATTEND_DATE DATE ;
Consider that a table with 6 fields ie timein,intrtimein,pertimein,pertimeout,intrtimeout,timeout
I have generating a attendance table and a table contain 6 various times for an employees and we need to arrange it in order
0815,0816,1230,1250,1645,1646
If 0815 is the starting time then timein ie mintime
0816 stored to be in intrtime
then1250 then it stored in pertimein
then 1230 then it stored in pertimeout
then 1645 stored in intrtimeout
then 1646 stored in timeout
I tried with max and min function but its not working properly pls tell me some solutions
Thanks & Regards
Srikkanth.M -
How to use group function in insert or update
Hai All
How can we use group function in insert or update statement
I am generating an attendance so i have different set of timing for example
0800,1200,1230, 1700 and i need to insert into these data into table with min value to intime and max value to
outtime and othere to inertval time in or out
Pls tell me with some example
For example
For INSERT
insert into T2 (barcode,empcode,intime,attend_date)
values(r2.cardn,r2.enpno,MIN(r2.ptime),r2.pdate);
For UPDATE
update dail_att set outtime= MAX(r2.ptime) where empcode=r2.enpno and barcode=r2.cardn and
attend_date=r2.pdate;
Here instead of where i need to use having so pls tell how to use
Thanks & Regards
Srikkanth.MHai Man
R2 is not a table name its a record
Let me explain clearly
I have to generate daily attendance for lot of employees So i have two table t1 and t2
T1 consist of three column empno,date,time
T2 consist of empno,name,date,intime,outtime,intrin,introut
So now i need to give the T1 Min value Of time to T2 Intime and T1 Max value of Time to T2 Outtime fields so there so many records while i am using
max(time) it gives the max value of all so i seperated by group function so now i have an error in subquery ie it is an single row subquery so i need to use multiple row subquery how i can use multiple row subquery in update statement
Thanks In Advance
Srikkanth.M -
PL/SQL equivalent of T-SQL - "group function is not allowed here"
Hi all, hope someone can give me a hand as I'm pretty stuck! I have been trying to convert some MS SQL Server T-SQL statements into Oracle PL/SQL and am stuck on the below one:
SELECT
CA.AssessmentID,
(SELECT ProductName + ISNULL(' - ' + PrincipalBenefit,'')
FROM rptPolicySnapshot WHERE PolicyID = MAX(CA.PolicyID)
AND SnapshotID = 1),
MAX(CA.PolicyID)
FROM rptClaimInvoiceLineSnapshot CIL
INNER JOIN rptClaimAssessmentSnapshot CA
ON CIL.AssessmentID = CA.AssessmentID
AND CIL.SnapshotID = CA.SnapshotID
WHERE CIL.SnapshotID = 1
GROUP BY CA.AssessmentID
This works fine in MSSQL but returns the below error in Oracle:
'ORA-00934: group function is not allowed here'
If I take out the subquery the query works fine.
Any ideas as to the syntax? I am new to Oracle so not sure as to how I should go about writing this.
Thanks in advance!
LeoWITH x AS (SELECT ca.assessmentid,
MAX (ca.policyid) policy_id
FROM rptclaiminvoicelinesnapshot cil
INNER JOIN rptclaimassessmentsnapshot ca
ON cil.assessmentid = ca.assessmentid
AND cil.snapshotid = ca.snapshotid
WHERE cil.snapshotid = 1
GROUP BY ca.assessmentid
SELECT x.assessment_id,
x.policy_id,
productname + decode(principalbenefit,null,null,' - ' || principalbenefit ) prodname
FROM rptpolicysnapshot, x
WHERE policyid = x.policy_id
AND snapshotid = 1I think that's in the neighbourhood. -
Is there a way to create a group function?
I want to create a group function like max,sum,count for a
pl/sgl function. This group function will be something like max
(let's call it max2) but it will return the second highest value
of a group.
Any ideas anyone?Since you might not completely understand what i mean i give you
this example:
the ranktest table is:
A B C
a b 1
a b 1
a b 5
a b 12
a b 7
b c 1
b c 3
b c 2
b c 4
b c 8
c d 4
c d 3
c d 6
c d 1
c d 2
c d 12
c d 7
c d 3
a b 5
c d 12
SELECT A,B,C
FROM (
SELECT A,B,C, dense_rank() over (
PARTITION BY A,B
ORDER BY C desc) AS sorted_c
FROM ranktest a)
WHERE sorted_c = 2
-- this way we get the c column to have the 2nd highest value
-- for each set of A and B
--RESULT
a b 7
b c 4
c d 7
SELECT a.A,a.B, sum(a.C), count(a.C)
FROM ranktest a
GROUP BY a.A,a.B
-- this way we get the group functions to work on two of the
-- columns
--RESULT
a b 31 6
b c 18 5
c d 50 9
The result that i want is
a b 31 6 7
b c 18 5 4
c d 50 9 7
My question is can this be done in a single select statement. I
know it can be done with an intermediate step (of the creation
of a temp table).
Plus i would like to ask the experts, would a single select
statement be more efficient?
NOTE: the tables and data are just test data. The real tables
and data and the select query are different. The real ranktable
has about 1 million records. That's why i am worried about
efficiency. -
How to create a user_defined grouping function?
I need to create a user_defined function and use it in my sql statement.
I know the user_defined function can be used anywhere the build-in function can be used.In oracle,there are grouping functions.
How can I create a user_defined grouping function and I can use it in my sql statement.Hi, can you explain exactly what you are trying to achieve:
SQL> create or replace function my_func return number as
2 begin
3 return 5;
4 end;
5 /
Function created.
SQL> select deptno,my_func(),max(sal)
2 from emp
3 group by deptno,my_func()
4 /
DEPTNO MY_FUNC() MAX(SAL)
10 5 5000
20 5 3000
30 5 2850 -
ORA-00978 without group function
I've experienced a strange problem with oracle 11g.
I've retrieved the oracle exception ORA-00978 even if there was no group function in my query.
I supposed was a problem in the optimizer so I rebuild the tables statistics, after that the query was execute successfully.
Does anyone has an idea what the problem is?
Is possible that a bug exists in the 11g optimizer?
My oracle version is:
Oracle Database 11g Enterprise Edition 11.1.0.6.0 64bit Production
the query i tried is:
SELECT *
FROM TBCALENDAR Cal,
VWCALENDARACTIVITY CA,
VWSE R,
TBSCHEDULERPARTITION P,
TBREGION REG,
TBRESOURCE RES ,
TBZIPCITY z
WHERE Res.id=Cal.RESOURCE_ID
AND R.RESOURCE_ID=RES.ID
AND Cal.ACTIVITY_ID=CA.ID
AND CA.SCHEDULING=1
AND Cal.SCHEDPARTITION_ID IN
(select item.PARTITION_ID
from tbidcprofile prof,
tbidcpartitem part,
tbschedpartitem item
where prof.USERPROFILE_ID=4
and prof.IDCPARTITION_ID=part.PARTITION_ID
and part.BUSINESSUNIT_ID=item.BUSINESSUNIT_ID
and part.REGION_ID=item.REGION_ID )
AND TRUNC(Cal.START_DT)=trunc(sysdate)
AND P.ID=Cal.SCHEDPARTITION_ID
AND REG.ID(+)=Cal.WORKREGION_ID
AND Z.GEOLOCATION_ID(+)=Cal.HOMEGEOLOC_ID;
VWCALENDARACTIVITY and VWSE are two views, but I can select from them without any problem.
I've also tried to remove one view at a time an the error occurs only when the query uses both view at the same time.
Thanks
Renzouser479513 wrote:
VWCALENDARACTIVITY and VWSE are two views, but I can select from them without any problem.
I've also tried to remove one view at a time an the error occurs only when the query uses both view at the same time.
What are the views definition ?
Nicolas. -
How to create a grouping function
How to create a grouping function ?? For example, can I create a function:myavg and I can use it like :
select col_a, col_b, myavg(col_c)
from table_a
group by col_a, col_b
Thanks in advance !!
nullI don't think that you can create a grouping function in Oracle, but you can create a function and use it in Select statments.
null -
Hello all,
How can I use a nested group function in a select statement?
For example: select sum(count(id), column a, column b from table_name group by???
Thanks in advance!select sum(CNT) column_a, column_b
from (select count(id), column_a, column_b from table_name
group by column_a, column_b)
group by column_a, column_b -
Not a single-group group function very urgent
HI,
select sum(avg(wait_to))+sum(avg(idle))+sum(avg(users))+sum(avg(system)) from system_cpu
where hostid='DSCP02469' group by cpuid
this query is working
but i want cpuid group to be displayed
so i wrote query like this.
select cpuid,sum(avg(wait_to))+sum(avg(idle))+sum(avg(users))+sum(avg(system)) from system_cpu
where hOstid=' ' group by cpuid it is throwing a error
not a single-group group function.
how can get sum of avg of colums,column based on some coloum
group by column
id,sum(avg( )) i have to get
please give me solution.
send me mail to [email protected]hi,
thanku for immediate reply but
if i do that
select cpuid, a+b+c+d total_value from (select cpuid,
sum(avg(wait_to)) a, sum(avg(idle)) b,sum(avg(users)) c,sum(avg(system)) d
from system_cpu where hostid='DSCP02469' group by cpuid)
SQL> /
select cpuid, a+b+c+d total_value from (select cpuid,
ERROR at line 1:
ORA-00937: not a single-group group functionelse
select cpuid, a+b+c+d total_value from (select
sum(avg(wait_to)) a, sum(avg(idle)) b,sum(avg(users)) c,sum(avg(system)) d
from system_cpu where hostid='DSCP02469' group by cpuid)
SQL> /
select cpuid, a+b+c+d total_value from (select
ERROR at line 1:
ORA-00904: "CPUID": invalid identifier2)
can we use select in select case
can we use select sal, case when sal then select * from emp like this
with regards
shannu sarma -
Nested group function without group xmlagg
I am getting nested group function without group by xmlagg when using the xmlagg function inside another xmlagg function. Find the table structure and sample data here,
CREATE TABLE "TEST_TABLE"
("KEY" NUMBER(20,0),
"NAME" VARCHAR2(50 ),
"DESCRIPTION" VARCHAR2(100 )
Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (1,'sam','desc1');
Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (2,'max','desc2');
Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (3,'peter',null);
Insert into TEST_TABLE (KEY,NAME,DESCRIPTION) values (4,'andrew',null);
select
XMLSerialize(document
xmlelement("root",
xmlagg(
xmlelement("emp"
, xmlforest(Key as "ID")
, xmlforest(name as "ename")
, xmlelement("Descriptions",
xmlagg(
xmlforest(description as "Desc")
) as clob indent
) as t
from test_table;Then i removed the xmlagg function from the above select query and used xmlelement instead
select
XMLSerialize(document
xmlelement("root",
xmlagg(
xmlelement("emp"
, xmlforest(Key as "ID")
, xmlforest(name as "ename")
, xmlelement("Descriptions",
xmlforest(description as "Desc")
) as clob indent
) as t
from test_table;This is working fine, but xml created with empty elements for Descriptions element for key 3 and 4 which has null values. I need don't need Descriptions element in the xml when it has null value. Please help me to resolve this.You can do it with a correlated subquery :
SQL> select xmlserialize(document
2 xmlelement("root",
3 xmlagg(
4 xmlelement("emp"
5 , xmlforest(
6 t.key as "ID"
7 , t.name as "ename"
8 , (
9 select xmlagg(
10 xmlelement("Desc", d.description)
11 order by d.description -- if necessary
12 )
13 from test_desc d
14 where d.key = t.key
15 ) as "Descriptions"
16 )
17 )
18 )
19 ) as clob indent
20 )
21 from test_table t;
XMLSERIALIZE(DOCUMENTXMLELEMEN
<root>
<emp>
<ID>1</ID>
<ename>sam</ename>
<Descriptions>
<Desc>desc1_1</Desc>
<Desc>desc1_2</Desc>
<Desc>desc1_3</Desc>
</Descriptions>
</emp>
<emp>
<ID>2</ID>
<ename>max</ename>
<Descriptions>
<Desc>desc2_1</Desc>
<Desc>desc2_2</Desc>
<Desc>desc2_3</Desc>
</Descriptions>
</emp>
<emp>
<ID>3</ID>
<ename>peter</ename>
</emp>
<emp>
<ID>4</ID>
<ename>andrew</ename>
</emp>
</root>
Or an OUTER JOIN + GROUP-BY :
select xmlserialize(document
xmlelement("root",
xmlagg(
xmlelement("emp"
, xmlforest(
t.key as "ID"
, t.name as "ename"
, xmlagg(
xmlforest(d.description as "Desc")
order by d.description -- if necessary
) as "Descriptions"
) as clob indent
from test_table t
left outer join test_desc d on d.key = t.key
group by t.key
, t.name
;Edited by: odie_63 on 11 juil. 2012 14:54 - added 2nd option -
Grouping Function usage in SQL
All,
How does grouping function work, does it also depends on the sequence of columns used in group by rollup
select deptno,job, group_id(), sum(sal), grouping(deptno) , grouping(job)
from emp
group by rollup (deptno, job)
If the query is run by "rollup(job,deptno)" grouping (deptno) returns 1 where as if "rollup(deptno,job)" grouping (deptno) returns 0.
Does the column sequence in group by rollup decides the returning of grouping(deptno) function. I feel to have some misunderstanding of the way its said in ORACLE documentation. We referred to the below link for our understanding.
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions071.htm
Request the PRO's to discuss and clarify on this.Hi,
ramarun wrote:
All,
How does grouping function work, GROUPING (x) returns 1 if the current row represents a superaggregate of x, and 0 otherwise. In other words, if GROUPING (x) is 0, then the row represents a single value of x. If GROUPING (x) is 1, then the row represents a total of all values of x.
For example the query you posted may produce results like this:
` DEPTNO JOB GROUP_ID() SUM(SAL) GROUPING(DEPTNO) GROUPING(JOB)
20 ANALYST 0 6000 0 0
ANALYST 0 6000 1 0
10 CLERK 0 1300 0 0
20 CLERK 0 1900 0 0
30 CLERK 0 950 0 0
CLERK 0 4150 1 0
10 MANAGER 0 2450 0 0
20 MANAGER 0 2975 0 0
30 MANAGER 0 2850 0 0
MANAGER 0 8275 1 0
10 PRESIDENT 0 5000 0 0
PRESIDENT 0 5000 1 0
30 SALESMAN 0 5600 0 0
SALESMAN 0 5600 1 0
0 29025 1 1Look at the last three rows. On this row
` DEPTNO JOB GROUP_ID() SUM(SAL) GROUPING(DEPTNO) GROUPING(JOB)
30 SALESMAN 0 5600 0 0GROUPING (deptno) is 0; so the row represents only deptno=30.
GROUPINT (job) is 0, so the row represents only job='SALESMAN'.
SUM (sal) is 5600, so the total salary of all salesmen in department 30 is 5600.
Now look at this row:
` DEPTNO JOB GROUP_ID() SUM(SAL) GROUPING(DEPTNO) GROUPING(JOB)
SALESMAN 0 5600 1 0GROUPING (deptno) is 1; so the row does not represent any one deptno; it is a superaggregate of all deptnos. Notice that the deptno column is NULL, even though there are no rows in the table where deptno is NULL.
GROUPING (job) is 0, so the row represents only job='SALESMAN'.
SUM (sal) is 5600, so the total salary of all salesmen in any department is 5600. (As it happens, all the salesmen are in department 30, so this sum is the same as on the previous row.)
Now look at the last row:
` DEPTNO JOB GROUP_ID() SUM(SAL) GROUPING(DEPTNO) GROUPING(JOB)
0 29025 1 1GROUPING (deptno) is 1; so the row does not represent any one deptno; it is a superaggregate of all deptnos. Notic that both deptno and job are both NULL on this row of output, even though neither column is ever NULL in the table.
GROUPING (job) is 0, so the row represents only job='SALESMAN'.
SUM (sal) is 5600, so the total salary of people with any job and any department is 29025.
does it also depends on the sequence of columns used in group by rollup Excellent question! Try it an see. Change the order or columns in the GROUP BY clause and see. If you get different results, then the order does matter.
select deptno,job, group_id(), sum(sal), grouping(deptno) , grouping(job)
from emp
group by rollup (deptno, job) If the query is run by "rollup(job,deptno)" grouping (deptno) returns 1 where as if "rollup(deptno,job)" grouping (deptno) returns 0. You get different results; therefore the order does matter.
Does the column sequence in group by rollup decides the returning of grouping(deptno) function...Yes.
"GROUP BY ROLLUP (deptno, job)" means a superaggregate row for each value of job, representing all deptnos, will be formed. That is, you will get one row representing all analysts; another row representing all clerks, another row representing all managers, and so on. This will be similar to the results that you would get if you never mentioned the first expression in the ROLLUP list, that is, deptno. You will not get a superaggregate row representing any one deptno, say, 10.
"GROUP BY ROLLUP (job, deptno)" means a superaggregate row for each value of deptno, representing all jobs, will be formed. That is, you will get one row representing all anlysts; another row representing deprtment 10, another row representing department 20, and so on. This will be similar to the results that you would get if you never mentioned the first expression in the ROLLUP list, that is, job. You will not get a superaggregate row representing any one job, say, 'ANALYST'. -
Goup by in oracle vs group function in coldfusion
Hello
what is the difference between goup by in oracle vs group
function in coldfusion?It's easier to list the similarities:
Both use the word "group".
Other than that: they're different things.
Both are well documented in their respective documentation
sources (which
are avaialble online). Suggest you read up.
Adam -
Hello does anyone know how to use the group function in
coldfusion
my tables that out-puts to a html
is outputing multiple CEENO
for example CEENO
11111
11111
11111
11111
someone here said to use the group function in coldfusion
do you know where i can put it?
<table border="2" cellpadding="2" cellspacing="0">
<tr>
<th>Record Number</th>
<TH>PEA_id</TH>
<th width="120">CEENO</th>
<TH>Address</TH>
<TH>ZipCode</TH>
<th>AdvisoryDate</th>
</tr>
<cfloop query="getthecases">
<tr bgcolor="<cfif currentrow mod
2>GHOSTWHITE<cfelse>WHITE</cfif>">
<td>#CurrentRow#</td>
<td>#PEA_id</td>
<Td>#CEENO#</td>
<td>#ST_NO# #ST_initial# #st_dir#</td>
<td>#ZipCode#</td>
<td>#dateformat(ADVISORY_DATE,"mm/dd/yyyy")#</td>
</tr>
</cfloop>
</table>What they are referencing is the group attribute of the
CFOUTPUT tag:
<cfoutput query="qryName" group="groupName">
#dbgroupvar#
<cfoutput>
#otherdbdata#
</cfoutput>
</cfoutput>
However, you have to write the correct SQL to get this to
work and that includes using the ORDER BY clause in your SQL
Statement to specify what column on which to group the output.
For example, the following query would get all employees from
the database and order them by the department for which they work:
<cfquery name="qTest" datasource="dsn">
SELECT name, title, department
FROM employee
ORDER BY department
</cfquery>
We can use the group attribute of cfoutput to show all
employees for each department:
<cfoutput query="qTest" group="department">
<h1>#department#</h1>
<table>
<cfoutput>
<tr>
<td>#name#</td>
<td>#title#</td>
</tr>
</cfoutput>
</table>
Hope this helps.
Cheers,
Craig -
ORA-00935: group function is nested too deeply - SQL Query is correct...
select s.sname, to_char(sum(t.amount), 'fm$999999.00')
from transactions t, salespeople s
where t.sid = s.sid
group by s.sname, t.year
having (t.year = 1997) and max(sum(t.amount));
I'm trying to print sales person name who had the highest total sell in 1997. I do not see where did i go wrong in the above query...Isn't nested group function allowed?!
What about this?
select s.sname, to_char(sum(t.amount), 'fm$999999.00') as "TOTAL"
from transactions t, salespeople s
where t.sid = s.sid
group by s.sname, t.year
having (t.year = 1997) and sum(t.amount) = (select max(amount) from transactions where year = 1997);
It should return something, but it does not due lack of data in database. I think the above code is correct. I just need to confirm from you...select s.sname, (case when sum(t.amount) > sum(t2.amount)
then sum(t.amount)
else sum(t2.amount)
end) "TOTAL"
from transactions t, salespeople s
where t.sid = s.sid
group by s.sname, t.year
having t.year = 1997
union
select s.sname, sum(t2.amount)
from transactions t2, salespeople s
where t2.sid = s.sid
group by s.sname, t2.year
having t2.year = 1997;
Your code looks good, but it does not work in Oracle Developer...
The above should work isn't it? The inner query gets executed then the outer one so the result i get in -
Getting an error - group function not allowed here
Below is the merge statement...
getting an error - group function not allowed here
WHY????
merge into summary
using
(select
a.user_id,
min(a.start_time_utc),
max(a.end_time_utc),
sum(a.duration_seconds),
/*total_upload
total_download
total_traffic,*/
max(r.package_id),
last_usage_charge, -------hard coded
max(r.peak_rate),
max(r.bst_plantype),
max(r.free_value), ---for free value
a.IsPeak,
sum(a.TotalDiscount)
from aaa_sessions a,rate_plan r,subscriber_info si
where
si.EXTERNAL_ID=a.USER_ID
and
si.PACKAGE_ID=r.PACKAGE_ID
group by
user_id,bst_plantype,ispeak)t ------do we need to use alias here
on
(summary.user_id=t.user_id
and
summary.type_of_summary=t.bst_plantype
and
summary.ispeak=t.ispeak)
When matched then
update
set
start_date =decode((t.start_time_utc-summary.start_date)-abs(t.start_time_utc-summary.start_date),0,summary.start_date,t.start_time_utc),
end_date=decode((t.end_time_utc-summary.end_date)-abs(t.end_time_utc-summary.end_date),0,t.end_time_utc,s.end_date),
total_duration=summary.total_duration+sum(duration_seconds),
total_upload=summary.total_upload+sum(upload_bytes),
total_download=summary.total_download+sum(download_bytes),
total_traffic=summary.total_upload+sum(upload_bytes)+summary.total_download+sum(download_bytes)
When not matched then
INSERT
(user_id ,
start_date,
end_date,
total_duration,
/*total_upload
total_download
total_traffic,*/
rate_plan_id,
last_usage_charge,
peak_rate,
type_of_summary,
IsPeak,
TotalDiscount)
VALUES
(t.user_id,
t.start_time_utc,
t.end_time_utc,
t.duration_seconds,
/*t.output_bytes,
t.input_bytes,
t.output_bytes+aa.input_bytes,*/
t.PACKAGE_ID,
1, ---hard coded the value
t.PEAK_RATE,
t.BST_PLANTYPE,
t.ispeak,
t.free_value);This is the query,...
Its giving no complilation errors..
I have not used aggregate functions in the insert/update..
have used only decode...
and nowhere i found that aggegate functions not allowed in the insert/update stmts of merge..
Can u please post a link where it is mentioned...
MERGE INTO summary
USING (SELECT a.user_id, MIN (a.start_time_utc) stc,
MAX (a.end_time_utc) etc, SUM (a.duration_seconds) ds,
SUM (a.download_bytes) download,
SUM (a.upload_bytes) upload, MAX (r.package_id) pkg_id,
MAX (r.peak_rate) p_rate, MAX (r.offpeak_rate)
ofp_rate,
MAX (r.bst_plantype) plan_type,
SUM (r.free_value) free_val, a.ispeak,
MAX (r.peak_pulse) p_pulse,
MAX (r.offpeak_pulse) ofp_pulse
FROM aaa_sessions a, rate_plan r, subscriber_info si
WHERE si.external_id = a.user_id
AND si.package_id = r.package_id
GROUP BY user_id, bst_plantype, ispeak) t
ON ( summary.user_id = t.user_id
AND summary.type_of_summary = t.plan_type
AND summary.rate_plan_id = t.pkg_id
AND summary.ispeak = t.ispeak)
WHEN MATCHED THEN
UPDATE
SET start_date =
DECODE ( (t.stc - summary.start_date)
- ABS (t.stc - summary.start_date),
0, summary.start_date,
t.stc
end_date =
DECODE ( (t.etc - summary.end_date)
- ABS (t.etc - summary.end_date),
0, t.etc,
summary.end_date
total_duration = summary.total_duration + t.ds,
total_download = summary.total_download + t.download,
total_upload = summary.total_upload + t.upload,
total_traffic =
summary.total_upload
+ t.upload
+ summary.total_download
+ t.download,
last_usage_charge =
DECODE (t.plan_type,
0, (t.ds - t.free_val)
/ DECODE (t.ispeak, 0, t.ofp_pulse, p_pulse)
* DECODE (t.ispeak, 0, t.ofp_rate, t.p_rate),
((t.download + t.upload) - t.free_val
* DECODE (t.ispeak, 0, t.ofp_rate, t.p_rate)
WHEN NOT MATCHED THEN
INSERT (user_id, start_date, end_date, total_duration,
total_download, total_upload, total_traffic, rate_plan_id,
last_usage_charge, peak_rate, type_of_summary, ispeak,
totaldiscount)
VALUES (t.user_id, t.stc, t.etc, t.ds, t.download, t.upload,
t.download + t.upload, t.pkg_id,
DECODE (t.plan_type,
0, (t.ds - t.free_val)
/ DECODE (t.ispeak, 0, t.ofp_pulse, p_pulse)
* DECODE (t.ispeak, 0, t.ofp_rate, t.p_rate),
((t.download + t.upload) - t.free_val
* DECODE (t.ispeak, 0, t.ofp_rate, t.p_rate)
t.p_rate, t.plan_type, t.ispeak, t.free_val);
COMMIT;
Maybe you are looking for
-
I am trying to update my phone and the phone is frozen on a screen that has the itunes symbol and the computer connection showing. I did not have itunes on my computer. So I downloaded itunes set up which took an hour or so. Now what do I do?
-
" APPLICATION NOT FOUND" while deploying Web dynpro java Application
Hi, I am getting a message that "application not found " While deploying the web dynpro java application .......... please give me a solution for this .
-
Inverses and 1-N relationships
It's common to see people complain that JDO doesn't automagically set child-to-parent pointers for 1-N relationships with inverses. The answer given, which I agree with, is that it is up to you to keep your data model consistent, and that use of JDO
-
Setting default "Initial View" options for new PDFs
I usually want most of the PDFs I create to open with the pages panel displayed and "fit to window". Currently I open every PDF and change that in the document properties under "Initial View", save and close. There must be a better way. Is it possibl
-
Set background color to selected jtable columns cell
I am clicking on jtables column.I want to select any column and that column (only selected cell of column)should get displayed with specific background color. plz help