Skip to main content

Sql Complex queries [Over and Partition by]

 Over and Partition by clause 

Useful when want to select aggregated results with non-aggregated columns, group by not useful in this case.


Example  

User table having username, name, and country field,  here we want user count by country 


select username as email,
name,
count(country) over
(partition by country) as tolaUserByCountry,
country
from User
order by tolaUserByCountry desc;

-- Another solution is using joins



select reseller.username as email,
reseller.name,
tolaUserByCountry
from User
join (select country, count(country)
as tolaUserByCountry from User group by country ) cr
on reseller.country = cr.country order by tolaUserByCountry desc ;






Row Number


select transid, row_number() over (order by chargedamount desc )
from paymentdetails limit 5;


top 5 records of with charged amount decreasing order.


Can find nth amount.

select pd.transid, pd.chargedamount
from (select transid, chargedamount, row_number() over (order by chargedamount desc ) as id
from paymentdetails) as pd
where pd.id = 4;



There may be cases where find the best from each country or location or something else.

select pd.transid, pd.chargedamount , pd.country
from (select transid, chargedamount,country, row_number() over (partition by country order by chargedamount desc ) as id
from paymentdetails) as pd
where pd.id = 1;

























Comments

Popular posts from this blog

Stay Updated As Software Engineer

Are you a software engineer looking to stay updated and grow in your field? We've got you covered with over 50 valuable resources to keep you on the cutting edge of technology. From newsletters to books, we've curated a diverse list just for you.   Newsletters:   Pragmatic Engineer: Link   TLDR: Link   Level-up software engineering: Link   Coding challenges: Link   Engineers Codex: Link   Techlead Mentor: Link   Saiyan Growth letter: Link   Wes Kao: Link   Addy Osmani: Link   And many more (see link below)   Books:   Engineering:   A Philosophy of Software Design Link   Clean Code Link   Communication & Soft Skills:   Smart Brevity Link   Connect: Building Exceptional Relationships Link   Crucial Conversations Link   Engineers Survival Guide Link   Leadership:   The Manager's Path Link   Staff Engineer: Leadership Beyond the Management Track Link   The Coaching Habit: Say Less, Ask More Link   While we can't list all 50+ resources here, this is a fantastic sta

12 Must-Know LeetCode+ Links for Coding Excellence

Introduction: Welcome to a comprehensive guide on mastering essential coding techniques and strategies! Whether you're a beginner or an experienced coder, these LeetCode+ links will elevate your skills and make you a more proficient problem solver. Let's dive into the world of algorithms, data structures, and coding patterns that will empower you to tackle complex challenges with confidence. 1. Sliding Window Learn the art of efficient sliding window techniques: Sliding Window - Part 1 and Sliding Window - Part 2 . Enhance your coding prowess and optimize algorithms with these invaluable insights. 2. Backtracking Unlock the power of backtracking algorithms: Backtracking . Discover how to systematically explore possibilities and find optimal solutions to a variety of problems. 3. Greedy Algorithm Master the art of making locally optimal choices for a globally optimal solution: Greedy Algorithm . Dive into strategies that prioritize immediate gains and lead to optimal outcomes

Learning How to Map One-to-Many Relationships in JPA Spring Boot with PostgreSQL

  Introduction In this blog post, we explore how to effectively map one-to-many relationships using Spring Boot and PostgreSQL. This relationship type is common in database design, where one entity (e.g., a post) can have multiple related entities (e.g., comments). We'll dive into the implementation details with code snippets and provide insights into best practices. Understanding One-to-Many Relationships A one-to-many relationship signifies that one entity instance can be associated with multiple instances of another entity. In our case: Post Entity : Represents a blog post with fields such as id , title , content , and a collection of comments . Comment Entity : Represents comments on posts, including fields like id , content , and a reference to the post it belongs to. Mapping with Spring Boot and PostgreSQL Let's examine how we define and manage this relationship in our Spring Boot application: Post Entity  @Entity @Getter @Setter @Builder @AllArgsConstructor @NoArgsCons