Sunday, December 17, 2023

Dense_Rank with Group function

 Source_Path: "E:\source_files\CUSTOMER_CGI.CSV"

sdate,custid,pname,region,pcategory,price 01-01-2019,111,pen,NY,stationary,10.00 01-01-2019,111,pencil,NY,stationary,40.00 01-01-2019,222,pen,NY,stationary,10.00 01-01-2019,333,pen,NY,stationary,10.00 01-01-2019,444,book,CT,stationary,55.00 01-01-2019,222,pen,NY,stationary,10.00 01-02-2019,333,pen,NY,stationary,10.00 01-02-2019,333,pen,NY,stationary,10.00 01-02-2019,111,slate,NY,stationary,40.00 01-02-2019,111,slate,NY,stationary,40.00 01-02-2019,555,bag,NY,stationary,100.00 01-03-2019,444,pen,CT,stationary,10.00 01-03-2019,444,pencil,CT,stationary,15.00 01-03-2019,444,pencil,CT,stationary,15.00 01-03-2019,444,pen,CT,stationary,10.00 01-03-2019,111,pen,NY,stationary,10.00 01-03-2019,111,pen,NY,stationary,10.00 01-04-2019,111,pen,NY,stationary,10.00 01-04-2019,222,pen(,NY,stationary,10.00 01-04-2019,333,pen,NY,stationary,10.00 01-04-2019,444,choco,CT,stationary,50.00 01-04-2019,444,choco,CT,stationary,50.00 SET LINES 100 ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY'; select * from CUSTOMER_CGI ORDER BY SDATE ; SDATE CUSTID PNAME REGION PCATEGORY PRICE ---------- ---------- ---------- ---------- ---------- ---------- 01-01-2019 333 pen NY stationary 10 01-01-2019 222 pen NY stationary 10 01-01-2019 111 pencil NY stationary 40 01-01-2019 111 pen NY stationary 10 01-01-2019 444 book CT stationary 55 01-01-2019 222 pen NY stationary 10 01-02-2019 111 slate NY stationary 40 01-02-2019 111 slate NY stationary 40 01-02-2019 333 pen NY stationary 10 01-02-2019 333 pen NY stationary 10 01-02-2019 555 bag NY stationary 100 01-03-2019 444 pen CT stationary 10 01-03-2019 444 pencil CT stationary 15 01-03-2019 444 pencil CT stationary 15 01-03-2019 444 pen CT stationary 10 01-03-2019 111 pen NY stationary 10 01-03-2019 111 pen NY stationary 10 01-04-2019 111 pen NY stationary 10 01-04-2019 222 pen NY stationary 10 01-04-2019 333 pen NY stationary 10 01-04-2019 444 choco CT stationary 50 01-04-2019 444 choco CT stationary 50 By SQL Method : Q1: --Get the custer detail who has done highest sales per day (datewise highest sales) SELECT * FROM ( SELECT custid, sdate, SUM (price) AS total_price, DENSE_RANK () OVER (PARTITION BY sdate ORDER BY SUM (price) desc) AS rnk --desc if u want maximum , asc if u want minimum FROM CUSTOMER_CGI GROUP BY sdate, custid ) tt WHERE tt.rnk = 1 ; Q2: --Get the custer detail along with date where the total sales price is/are the highest . SELECT * FROM ( SELECT custid, sdate, SUM (price) AS total_price, DENSE_RANK () OVER (ORDER BY SUM (price) desc) AS rnk --desc if u want maximum , asc if u want minimum FROM CUSTOMER_CGI GROUP BY sdate, custid ) tt WHERE tt.rnk = 1 By Dataframe Method:

No comments:

Post a Comment