Discussions
Categories
- 385.5K All Categories
- 5.1K Data
- 2.5K Big Data Appliance
- 2.5K Data Science
- 453.4K Databases
- 223.2K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 47 Multilingual Engine
- 606 MySQL Community Space
- 486 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.2K ORDS, SODA & JSON in the Database
- 585 SQLcl
- 4K SQL Developer Data Modeler
- 188K SQL & PL/SQL
- 21.5K SQL Developer
- 46 Data Integration
- 46 GoldenGate
- 298.4K Development
- 4 Application Development
- 20 Developer Projects
- 166 Programming Languages
- 295K Development Tools
- 150 DevOps
- 3.1K QA/Testing
- 646.7K Java
- 37 Java Learning Subscription
- 37.1K Database Connectivity
- 201 Java Community Process
- 108 Java 25
- 22.2K Java APIs
- 138.3K Java Development Tools
- 165.4K Java EE (Java Enterprise Edition)
- 22 Java Essentials
- 176 Java 8 Questions
- 86K Java Programming
- 82 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 208 Java User Groups
- 25 JavaScript - Nashorn
- Programs
- 667 LiveLabs
- 41 Workshops
- 10.3K Software
- 6.7K Berkeley DB Family
- 3.6K JHeadstart
- 6K Other Languages
- 2.3K Chinese
- 207 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 474 Portuguese
Sql Query for reporting

Hi,
Please help me to retrieve the Non nullable data for each column based on the vendor_rank column, Partitioned by Pricing num.
Refer the attached screenshot:
Sample script:
CREATE TABLE TEST_RANK
(PRICING_NUMBER NUMBER(9),
NAME VARCHAR(30),
LAST_NAME VARCHAR(30),
DEPT VARCHAR(30),
RANK NUMBER(3));
INSERT INTO TEST_RANK SELECT 91, 'State bank',NULL,NULL,1 FROM DUAL;
INSERT INTO TEST_RANK SELECT 91, 'State bank',NULL,'BANK',2 FROM DUAL;
INSERT INTO TEST_RANK SELECT 91, 'State bank of TN','Ind','BANKs',3 FROM DUAL;
INSERT INTO TEST_RANK SELECT 92, 'HDFC',NULL,'Curr',1 FROM DUAL;
INSERT INTO TEST_RANK SELECT 92, NULL,'HDFC bank','Curr',2 FROM DUAL;
INSERT INTO TEST_RANK SELECT 93, 'Citi','Bank','Bank',1 FROM DUAL;
insert into TEST_RANK SELECT 94, 'YEs','Bank',NULL,1 FROM DUAL;
SELECT * FROM TEST_RANK;
Best Answer
-
Hi, @Jeevan Anand
Thanks for posting the sample data and results. Don't forget to explain how you get the results from the sample data. For example "I want one row of output fro every distinct value of pricing_number. In the name column, I want to show the first non-NULL name (in order by the rank column) of all the rows with that pricing_id, and the same for the last_name and dept columns."
If that's what you want to do, here's one way to do it:
WITH unpivoted_data AS ( SELECT pricing_number, col, str , RANK () OVER ( PARTITION BY pricing_number, col ORDER BY rnk ) AS rnk_rank FROM test_rank UNPIVOT ( str FOR col IN (name, last_name, dept) ) ) SELECT * FROM unpivoted_data PIVOT ( MIN (str) FOR col IN ( 'NAME' AS name , 'LAST_NAME' AS last_name , 'DEPT' AS dept ) ) WHERE rnk_rank = 1 ;
Always post your full Oracle version (e.g. 18.4.0.0.0) also. The query above works in version 11.1.0.1.0 and up.
Answers
-
Hi, @Jeevan Anand
Thanks for posting the sample data and results. Don't forget to explain how you get the results from the sample data. For example "I want one row of output fro every distinct value of pricing_number. In the name column, I want to show the first non-NULL name (in order by the rank column) of all the rows with that pricing_id, and the same for the last_name and dept columns."
If that's what you want to do, here's one way to do it:
WITH unpivoted_data AS ( SELECT pricing_number, col, str , RANK () OVER ( PARTITION BY pricing_number, col ORDER BY rnk ) AS rnk_rank FROM test_rank UNPIVOT ( str FOR col IN (name, last_name, dept) ) ) SELECT * FROM unpivoted_data PIVOT ( MIN (str) FOR col IN ( 'NAME' AS name , 'LAST_NAME' AS last_name , 'DEPT' AS dept ) ) WHERE rnk_rank = 1 ;
Always post your full Oracle version (e.g. 18.4.0.0.0) also. The query above works in version 11.1.0.1.0 and up.
-
Hi,
RANK is the name of a built-in function, so it's not a good column name. In the query above, I used rnk as the column name.
The query above does not assume the values of rnk are consecutive integers, they can be any values. The string displayed will be the first non-NULL string (on order by rnk), regardless of whether the lowest rnk is 1, or 2, or 2.5, or -2.5, or anything else.
-
Use the first/last group function.
-
Thanks a lot Frank Kulash.