Decode with multiple column sub query
Dear's
What is the wrong with the query
select * from common_master
where
(company_code,lpad(rtrim(ltrim(bu_code)),12,' ')) IN
decode ('ADMIN','ADMIN',(select company_code ,lpad(rtrim(ltrim(bu_code)),12,' ') from common_master )
,(select company_code ,lpad(rtrim(ltrim(bu_code)),12,' ') from user_system_privs) )
The error message as
Error starting at line 16 in command:
select * from common_master
where
(company_code,lpad(rtrim(ltrim(bu_code)),12,' ')) IN
decode ('ADMIN','ADMIN',(select company_code ,lpad(rtrim(ltrim(bu_code)),12,' ') from common_master )
,(select company_code ,lpad(rtrim(ltrim(bu_code)),12,' ') from user_system_privs) )
Error at Command Line:18 Column:53
Error report:
SQL Error: ORA-00920: invalid relational operator
00920. 00000 - "invalid relational operator"
*Cause:
*Action:
Dennis
You can club the columns in where clause and in decode
like
select * from common_master
where
(company_code || lpad(rtrim(ltrim(bu_code)) || to_char(12) ||' ') IN
( select decode ('ADMIN','ADMIN',company_code || lpad(rtrim(ltrim(bu_code)) || to_char(12) || ' ') from common_master ),
,company_code || lpad(rtrim(ltrim(bu_code)) || to_char(12) ||' ') from user_system_privs) )
HTH,
~Yogesh
Similar Messages
-
The issue with using the multiple columns sub-query in WHERE clause
Hi All,
my database version is 10.2.
the problem i am trying to deal with is that when I use multiple column sub-query in the WHERE clause of the SELECT statement, the actual row number returned from the sub-query is different from the whole statement.
And what I found is that, whenever there is NULL in any of those columns returned from the SUB-QUERY, the outer query will just return NULL for that whole row.
Here is an example:
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t;
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT NULL 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10
select * from EMP where (EMPNO, ENAME, job, MGR, HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, mgr, hiredate, sal, deptno from EMP
intersect
select empno, ename, job, mgr, hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 If I specifically deal with the NULL situations for the columns which might return NULL, I can get the result right.
select * from EMP where (EMPNO, ENAME, job, NVL(MGR,-1), HIREDATE, SAL, DEPTNO) in (
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from EMP
intersect
select empno, ename, job, nvl(mgr,-1), hiredate, sal, deptno from t);
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT null 17-NOV-81 5000 10
7934 MILLER CLERK 7782 23-JAN-82 1300 10 the problem is that, I feel this is a very lame way of handling it.
So, I wonder or am asking if there is any better or standard way to do it?
any help would be highly appreciated.
ThanksHi,
As you discovered, INTERSECT treats NULL as a value, but IN does not.
What you did with NVL is one way to handle the situation. If there was a chance that any of the columns could be NULL, then you might prefer something like this:
select *
from EMP
where ( EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
) in (
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from emp
intersect
select EMPNO || '~' ||
ENAME || '~' ||
job || '~' ||
MGR || '~' ||
TO_CHAR (HIREDATE, 'DD-MON-YYYY HH24:MI:SS')
|| '~' ||
SAL || '~' ||
DEPTNO
from t
);This assumes that you can identify some string (I used '~') that never occurs in the strings in these tables.
This is implicitly converting the NUMBERs. That's usually not a good thing to do. but explicitly converting them would make this even more tedious.
You should explicitly convert any DATEs to strings, however. Depending on your default format, and your data, you might get away with implicit conversions even for DATEs, but don't bet on it.
If you had to do this often, you might consider writing a user-defined function:
delimited_string (empno, ename, job, mgr, hiredate, sal, deptno) would return a string like
'7839~KING~PRESIDENT~~17-NOV-1981~5000~10'
This will make the coding easier, but beware: it will make the execution slower. -
GROUP BY with multiple columns.
I have little query about GROUP BY clause.
Sometimes we will give multiple column names in GROUP BY .If it behaves in the same way as ORDER BY (multiple columns) i would love to have an example where I can use GROUP BY multiple columns. Anyways howsoever it behaves it will be really appreciated if anyone can explain a scenario where GROUP BY with multiple columns can be used.
I know about the usage of aggregate functions with GROUP BY but using only one column.
Thanks..!!!Hi,
Group by is used for grouping of same records. check this bellow scenario
SELECT registrationid, dateofbirth, age FROM prawin62 WHERE registrationid = 22;
22 1/23/1975 54
22 5/18/2011 66
22 5/18/2011 66
22 5/18/2011 66
22 5/18/2011 66
22 5/18/2011 66SELECT registrationid, sum(age) FROM prawin62 WHERE registrationid = 22 GROUP BY registrationid;
22 384SELECT registrationid, dateofbirth, sum(age) FROM prawin62 WHERE registrationid = 22 GROUP BY registrationid, dateofbirth ;
22 5/18/2011 330
22 1/23/1975 54Thanks,
Praveen -
Post Author: vman
CA Forum: General
I am working a report that has had the details section format with multiple columns selected. I want to change the report and remove the check box, but it is grayed out. How can I get the report details section back to default without the detail size constraints?Post Author: rcoleman
CA Forum: General
Bearing in mind that I'm a relative newbie to the report writing/Crystal world...the only thing I can think of is that maybe the report was written using an older version of Crystal and this option wasn't available. Another possibility, if it's a sub-report, you might try making a copy of the sub-report saving it with the same name, deleting the old sub-report, replacing it with the copy you made. I know that has worked for me when I've had problems with the re-import when opening is greyed out. -
Repeat "Detail a" section when using "Format with Multiple Columns" layout
I am using Crystal Reports for .NET (VS 2008) and I would like to get the "Detail a" section to repeat at top of a second column containing additional data in "Detail b" section. Please see below for example, specifically "Detail a (2)". Any tips or help much appreciated.
Detail a (1)_____Detail a (2)
Detail b (1-1)___Detail b (2-3)
Detail b (1-2)___Detail b (2-4)
--Detail b (1-3)___Detail a (3)
Detail a (2)______--Detail b (3-1)
--Detail b (2-1)
--Detail b (2-2)
Thx, Scottthe format with multiple columns i dont believe will show you the value 2x, but allows you to display the values across instead of down.
i think you may want to use a sub report -
Creating Select List with multiple columns
I want to create a select list with multiple columns. I followed the demo application model described in the by Denes Kubicek (Currently my reference for APEX !!)
The code is as follows:
CREATE OR REPLACE FUNCTION getcrops(p_cropid IN NUMBER)
RETURN VARCHAR2
IS
v_cropid VARCHAR2 (400);
v_fcode VARCHAR2 (400);
v_product VARCHAR2 (400);
v_var VARCHAR2 (400);
v_fname VARCHAR2 (400);
v_acres VARCHAR2 (400);
v_style_start VARCHAR2 (400);
v_style_end VARCHAR2 (400);
v_return VARCHAR2 (400);
BEGIN
FOR c IN (select "CROP"."CROPID" as "CROP ID",
"CROP"."FIELDCODE" as "FIELD CODE",
"CARROTPRODUCTLIST"."CARROTPRODUCTNAME" as "PRODUCT",
"VARIETYLIST"."VARIETYNAME" as "VARIETY",
"FIELD"."FIELDNAME" as "FIELD NAME",
"CROP"."SIZEINACRES" as "ACRES"
from "FIELD" "FIELD",
"CARROTPRODUCTLIST" "CARROTPRODUCTLIST",
"VARIETYLIST" "VARIETYLIST",
"CROP" "CROP"
where "CROP"."CARROTPRODUCTTYPE"="CARROTPRODUCTLIST"."CARROTPRODUCTID"
and "CROP"."VARIETYID"="VARIETYLIST"."VARIETYLISTID"
and "CROP"."FIELDID"="FIELD"."FIELDID")
LOOP
v_cropid := TO_CHAR (c.'CROP ID', 'FML999G999G999G999G990');
v_fcode := c.'FIELD CODE';
v_product := c.'PRODUCT';
v_var := c.'VARIETY';
v_fname :=c.'FIELD NAME';
v_acres :=c.'ACRES';
FOR i IN 1 .. 12 - LENGTH (c."CROP ID")
LOOP
v_cropid := v_cropid || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'FIELD CODE')
LOOP
v_fcode := v_fcode || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'PRODUCT')
LOOP
v_product := v_product || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'VARIETY')
LOOP
v_var := v_var || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'FIELD NAME')
LOOP
v_fname := v_fname || ' ';
END LOOP;
FOR i IN 1 .. 12 - LENGTH (c.'ACRES')
LOOP
v_acres := v_acres || ' ';
END LOOP;
v_return := v_cropid || v_fcode || v_product || v_var || v_fname || v_acres;
END LOOP;
RETURN v_return;
END;
I created this anonymous Pl/SQL function at a application level ..Then I followed up to create a select list with a function inside. However I could not create select list with the command suggested in the demo which is
select getcrops(cropid) d, cropid r from crop;
APEX (2.1) returns a error message. What am I missing ? Should the function be called from somewhere else after creating a regular select list..? Where the functions (Pl/SQL) should be ideally stored for application level access..? and for across application level access ? How can I check the function works good. Help is appreciated.Still really one column.
If they need to be independent then you've got to have three selection lists.
% -
Selection list with multiple columns
How can i create a selection list with multiple columns?
Still really one column.
If they need to be independent then you've got to have three selection lists.
% -
Select list with multiple columns
I want to create a select list with multiple column option. What are the options. Any developments in apex 3.0.1 ? Thanks in advance. I tried the tutorials by Kubicek.
I am kind of stuck to see the function entry inside LOV definition returns a error.Still really one column.
If they need to be independent then you've got to have three selection lists.
% -
hello all ,
how to create a list with multiple columns ? can anyone share some examples or code snippetsWhat I would usually do is create a movieclip/sprite that contains a row with every column in it (each column being a textfield). Then pile up the rows one atop the other (using Actionscript) as they are populated with data.
-
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!Hi,
fac30d8e-74d3-42aa-b643-e30a3780e00f wrote:
Dear Sir/Madam
I'm a student who is interested in Oracle Database and
I have some problems with JOIN and Sub-query.
I hope so many of you could help me.
if i use JOIN without sub-query, may it be faster or not?
SELECT field1, field2 FROM tableA INNER JOIN tableB
if i use JOIN with sub-query, may it be faster or not?
SELECT field1,field2,field3 FROM tableA INNER JOIN (SELECT field1,field2 FROM tableB)
Thanks in advance!
As the others have said, the execution plan will give you a better idea about which is faster.
If you're trying to see how using (or not using) a sub-query affects performance, make the rest of the queries as similar as possible. For example, include field3 in both queries, or ignore field3 in both queries.
In this particular case, I guess the optimizer would do the same thing either way, but that's just a guess. I can't see your execution plans.
In general, simpler code is faster, and better in other ways, too. In this case
tableB
is simpler than
(SELECT field1, field2 FROM tableB)
Why do you want a sub-query in this example? -
Selectively Format Groups with Multiple Columns
I have a bit of an obscure task I've been trying to hammer out to no avail. I've searched quite a bit but can't seem to find anyone else who has attempted this. I have two group by statements, one is a category and the other is a rank. What I'm trying to do is organize the report so that there are category rows and below each category row is a set of columns for each rank with their associated values as such:
category1
rank1 rank2 rank3 rank4
value1 value1 value1 value1
value2 value2 value2 value2
category2
rank1 rank2 rank3
value1 value1 value1
value2 value2 value2
I've tried using details>Section Expert>>Layout>Format Groups with multiple column. Unfortunately, this applies to both groups putting the categories into columns instead of rows, making a mess. Does anyone know of a way to selectively put groups into columns and ensure alignment? In the above example the user would be able to compare rank values from multiple categories since they all line up. Any help with this would be very much appreciated.Hi,
Have you tried using a crosstab? Give this a shot:
1) Group the report on Category
2) Create a new group header section. Group Header b
3) Place a crosstab in this section
4) The columns would be the Rank field, I'm not sure about what you would like to show in the rows
5) The summarized field would be the Value field ofcourse
Let me know how this goes.
-Abhilash -
Treeview with multiple columns
I googled alot about creating a treeview with multiple columns similar to a listview in details mode with the added benefit of nodes and roots. I got some answers but I was wondering if visual 2008 itself has such an option, and if not what is my best
choice?
Debra has a questionI just wanted to know if in the visual studio 2008 itself there is an option to have a multi column treeview or a listview with nodes...
Debra has a question
Hi Debra,
>> a multi column treeview
As far as I know, the standard Microsoft .NET TreeView does not support columns, and if you want a tree control that supports columns, you could turn to the link from Tony:
#TreeView with Columns
http://www.codeproject.com/Articles/23746/TreeView-with-Columns
>>a listview with nodes
I made a research about “a listview with nodes” and found no results. I checked the ListView Class and did not find information about nodes.
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a
href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey. -
Top N Analysis with multiple columns
Hi
I am using Oracle 9i. I do have a table which contains datewise promotional material types for an organisation.
The structure is as follows:
CREATE TABLE TEST
(CDATE DATE,
BROCHURE VARCHAR2(1),
WEBSITE VARCHAR2(1),
DIRECT_MAIL VARCHAR2(1),
PRESS_RELEASE VARCHAR2(1),
JOURNAL_AD VARCHAR2(1)
and the sample data is as follows:
CDate Brochure Website Direct_Mail Press_Release Journal_Ad
01/04/1996 Y Y Y N N
02/04/1996 Y Y N N N
23/06/1996 Y N Y Y N
13/09/1996 Y Y N N N
01/04/1997 Y Y N N N
02/04/1997 Y Y Y N Y
23/06/1997 N Y N N Y
13/09/1997 Y Y N N N
01/04/1998 Y Y Y N N
02/04/1998 Y N N Y N
23/06/1998 N Y N N Y
13/09/1998 Y Y N N Y
01/04/1999 Y Y Y N Y
02/04/1999 Y N N Y N
23/06/1999 N Y N N N
13/09/1999 Y Y Y N N
I want to have year wise top 4 promotional types in terms of count of 'Y' only. The result should be like as follows:
YEAR:1996
TYPE COUNT
BROCHURE 4
WEBSITE 3
DIRECT_MAIL 2
PRESS_RELEASE 1
JOURNAL_AD 0
YEAR:1997
TYPE COUNT
WEBSITE 4
BROCHURE 3
JOURNAL_AD 2
DIRECT_MAIL 1
PRESS_RELEASE 1
Please suggest a solution for the same. I am not able to sort it for multiple columns.
Regards
MSOne of the questions that must be asked when you have a requirement to only show the top N ranked items in a list, is "what about a tie in the ranking?".
Oracle has two ranking functions that allow you to deal with either requirement - RANK and DENSE_RANK. Both operate as either analytic or aggregate functions, so either will work for your requirements. The previous posting by Miguel demonstrated how to decode your Y/N flags and pivot the data.
In this example, I've taken the liberty of adding some data to year 2000 that will show the difference between RANK and DENSE_RANK as well as how to use them to filter your results.
First, here's the decoded/pivoted data:
SQL>WITH test AS
2 ( SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
3 UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
4 UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
5 UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
6 UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
7 UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
8 UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
9 UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
10 UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
11 UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
12 UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
13 UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
14 UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
15 UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
16 UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
17 UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
18 UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
19 UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
20 UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
21 UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
22 )
23 SELECT cyear
24 ,ctype
25 ,RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
26 ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
27 FROM (SELECT TRUNC(CDATE,'Y') CYEAR
28 ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
29 FROM test
30 GROUP BY TRUNC(CDATE,'Y')
31 UNION ALL
32 SELECT TRUNC(CDATE,'Y') CYEAR
33 ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
34 FROM test
35 GROUP BY TRUNC(CDATE,'Y')
36 UNION ALL
37 SELECT TRUNC(CDATE,'Y') CYEAR
38 ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
39 FROM test
40 GROUP BY TRUNC(CDATE,'Y')
41 UNION ALL
42 SELECT TRUNC(CDATE,'Y') CYEAR
43 ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
44 FROM test
45 GROUP BY TRUNC(CDATE,'Y')
46 UNION ALL
47 SELECT TRUNC(CDATE,'Y') CYEAR
48 ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
49 FROM test
50 GROUP BY TRUNC(CDATE,'Y')
51 )
52* order by cyear desc, ranking
53 /
CYEAR CTYPE RANKING DENSE_RANKING
01-Jan-2000 00:00:00 BROCHURE 1 1
01-Jan-2000 00:00:00 WEBSITE 1 1
01-Jan-2000 00:00:00 DIRECT_MAIL 3 2
01-Jan-2000 00:00:00 PRESS_RELEASE 4 3
01-Jan-2000 00:00:00 JOURNAL_AD 5 4
01-Jan-1999 00:00:00 BROCHURE 1 1
01-Jan-1999 00:00:00 WEBSITE 1 1
01-Jan-1999 00:00:00 DIRECT_MAIL 3 2
01-Jan-1999 00:00:00 PRESS_RELEASE 4 3
01-Jan-1999 00:00:00 JOURNAL_AD 4 3
01-Jan-1998 00:00:00 BROCHURE 1 1
01-Jan-1998 00:00:00 WEBSITE 1 1
01-Jan-1998 00:00:00 JOURNAL_AD 3 2
01-Jan-1998 00:00:00 DIRECT_MAIL 4 3
01-Jan-1998 00:00:00 PRESS_RELEASE 4 3
01-Jan-1997 00:00:00 WEBSITE 1 1
01-Jan-1997 00:00:00 BROCHURE 2 2
01-Jan-1997 00:00:00 JOURNAL_AD 3 3
01-Jan-1997 00:00:00 DIRECT_MAIL 4 4
01-Jan-1997 00:00:00 PRESS_RELEASE 5 5
01-Jan-1996 00:00:00 BROCHURE 1 1
01-Jan-1996 00:00:00 WEBSITE 2 2
01-Jan-1996 00:00:00 DIRECT_MAIL 3 3
01-Jan-1996 00:00:00 PRESS_RELEASE 4 4
01-Jan-1996 00:00:00 JOURNAL_AD 5 5You can see that in year 2000 there is a tie for first place (ranking #1). The RANK function will name the second highest count 3 (skipping the rank of 2 due to the tie), while the DENSE_RANK function will not skip a ranking.
Now, to filter on the ranking, wrap your query in another in-line view like this - but use which ever ranking function YOUR requirements call for:
SQL>WITH test AS
2 ( SELECT TO_DATE('01/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
3 UNION ALL SELECT TO_DATE('02/04/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
4 UNION ALL SELECT TO_DATE('23/06/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
5 UNION ALL SELECT TO_DATE('13/09/1996','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
6 UNION ALL SELECT TO_DATE('01/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
7 UNION ALL SELECT TO_DATE('02/04/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
8 UNION ALL SELECT TO_DATE('23/06/1997','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
9 UNION ALL SELECT TO_DATE('13/09/1997','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
10 UNION ALL SELECT TO_DATE('01/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
11 UNION ALL SELECT TO_DATE('02/04/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
12 UNION ALL SELECT TO_DATE('23/06/1998','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
13 UNION ALL SELECT TO_DATE('13/09/1998','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
14 UNION ALL SELECT TO_DATE('01/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
15 UNION ALL SELECT TO_DATE('02/04/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'N' AS WEBSITE, 'N' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
16 UNION ALL SELECT TO_DATE('23/06/1999','dd/mm/yyyy') AS CDATE, 'N' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
17 UNION ALL SELECT TO_DATE('13/09/1999','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
18 UNION ALL SELECT TO_DATE('01/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'Y' AS JOURNAL_AD FROM DUAL
19 UNION ALL SELECT TO_DATE('02/04/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
20 UNION ALL SELECT TO_DATE('23/06/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'N' AS DIRECT_MAIL, 'N' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
21 UNION ALL SELECT TO_DATE('13/09/2000','dd/mm/yyyy') AS CDATE, 'Y' AS BROCHURE, 'Y' AS WEBSITE, 'Y' AS DIRECT_MAIL, 'Y' AS PRESS_RELEASE, 'N' AS JOURNAL_AD FROM DUAL
22 )
23 SELECT * FROM (
24 SELECT cyear
25 ,ctype
26 ,RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) ranking
27 ,DENSE_RANK() OVER (PARTITION BY cyear ORDER BY num_media DESC) dense_ranking
28 FROM (SELECT TRUNC(CDATE,'Y') CYEAR
29 ,'BROCHURE' CTYPE, SUM(DECODE(BROCHURE, 'Y', 1, 0)) NUM_MEDIA
30 FROM test
31 GROUP BY TRUNC(CDATE,'Y')
32 UNION ALL
33 SELECT TRUNC(CDATE,'Y') CYEAR
34 ,'WEBSITE' CTYPE, SUM(DECODE(WEBSITE, 'Y', 1, 0))
35 FROM test
36 GROUP BY TRUNC(CDATE,'Y')
37 UNION ALL
38 SELECT TRUNC(CDATE,'Y') CYEAR
39 ,'DIRECT_MAIL' CTYPE, SUM(DECODE(DIRECT_MAIL, 'Y', 1, 0))
40 FROM test
41 GROUP BY TRUNC(CDATE,'Y')
42 UNION ALL
43 SELECT TRUNC(CDATE,'Y') CYEAR
44 ,'PRESS_RELEASE' CTYPE, SUM(DECODE(PRESS_RELEASE, 'Y', 1, 0))
45 FROM test
46 GROUP BY TRUNC(CDATE,'Y')
47 UNION ALL
48 SELECT TRUNC(CDATE,'Y') CYEAR
49 ,'JOURNAL_AD' CTYPE, SUM(DECODE(JOURNAL_AD, 'Y', 1, 0))
50 FROM test
51 GROUP BY TRUNC(CDATE,'Y')
52 )
53 )
54 where RANKING <= 4
55* order by cyear desc, ranking
56 /
CYEAR CTYPE RANKING DENSE_RANKING
01-Jan-2000 00:00:00 WEBSITE 1 1
01-Jan-2000 00:00:00 BROCHURE 1 1
01-Jan-2000 00:00:00 DIRECT_MAIL 3 2
01-Jan-2000 00:00:00 PRESS_RELEASE 4 3
01-Jan-1999 00:00:00 BROCHURE 1 1
01-Jan-1999 00:00:00 WEBSITE 1 1
01-Jan-1999 00:00:00 DIRECT_MAIL 3 2
01-Jan-1999 00:00:00 JOURNAL_AD 4 3
01-Jan-1999 00:00:00 PRESS_RELEASE 4 3
01-Jan-1998 00:00:00 BROCHURE 1 1
01-Jan-1998 00:00:00 WEBSITE 1 1
01-Jan-1998 00:00:00 JOURNAL_AD 3 2
01-Jan-1998 00:00:00 PRESS_RELEASE 4 3
01-Jan-1998 00:00:00 DIRECT_MAIL 4 3
01-Jan-1997 00:00:00 WEBSITE 1 1
01-Jan-1997 00:00:00 BROCHURE 2 2
01-Jan-1997 00:00:00 JOURNAL_AD 3 3
01-Jan-1997 00:00:00 DIRECT_MAIL 4 4
01-Jan-1996 00:00:00 BROCHURE 1 1
01-Jan-1996 00:00:00 WEBSITE 2 2
01-Jan-1996 00:00:00 DIRECT_MAIL 3 3
01-Jan-1996 00:00:00 PRESS_RELEASE 4 4 -
Hi,
I am facing some problems in teaching Multiple column subqueries and also ig u'll could with some example show me how to use sub-query using from clause??Essentially you are using subqueries within the FROM clause.
Here is an example of an embedded view (i.e. view-on-the-fly):
select a.person_id, a.fname, a.lname,
c.job_id
from person a,
(select b.person_id, b.fname,
b.lname, b.job_id
from jobs b) c
where a.person_id = c.person_id -
Outter-Join with requirement for sub-query
I am working on a view that needs to return 1 record per payment date within the primary payment table.
Part of the information that is to be returned in this row comes from a date-controlled table containing superannuation information. This data may or may not be present, requiring an outer-join. The final problem is that there may be multiple rows in this superannuation table requiring me to retrieve the record with the most recent start date, prior to the payment date. This is where I'm breaking down currently as I cannot outer-join to a sub-query.
I had an idea that I could create an inline view of the superannuation table with 1 row for each of the last 365 days (The reports that will be built off the view are always run within 1-2 months of the payment date), with the date and either the required information or blanks if it did not exist. This would avoid me requiring an outer-join as I could just join on the payment date to the date in the inline query and return whatever data was there.
I'm pretty sure I should be able to do this with analytics rather than creating a date table, by restricting the rows to 365, then havign a column that is effectively the previous column - 1. Unfortunately I'm fairly new to analytics and find the Oracle documentation hard to decipher.
Can anyone help with this or perhaps suggest an alternate solution?please don't do that. that's the most pathetic way generate rows. all_objects is a very nasty view, and oracle reserves the right to merge the view into the rest of your query, making performance less than you had hoped for. how about this instead:
select ...
from (
select trunc(sysdate-rownum+1) super_date
from dual
connect by level < = 365 ) dates,
your_table_here
where dates.super_date = your_column (+)
...
Maybe you are looking for
-
Error accessing reporting services - DCOM
Hello, after a (somewhat) smooth installation of SCCM 2007 SP1 on Win2k8 server i'm noticing a problem accessing reporting site , i've seen this issue in a lot of tek tips sites pointing to DCOM permissions, however the fixes recommended don't seem t
-
I forgot the security questions and also the password (and even account id, sequrity questions,... becouse I didn't use for a long time) for security info email of my apple id. Now I want to change the security info email with another. What Should I
-
I am running a new G5 Quad Core server serving a webpage. The webservices appears to have crashed and will not start up. I have look at the logs and this is what appears Processing config file: /etc/httpd/sites/000110.0.0.250_80xxxxxxx.co.uk.conf Pro
-
Can a 256 bit encrypted disk image be read by Tiger?
I have computers running Leopard, Tiger and Panther. If I create a 256 bit encrypted disk image in Leopard, will the image be able to mount and used for reading and writing in Tiger and or Panther? Or should I use 128 bit encryption in a mixed OS X e
-
I have to re-install, but first I ran Disk Utility to repair anything that needed fixing. It reported these errors: "Invalid B-tree node size," volume check failed," and "The underlying task reported failure on exit (-9972)," none of which it could r