Grouping select statement
Hi! I'm running this query on oracle :
"select e.userid, d.accountno from userpasswords e, account d where e.tin = d.tin" ,
but when I sort or group them according to the userids, i kept on getting errors. Does anyone here know what I missed?
It works for me.
1* select count(jj.job_title), jj.job_title from jobs jj group by job_title
SQL> /
COUNT(JJ.JOB_TITLE) JOB_TITLE
1 Accounting Manager
1 Sales Representative
1 Purchasing Clerk
1 Programmer
1 Public Relations Representative
1 Administration Vice President
1 Stock Manager
1 Marketing Representative
1 President
1 Administration Assistant
1 Finance Manager
COUNT(JJ.JOB_TITLE) JOB_TITLE
1 Purchasing Manager
1 Human Resources Representative
1 Representive
1 Accountant
1 Stock Clerk
1 Shipping Clerk
1 Public Accountant
1 Sales Manager
1 Marketing Manager
20 rows selected.
Similar Messages
-
Count(*) in select statement having group by clause
Hi
I would like to use count(*) in select statement having group by clause. say for example there is a state with a number of cities listed. I would like to get the count of cities for each state.
the sql stement is grouped by city and it is joined with 5 more tables.
ThanksI suspect you want to look into analytic functions (assuming you have a recent version of Oracle). asktom.oracle.com has numerous examples if you do a search.
Justin -
Count(*) function in select statement having group by condition
Hi
I would like to use count(*) in select statement having group by clause. say for example there is a state with a number of cities listed. I would like to get the count of cities for each state.
the sql stement is grouped by state and it is joined with 5 more tables.
Thanks
ps: ignore the previous postAssuming there is one record per city per state, then
SELECT state,count(*)
FROM state_tbl
GROUP BY stateWill get one record per state with the number of cities in each state. If you want to join that result set to other tables you need to either create a view with that statement or use an in-line view viz.
SELECT c.cust_name,c.state,s.num_cities
FROM customers c,
(SELECT state,count(*) num_cities
FROM state_tbl
GROUP BY state) s
WHERE c.state = s.stateTTFN
John -
Group By Select Statement aggregate function error.
I am using Dreamweaver MX4 and have an ASP web page. I can easily do this in Access 2007 a simple "group by" sum totals statement.
Why can I not get this to work on a web page? Are the "#" signs messing this up in the Code2 Select Statement?
How can I get Code2 to work correctly? Code1 works fine. A user enters the customer and a date range on a form and submits request. I get error message in Dreamweaver. You tried to execute a query where the specified expression field3 is not part of an aggregate function
Code1. This works fine SELECT Field3, Field10, SUM(Field16) as SumofField16 FROM CustomerHistory_CP WHERE Field3 LIKE '%Search_Criteria%' AND Field6 >= #1/2/09# AND Field6 <= #1/30/09# GROUP BY Field3, Field10
Code2. I get error message SELECT Field3, Field10, SUM(Field16) as SumofField16 FROM CustomerHistory_CP WHERE Field3 LIKE '%Search_Criteria%' AND Field6 >= #Date1# AND Field6 <= #Date2# GROUP BY Field3, Field10I am using Dreamweaver MX4 and have an ASP web page. I can easily do this in Access 2007 a simple "group by" sum totals statement.
Why can I not get this to work on a web page? Are the "#" signs messing this up in the Code2 Select Statement?
How can I get Code2 to work correctly? Code1 works fine. A user enters the customer and a date range on a form and submits request. I get error message in Dreamweaver. You tried to execute a query where the specified expression field3 is not part of an aggregate function
Code1. This works fine SELECT Field3, Field10, SUM(Field16) as SumofField16 FROM CustomerHistory_CP WHERE Field3 LIKE '%Search_Criteria%' AND Field6 >= #1/2/09# AND Field6 <= #1/30/09# GROUP BY Field3, Field10
Code2. I get error message SELECT Field3, Field10, SUM(Field16) as SumofField16 FROM CustomerHistory_CP WHERE Field3 LIKE '%Search_Criteria%' AND Field6 >= #Date1# AND Field6 <= #Date2# GROUP BY Field3, Field10 -
SQL column as select statement to be referenced into a group by
Is there a notation or solution to be able to reference a column that is a inline select statement within a group by as detailed below? The group by will not accept the alias. I also tried to make the SQL a MAX to remove the need for the group by reference and this returned invalid expression.
SELECT DISTINCT hdr.BUSINESS_UNIT,
hdr.SESSN_ID,
hdr.STREAM_ROOT_ID,
hdr.SESSN_STS_CD,
hdr.SESSN_CRE_DTTM,
CASE
WHEN C.OPRID <> ' ' THEN C.OPRID
ELSE S.OPERATOR
END OPRID,
strm.QS_APP_CONTEXT,
RECV.QTY_SH_RECVD Quantity_Received,
CASE
WHEN hdr.BUSINESS_UNIT = 'MFG01' THEN MAX(G.MFDS_SGRP_SIZE)
ELSE MAX(S.SESSN_SGRP_SIZE)
END Quantity_Inspected,
MAX(S.QS_VALUEREADING_1) Defect_Count,
CASE
WHEN MAX(S.QS_VALUEREADING_1) = 0 THEN ' '
ELSE MAX(G.MFDS_NAME)
END Characteristic,
MAX(CMNT.QS_COMMENT2) COMMENTS,
strm.INV_ITEM_ID,
itm.DESCR,
strm.WORK_CENTER_CODE,
strm.VENDOR_ID,
*(SELECT V.NAME1 FROM PS_VENDOR V WHERE strm.VENDOR_ID = V.VENDOR_ID AND V.SETID = (SELECT SETID FROM PS_SET_CNTRL_REC*
WHERE RECNAME = 'VENDOR'
AND SETCNTRLVALUE = strm.BUSINESS_UNIT)) VENDOR_NAME,
strm.PRDN_AREA_CODE,
strm.COMPL_OP_SEQ,
strm.PRODUCTION_TYPE,
C.RECEIVER_ID,
C.RECV_LN_NBR,
RECV.PO_ID,
RECV.LINE_NBR,
RECV.SCHED_NBR,
C.PRODUCTION_ID,
C.SERIAL_ID,
C.INV_LOT_ID
FROM PS_QS_SESSN_HDR8 hdr
LEFT OUTER JOIN PS_QS_SESSN_TRACE8 C
ON hdr.BUSINESS_UNIT = C.BUSINESS_UNIT
AND hdr.SESSN_ID = C.SESSN_ID
LEFT OUTER JOIN PS_RECV_INSPDTL_VW RECV
ON C.BUSINESS_UNIT = RECV.BUSINESS_UNIT
AND C.RECEIVER_ID = RECV.RECEIVER_ID
AND C.RECV_LN_NBR = RECV.RECV_LN_NBR
LEFT OUTER JOIN PS_QS_STREAM_ROOT strm
ON hdr.STREAM_ROOT_ID = strm.STREAM_ROOT_ID
AND hdr.BUSINESS_UNIT = strm.BUSINESS_UNIT
LEFT OUTER JOIN PS_QS_STREAM8_VW G
ON strm.STREAM_ROOT_ID = G.STREAM_ROOT_ID
AND strm.BUSINESS_UNIT = G.BUSINESS_UNIT
LEFT OUTER JOIN PS_QS_SUBGROUP S
ON hdr.BUSINESS_UNIT = S.BUSINESS_UNIT
AND hdr.SESSN_ID = S.SESSN_ID
AND S.STREAM_ID = G.STREAM_ID
LEFT OUTER JOIN PS_QS_SESSN_COMM8 CMNT
ON S.BUSINESS_UNIT = CMNT.BUSINESS_UNIT
AND S.SESSN_ID = CMNT.SESSN_ID
AND S.STREAM_ID = CMNT.STREAM_ID
AND C.SAMPLE = CMNT.SAMPLE
LEFT OUTER JOIN PS_MASTER_ITEM_TBL itm
ON itm.INV_ITEM_ID = strm.INV_ITEM_ID
LEFT OUTER JOIN PS_SET_CNTRL_REC cntrl
ON itm.SETID = cntrl.SETID
AND cntrl.RECNAME = 'MASTER_ITEM_TBL'
AND cntrl.SETCNTRLVALUE = strm.BUSINESS_UNIT
WHERE S.QS_VALUEREADING_1 = (SELECT MAX(S2.QS_VALUEREADING_1)
FROM PS_QS_SUBGROUP S2
WHERE S2.BUSINESS_UNIT = S.BUSINESS_UNIT
AND S2.SESSN_ID = S.SESSN_ID
AND S2.STREAM_ID = S.STREAM_ID)
GROUP BY hdr.BUSINESS_UNIT,
hdr.SESSN_ID,
hdr.STREAM_ROOT_ID,
hdr.SESSN_STS_CD,
hdr.SESSN_CRE_DTTM,
C.OPRID,
S.OPERATOR,
strm.QS_APP_CONTEXT,
RECV.QTY_SH_RECVD,
strm.INV_ITEM_ID,
itm.DESCR,
strm.WORK_CENTER_CODE,
strm.VENDOR_ID,
VENDOR_NAME,
strm.PRDN_AREA_CODE,
strm.COMPL_OP_SEQ,
strm.PRODUCTION_TYPE,
C.RECEIVER_ID,
C.RECV_LN_NBR,
RECV.PO_ID,
RECV.LINE_NBR,
RECV.SCHED_NBR,
C.PRODUCTION_ID,
C.SERIAL_ID,
C.INV_LOT_IDHi,
Assign the alias in a sub-query. Then you'll be able to use it wherever you want to, and how many times you want to, in a super-query.
For example:
WITH got_vendor AS
SELECT hdr.business_unit
SELECT v.name1
FROM ps_vendor v
WHERE strm.vendor_id = v.vendor_id
AND v.setid = (
SELECT setid
FROM ps_set_cntrl_rec
WHERE recname = 'VENDOR'
AND setcntrlvalue = strm.business_unit
) AS vendor_name
FROM ps_qs_sessn_hdr8 hdr
SELECT business_unit -- NOTE: no hdr.; all columns are from got_vendor now
, vendor
FROM got_vendor
GROUP BY business_unit
, vendor
;When you define an alias (such as vendor) in a query, you can use that alias in the ORDER BY clause of that same query, but that's the only place in that same query where you can use it. If you want to use the alias anywhere else (e.g., in the GROUP BY clause, as in your example, the WHERE clause, or elsewhere in the SELECT clause), then you probably want to compute it in a sub-query, as shown above.
There's probably a better way to compute vendor, but that's a separate problem.
Edited by: Frank Kulash on Jan 3, 2012 10:37 AM
Added example -
Creating a record group not via select statement
Normally a record group is created via a select statement
(f.e. Querystr='Select deptno,dname from dept
populate_group_with_query( GroupId, QueryStr )
Is it possible to create it via a package procedure sending a
refcursor / arrayFIND_GROUP(groupname);
CREATE_GROUP_FROM_QUERY(groupname, qry);
POPULATE_GROUP_WITH_QUERY(rgid, qry);
Add_Group_Column(...);
Add_Group_Row(...); -
Group by week on select statement
Hi friends,
I have a select statement which returns me the data that i need to group it on a weekly basis. The week starts from wednesday morning to tuesday night. what should e the syntax for group by clause to get the desired result. I am using the bar chart to display the results.
Please let me know.
Regards,
PradeepI have moved this question to SQL/PLSQL forum.
-
What is the use of additon in up to 1 rows in SELECT statement
Hi All,
What is the use of up to 1 rows in select statement.
for example
SELECT kostl
FROM pa0001
INTO y_lv_kostl UP TO 1 ROWS
WHERE pernr EQ pernr
AND endda GE sy-datum.
ENDSELECT.
I'm unable to get in wat situations we hav to add up to 1 rows
please help me out...
Thanks,
santosh.Hi,
Use "select up to 1 rows" only if you are sure that all the records returned will have the same value for the field(s) you are interested in. If not, you will be reading only the first record which matches the criteria, but may be the second or the third record has the value you are looking for.
The System test result showed that the variant Single * takes less time than Up to 1 rows as there is an additional level for COUNT STOP KEY for SELECT ENDSELECT UP TO 1 ROWS.
The 'SELECT .... UP TO 1 ROWS' statement is subtly different. The database selects all of the relevant records that are defined by the WHERE clause, applies any aggregate, ordering or grouping functions to them and then returns the first record of the result set.
Regards,
Bhaskar -
If I want to order by dateAdded to I have to include it in the select statement like
If I do this:
<cfquery name="chart" datasource="#datasource#">
select behaviourID,score, dateAdded2 = convert(varchar,
dateAdded , 101)
from staff_charts_data
where userID =
<cfqueryparam value="#arguments.userID#"
cfsqltype="cf_sql_integer"> AND
NOT score = 5
</cfquery>
If I want to order by dateAdded to I have to include it in
the select statement like so:
<cfquery name="chart" datasource="#datasource#">
select behaviourID,score, dateAdded2 = convert(varchar,
dateAdded , 101),dateAdded
from staff_charts_data
where userID =
<cfqueryparam value="#arguments.userID#"
cfsqltype="cf_sql_integer"> AND
NOT score = 5
</cfquery>you should be able to order by dateAdded without it being in
the select clause. The only time the order by field has to be in
the select clause is when your query has an aggregate and group by
clause, like this:
select field1, field2, max(field3) as youralias
from sometables
where whatever
group by field1, field2
order by ???
You can only order field1, field2, max(field3) or youralias.
You can't order by field4. -
Using Select statement in IF condition?
hi all,
Can i use select statement in IF COndition in pl sql ?
eg like- if( select 1 from ASD) then
end if;There is no way to do any kind of select statement inside if conditions.
Why don't test simple cases like this first?
An example to show it.
SQL> begin
2 if exists (select 1 from dual) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if exists (select 1 from dual) then
ERRORE alla riga 2:
ORA-06550: line 2, column 5:
PLS-00204: function or pseudo-column 'EXISTS' may be used inside a SQL
statement only
ORA-06550: line 2, column 2:
PL/SQL: Statement ignored
SQL> begin
2 if ( (select count(*) from dual) > 0 ) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if ( (select count(*) from dual) > 0 ) then
ERRORE alla riga 2:
ORA-06550: line 2, column 8:
PLS-00103: Encountered the symbol "SELECT" when expecting one of the following:
( - + case mod new not null others <an identifier>
<a double-quoted delimited-identifier> <a bind variable> avg
count current exists max min prior sql stddev sum variance
execute forall merge time timestamp interval date
<a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an alternativ
ORA-06550: line 2, column 33:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
. , @ ; for <an identifier>
<a double-quoted delimited-identifier> group having intersect
minus order partition start subpartition union where connect
SQL> begin
2 if ( 0 in (select count(*) from dual) ) then
3 dbms_output.put_line('ok');
4 end if;
5 end;
6 /
if ( 0 in (select count(*) from dual) ) then
ERRORE alla riga 2:
ORA-06550: line 2, column 12:
PLS-00405: subquery not allowed in this context
ORA-06550: line 2, column 2:
PL/SQL: Statement ignoredBye Alessandro -
Return multiple values from a function to a SELECT statement
I hope I've provided enough information here. If not, just let me know what I'm missing.
I am creating a view that will combine information from a few tables. Most of it is fairly straightforward, but there are a couple of columns in the view that I need to get by running a function within a package. Even this is fairly straightforward (I have a function named action_date in a package called rp, for instance, which I can use to return the date I need via SELECT rp.action_date(sequence_number).
Here's the issue: I actually need to return several bits of information from the same record (not just action_date, but also action_office, action_value, etc.) - a join of the tables won't work here as I'll explain below. I can, of course, run a separate function for each statement but that is obviously inefficient. Within the confines of the view select statement however, I'm not sure how to return each of the values I need.
For instance, right now, I have:
Table1:
sequence_number NUMBER(10),
name VARCHAR(30),
Table2:
Table1_seq NUMBER(10),
action_seq NUMBER(10),
action_date DATE,
action_office VARCHAR(3),
action_value VARCHAR(60),
I can't simply join Table1 and Table2 because I have to do some processing in order to determine which of the matching returned rows I actually need to select. So the package opens a cursor and processes each row until it finds the one that I need.
The following works but is inefficient since all of the calls to the package will return columns from the same record. I just don't know how to return all the values I need into the SELECT statement.
CREATE VIEW all_this_stuff AS
SELECT sequence_number, name,
rp.action_date(sequence_number) action_date,
rp.action_office(sequence_number) action_office,
rp.action_value(sequence_number) action_value
FROM table1
Is there a way to return multiple values into my SELECT statement or am I going about this all wrong?
Any suggestions?
Thanks so much!Hi,
What you want is a Top-N Query , which you can do using the analytic ROW_NUMBER function in a sub-query, like this:
WITH got_rnum AS
SELECT action_seq, action_dt, action_office, action_type, action_value
, ROW_NUMBER () OVER ( ORDER BY action_date
, action_seq
, action_serial
) AS rnum
FROM table2
WHERE action_code = 'AB'
AND action_office LIKE 'E' -- Is this right?
SELECT action_seq, action_dt, action_office, action_type, action_value
FROM got_rnum
WHERE rnum = 1
;As written, this will return (at most) one row.
I suspect you'll really want to get one row for each group , where a group is defined by some value in a table to which you're joining.
In that case, add a PARTITION BY clause to the ROW_NUMBER function.
If you'd post a little sample data (CREATE TABLE and INSERT statements), I could show you exactly how.
Since I don't have your tables, I'll show you using tables in the scott schema.
Here's a view that has data from the scott.dept table and also from scott.emp, but only for the most senior employee in each department (that is, the employee with the earliest hiredate). If there happens to be a tie for the earliest hiredate, then the contender with the lowest empno is chosen.
CREATE OR REPLACE VIEW senior_emp
AS
WITH got_rnum AS
SELECT d.deptno
, d.dname
, e.empno
, e.ename
, e.hiredate
, ROW_NUMBER () OVER ( PARTITION BY d.deptno
ORDER BY e.hiredate
, e.empno
) AS rnum
FROM scott.dept d
JOIN scott.emp e ON d.deptno = e.deptno
SELECT deptno
, dname
, empno
, ename
, hiredate
FROM got_rnum
WHERE rnum = 1
SELECT *
FROM senior_emp
;Output:
. DEPTNO DNAME EMPNO ENAME HIREDATE
10 ACCOUNTING 7782 CLARK 09-JUN-81
20 RESEARCH 7369 SMITH 17-DEC-80
30 SALES 7499 ALLEN 20-FEB-81
By the way, one of the conditions in the query you posted was
action_office LIKE 'E'which is equivalent to
action_office = 'E'(LIKE is always equivalent to = if the string after LIKE doesn't contain any wildcards.)
Did you mean to say that, or did you mean something like this:
action_office LIKE 'E%'instead? -
Problem with Select Statements
Hi All,
I have a performance problem for my report because of the following statements.
How can i modify the select statements for improving the performance of the report.
DATA : shkzg1h LIKE bsad-shkzg,
shkzg1s LIKE bsad-shkzg,
shkzg2h LIKE bsad-shkzg,
shkzg2s LIKE bsad-shkzg,
shkzg1hu LIKE bsad-shkzg,
shkzg1su LIKE bsad-shkzg,
shkzg2hu LIKE bsad-shkzg,
shkzg2su LIKE bsad-shkzg,
kopbal1s LIKE bsad-dmbtr,
kopbal2s LIKE bsad-dmbtr,
kopbal1h LIKE bsad-dmbtr,
kopbal2h LIKE bsad-dmbtr,
kopbal1su LIKE bsad-dmbtr,
kopbal2su LIKE bsad-dmbtr,
kopbal1hu LIKE bsad-dmbtr,
kopbal2hu LIKE bsad-dmbtr.
*These statements are in LOOP.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1s , kopbal1s)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1su , kopbal1su)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1h , kopbal1h)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg1hu , kopbal1hu)
FROM bsid
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2s , kopbal2s)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2su , kopbal2su)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'S'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2h , kopbal2h)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz EQ ''
GROUP BY shkzg.
ENDSELECT.
SELECT shkzg SUM( dmbtr )
INTO (shkzg2hu , kopbal2hu)
FROM bsad
WHERE bukrs = ibukrs
AND kunnr = ktab-kunnr
AND budat < idate-low
AND shkzg = 'H'
AND umskz IN zspgl
GROUP BY shkzg.
ENDSELECT.>
Siegfried Boes wrote:
> Please stop writing answers if you understrand nothing about database SELECTS!
> All above recommendations are pure nonsense!
>
> As always with such questions, you must do an analysis before you ask! The coding itself is perfectly o.k., a SELECT with an aggregate and a GROUP BY can not be changed into a SELECT SINGLE or whatever.
>
> But your SELECTS mustr be supported by indexes!
>
> Please run SQL Trace, and tell us the results:
>
> I see 8 statements, what is the duration and the number of records coming back for each statement?
> Maybe only one statement is slow.
>
> See
> SQL trace:
> /people/siegfried.boes/blog/2007/09/05/the-sql-trace-st05-150-quick-and-easy
>
>
> Siegfried
Nice point there Siegfried. Instead of giving constructive suggestion, people here give a very bad suggestion on using SELECT SINGLE combined with SUM and GROUP BY.
I hope the person already look at your reply before he try using select single and wondering why he has error.
Anyway, the most important thing is how many loop expected for those select statements?
If you have like thousands of loop, you can expect a poor performance.
So, you should also look at how many times the select statement is called and not only performance for each select statement when you're doing SQL trace.
Regards,
Abraham -
Issue with Past Month data in the Select Statement
Hi,
I written the following query,
SELECT /*+NO_MERGE(A)*/
CASE
WHEN snap_shot_date > a.q3
AND snap_shot_date <= a.q4
THEN q4char
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN q3char
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN q2char
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN q1char
END snap_shot_date,
CASE
WHEN snap_shot_date > a.q3 AND snap_shot_date <= a.q4
THEN 1
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN 2
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN 3
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN 4
END sort_by,
pillar3_exposure_class_code, pd_band_description,
ROUND (SUM (p3.ead_post_sec_post_crm)),
ROUND (SUM (notional_principle)),
DECODE (SUM (notional_principle),
0, 0,
ROUND ( SUM (DECODE (exposure_type,
'UNDRAW', ead_post_sec_post_crm,
0
/ SUM (notional_principle),
4
DECODE (SUM (p3.ead_post_sec_post_crm),
0, 0,
ROUND ( SUM (pd_value * p3.ead_post_sec_post_crm)
/ SUM (p3.ead_post_sec_post_crm),
2
DECODE (SUM (p3.ead_post_sec_post_crm),
0, 0,
ROUND (SUM (rwa) / SUM (p3.ead_post_sec_post_crm), 4)
DECODE (SUM (p3.ead_post_sec_post_crm),
0, 0,
ROUND ( SUM (lgd_rate * p3.ead_post_sec_post_crm)
/ SUM (p3.ead_post_sec_post_crm),
2
TO_CHAR (MAX (a.max_date), 'FMMonth DD, YYYY')
FROM summary.pillar3 p3,
(SELECT DISTINCT (month_end_date) max_date,
LAST_DAY (month_end_date) q4,
TO_CHAR (LAST_DAY (month_end_date),
'MON YYYY'
) q4char,
ADD_MONTHS (LAST_DAY (month_end_date), -3) q3,
TO_CHAR
(ADD_MONTHS (LAST_DAY (month_end_date), -3),
'MON YYYY'
) q3char,
ADD_MONTHS (LAST_DAY (month_end_date), -6) q2,
TO_CHAR
(ADD_MONTHS (LAST_DAY (month_end_date), -6),
'MON YYYY'
) q2char,
ADD_MONTHS (LAST_DAY (month_end_date), -9) q1,
TO_CHAR
(ADD_MONTHS (LAST_DAY (month_end_date), -9),
'MON YYYY'
) q1char,
ADD_MONTHS (LAST_DAY (month_end_date), -12) q0
FROM rcdwstg.stg_bcar_detail) a
WHERE snap_shot_date BETWEEN ADD_MONTHS (a.max_date, -12) AND a.max_date
GROUP BY CASE
WHEN snap_shot_date > a.q3 AND snap_shot_date <= a.q4
THEN q4char
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN q3char
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN q2char
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN q1char
END,
CASE
WHEN snap_shot_date > a.q3 AND snap_shot_date <= a.q4
THEN 1
WHEN snap_shot_date > a.q2 AND snap_shot_date <= a.q3
THEN 2
WHEN snap_shot_date > a.q1 AND snap_shot_date <= a.q2
THEN 3
WHEN snap_shot_date > a.q0 AND snap_shot_date <= a.q1
THEN 4
END,
pillar3_exposure_class_code,
pd_band_description
ORDER BY 2 DESC;I have written the query to get the latest 12 months data from a table and split that into 4 quarter to show in the Cognos Report. But when the table having 13th or the past months data, the select statement is showing Empty values in the first two columns and fetching the 13 month data too.
Can anyone help me in this to avoid the problem.
Thanks
Radha KWHERE snap_shot_date BETWEEN ADD_MONTHS(TRUNC(a.max_date, 'MM'), -11) AND a.max_date
.... -
Questions on select statement in the data model
Hi,
In the select statement, the order by statement seems only to work at the lowest level of the select statement. Is this true?
What is the the purpose of the group by statement in the select statement?
As far as I understand, the grouping is catered for in the data model.....
Regards,
KinFrom the oracle report docs:
The order of column values in a default group is
determined by the ORDER BY clause of the query for
SQL queries and by the sort column for Express queries.
For column values in user-created groups, however, you
must use Break Order to specify how to order the break
column's values.So order by in your sql query is affected by groups in your data model.
The purpose of a group by statement in your select statement is to group values for aggregation purposes, like
select department_id,
min(salary),
max (salary)
from employees
group
by department_id
order
by department_id;Grouping in the data model of a report is a little different than a group by clause, but not entirely. -
Index not being used in Select statement
Friends
I have the following SQL statement:
SELECT
a.acct,
a.date_field,
UPPER(b.feegroup) feegrp,
SUM (a.fee1) fee1,
SUM (a.fee2) fee2,
SUM (a.fee3) fee3
FROM table1 a, table2 b
WHERE 1 = 1
AND a.fee_id = b.fee_id
GROUP BY a.acct, a.date_field, b.feegroup;
Both the tables have index on fee_id column. When I run the explain plan for this statement, I am getting the following output:
Operation | Option | Object Name | Position
SELECT STATEMENT | | | 560299
HASH | GROUP BY| |1
TABLE ACCESS | FULL| table2 | 1
TABLE ACCESS | FULL| table1 | 2
Why Oracle is not using the index?
Edited by: darshilm on Dec 10, 2009 3:56 PMThe proposed plan is the optimal according to your current conditions in the "where clause" where you have only the equality join condition and therefore the CBO can use HASH JOIN. Using any kind of index access would just increase the amount of required work unless you would add some very restrictive conditions which will select rows from relatively small amount of blocks. Here I have to mention that what really counts in the CBO cost calculation is the amount of blocks accessed and not the number of rows. The "currency" for I/O in Oracle is a block and not a row. CBO always uses an assumption that there is nothing in the buffer cache and it will have to perform a physical read for every block.
How many blocks will actually be accessed depends on the data distribution. It can happen that every single row that you have to retrieve resides in a different block and although you access only 1000 rows out of a million row table you would have to visit almost every block of that table. For such a situation a FULL TABLE SCAN is the best access path and Oracle will use multiblock I/O for that. On the other side you can have those 1000 rows only in a few blocks and then the index access would be the most appropriate one. For index access Oracle uses single block I/O. Usually the actual situation is somewhere between this two extreme situations. But you can run some tests by yourself and see when an index access will be replaced by a full table scan while you will make your predicates less selective.
HTH, Joze
Co-author of the forthcoming book "Expert Oracle Practices"
http://www.apress.com/book/view/9781430226680
Oracle related blog: http://joze-senegacnik.blogspot.com/
Blog about flying: http://jsenegacnik.blogspot.com/
Blog about Building Ovens, Baking and Cooking: http://senegacnik.blogspot.com
Maybe you are looking for
-
please if you can give me an example
-
No Images Available In Boot Camp
I have Mac Book Air 13 Inch with I7 and 8 GB Ram, I downloaded the windows 8.1 Pro ISO and using Boot Camp to Install. I successfully created Boot able USB with Windows 8.1 operating system using Boot Camp, But I'm getting No Images Available Screen
-
Is there any way to export a FR report to a HTML file? In workspace you can see FR reports as HTML, but I can't see a way to export it. We would like to export the FR report and publish it on a server that don't have workspace. I know that one way co
-
Port forwarding to ip address only allows input of last digits
I have the Linksys BEFW11S4 router and am trying to port forward to a static ip address but the port forwarding screen is hard coded to 192.168.1.__ and only allows me to enter the last 3 digits of the quartet. Does anyone know how I can get past thi
-
Is it possible to disable data on the iphone 3G or 3GS completely?
When I travel I want to get a local prepaid sim card and use my phone but everywhere I've been outside the US data is such an incredible ripoff that it basically can't be used (read: dollars per megabyte). If even dare to insert a foreign prepaid sim