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
Similar Messages
-
Count(*) , group by with multiple columns from multiple tables involved
Hi all,
I am relatively new to SQL.
Currently I have these few requirements, to display quite a number of fields from 3 tables for display of report.
In my query I need to:
1.) count(*)
2.) select quite a number of fields from table 1,2,3
However when count(*) is used, grouped by has to be used to.
How do I actually use group by with so many columns to be selected?
I have actually used the query below, but the count(*) returns 1, the correct output should be 3 instead.
select count(*), table1.col1, table1.col2, table1.col3, table2.col3, table2.col4, table2.col6, table3.col1, table3.col4, table3.col5
from table1, table2, table3
where
<conditions>........................
group by table1.col1, table1.col2, table1.col3, table2.col3, table2.col4, table2.col6, table3.col1, table3.col4, table3.col5
I know this group by statement looks very unrefined. How can I select multiple fields from different tables, and yet get the count(*) correctly?
Thank you so much for your time.Hmm yes it actually does return count as 1 for each row. But there are 3 rows returned. E.g.
ctr table1.col1 table1.col2 ..........
1 value1 value1
1 value2 value3
1 value3 value4
If I put the count(*) outside, it returns 3 , the correct output
ctr
3
select count(*) from
select table1.col1, table1.col2, table1.col3, table2.col3, table2.col4, table2.col6, table3.col1, table3.col4, table3.col5
from table1, table2, table3
where
<conditions>
group by table1.col1, table1.col2, table1.col3, table2.col3, table2.col4, table2.col6, table3.col1, table3.col4, table3.col5
Thus I was wondering if it was the group by of multiple colns that resulted in the count stucked at value 1. -
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 -
Repeat group header in multiple columns
I have a group and the details section is set to format with multiple columns. I have the repeat group header on each page option checked but I also want it to repeat group header if the group spans multiple columns. Does anyone know how to do this?
Thanks for your reply but I'm not sure how you copy a group header into another column if the data that is displayed there is dynamically displayed. Please advise. Thanks
-
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.
-
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 -
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. -
PIVOT with multiple columns to add and multiple levels of grouping
Hi friends,
I got a table with the columns in the form of:
CRITERIA_A,
CRITERIA_B,
CRITERIA_C,
AMOUNT_A,
AMOUNT_B,
AMOUNT_C,
AMOUNT_D
Any way to design a pivot to present the table reflecting:
Sums of Amount_A, Amount_B, Amount_C, Amount_D
For Rows reflecting grouping levels on:
Criteria_B, Criteria_C
and Columns breakup for:
Criteria_A?
An example like:
Criteria_A1
Criteria_A2
Criteria_A3
Criteria_A4
Row Labels
Sum of Amount_A
Sum of Amount_B
Sum of Amount_C
Sum of Amount_D
Sum of Amount_A
Sum of Amount_B
Sum of Amount_C
Sum of Amount_D
Sum of Amount_A
Sum of Amount_B
Sum of Amount_C
Sum of Amount_D
Sum of Amount_A
Criteria_B3
94
107
36
127
84
132
41
176
24
16
67
29
38
Criteria_C1
24
25
5
49
14
66
5
49
24
16
67
29
Criteria_C2
70
82
31
78
38
Criteria_C3
38
41
31
78
Criteria_C4
32
25
5
49
Criteria_B1
56
142
78
26
32
25
67
8
24
Criteria_C2
24
66
26
8
32
25
67
8
Criteria_C3
32
76
52
18
Criteria_C4
24
Criteria_B2
162
309
264
81
132
230
155
124
14
25
52
8
38
Criteria_C1
38
76
26
8
62
98
98
66
14
25
52
8
38
Criteria_C2
86
157
186
44
70
132
57
58
Criteria_C3
38
76
52
29
Criteria_B4
100
148
130
116
56
142
93
57
46
Criteria_C1
24
66
67
49
32
Criteria_C2
32
76
26
8
14
Criteria_C3
76
82
78
67
Criteria_C4
24
66
52
49
Grand Total
312
558
378
234
348
535
393
424
94
183
212
94
146
Thanx in advance, Best Regards, Faraz A QureshiSounds like this to me if in T-SQL query
(best guess based on what you posted)
SELECT
CRITERIA_B,
CRITERIA_C,
SUM(CASE WHEN CRITERIA_A = 'Critera_A1' THEN AMOUNT_A ELSE 0 END) AS CRITERIA_A1_AMOUNT_A,
SUM(CASE WHEN CRITERIA_A = 'Critera_A1' THEN AMOUNT_B ELSE 0 END) AS CRITERIA_A1_AMOUNT_B,
SUM(CASE WHEN CRITERIA_A = 'Critera_A1' THEN AMOUNT_C ELSE 0 END) AS CRITERIA_A1_AMOUNT_C,
SUM(CASE WHEN CRITERIA_A = 'Critera_A2' THEN AMOUNT_A ELSE 0 END) AS CRITERIA_A2_AMOUNT_A,
SUM(CASE WHEN CRITERIA_A = 'Critera_A2' THEN AMOUNT_B ELSE 0 END) AS CRITERIA_A2_AMOUNT_B,
SUM(CASE WHEN CRITERIA_A = 'Critera_A2' THEN AMOUNT_C ELSE 0 END) AS CRITERIA_A2_AMOUNT_C,
SUM(CASE WHEN CRITERIA_A = 'Critera_AN' THEN AMOUNT_A ELSE 0 END) AS CRITERIA_AN_AMOUNT_A,
SUM(CASE WHEN CRITERIA_A = 'Critera_AN' THEN AMOUNT_B ELSE 0 END) AS CRITERIA_AN_AMOUNT_B,
SUM(CASE WHEN CRITERIA_A = 'Critera_AN' THEN AMOUNT_C ELSE 0 END) AS CRITERIA_AN_AMOUNT_C,
FROM Table
GROUP BY CRITERIA_B,
CRITERIA_C
to make it dynamic see
http://beyondrelational.com/modules/2/blogs/70/posts/10791/dynamic-crosstab-with-multiple-pivot-columns.aspx
However it would be much easier to build this in SSRS reports using matrix if you can use it.
In that case just bring data as is
then add a matrix to report
Add column grouping on Criteria A and Row grouping on Criteria B and then Criteria C .
In data portion add three columns with expressions as
=SUM(Fields!Amount_A.Value)
=SUM(Fields!Amount_B.Value)
=SUM(Fields!Amount_C.Value)
then you will get exact format what you're asking for
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page -
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 -
Convert multiple rows to one row with multiple columns
Hi
i have a table Match_1 with 2 columns 'Source' and 'target'.One source can have multiple targets and that number could be anything
CREATE TABLE Match_1
Source CHAR(1),
Target CHAR(1)
INSERT INTO Match_1 VALUES ('A', 'B');
INSERT INTO Match_1 VALUES ('A', 'C');
INSERT INTO Match_1 VALUES ('A', 'D');
INSERT INTO Match_1 VALUES ('A', 'E');
INSERT INTO Match_1 VALUES ('V', 'X');
INSERT INTO Match_1 VALUES ('V', 'Y');
INSERT INTO Match_1 VALUES ('V', 'X');
INSERT INTO Match_1 VALUES ('V', 'W');
COMMIT;
i need to get my output in the below format
Source target 1 target 2 target 3 target 4 target n
A B C D
V X Y Z W
Could you please provide me the required SQL.
REgards
-LearnsequelWhat is your database version (4 digit) ? also my example won't be generate columns for your information:
it will produce a result like that :
select source, listagg(target,',') within group (order by target)
from match_1
group by source;
A B,C,D,E
V W,X,X,Yps: in previos post, I wrote "with" word wrong in sql. -
How to link Excel Chart with multiple columns dynamically?
Hi all,
I have one problem. I have an excel chart which has to read data from multiple columns for it's X-axis values (time stamps) and the same number of values for it's Y-axis from multiple columns. See figure attached.
Now, you can do this by manually assigning values by holding control key to tell the chart that data from many columns is continued from the first column. i.e, a long data is distributed along may columns. This is done very easily manually.
Now, how to do it at runtime through LabVIEW 8.0?
Although, I have done it through labview when there where only two columns, A and B streching up to any length. But, now to save space we are dumping into many columns in the same page and want the chart plot that data as we dump it.
It is tough since I don't know how to assign SourceData of the chart through labview.
I have attached chart's SourceData picture.
Hope you all can solve my problem.
The Y-axis values are: =(good!$B$30:$B$70,good!$D$30:$D$70,good!$F$30:$F$70,good!$H$30:$H$70,good!$B$71:$B$140,good!$D$71:$D$140,good!$F$71:$F$140,good!$H$71:$H$140,good!$B$141:$B$189)
good is the name of the file.
The X- axis values are: =(good!$A$30:$A$70,good!$C$30:$C$70,good!$E$30:$E$70,good!$G$30:$G$70,good!$A$71:$A$140,good!$C$71:$C$140,good!$E$71:$E$140,good!$G$71:$G$140,good!$A$141:$A$189)
See the columns vary from A to F.
I have still not got what I love.....
Attachments:
ExcelChart.JPG 113 KB
SourceData.JPG 34 KB
XY values.JPG 36 KBOk i solved the excel chart problem. Here is the figure showing my victory! But offcourse with NI forum help.
Message Edited by Halemani on 09-19-2008 06:33 AM
I have still not got what I love.....
Attachments:
Chart_XValues_Values.JPG 36 KB
Maybe you are looking for
-
[IDOC] WE19 send successfully ...but where it is?
Hi, creating scenario IDOC -> XI -> File with own IDOC. After sending it with we19 he said "IDoc sent to SAP system or external program". In we02 and we05 the IDOC it says "Data passed to port OK". Business Systems: Sender : SAPBS Client 200 Receiver
-
How do I save more than one photo album on my iphone?
-
N95 Slidebar have got to big separation and it's "...
N95 Slidebar have got to big separation and it's "clicking" when i'm pushing (for example red button) before this button "click". It's normal? In N80 slidebar is working without any "clicks". N95 [Nokia -> please! can you fix reduce reduced support o
-
My old e-mail address was SHAW XXXXX.and worked very well with Thunder Bird mail. I recently switched to new ISP Bell and my new address is XXXXXX However with several attempts with techs ,Thunder Bird will not work with my new ISP. I had to start us
-
Espace insécable/nonbreaking space and nonbreaking space (hunting fix)
Hello, I have a little problem, my non-breaking space and its fixed after verification that the many non-breaking space normal, is there a preference or configuration setting in my paragraph styles or character that could influence the non-breaking s