Тёмный

Practice Activity - Retrieving the last row for each group in a table in SQL Server 

SQL Server 101
Подписаться 17 тыс.
Просмотров 12 тыс.
50% 1

You may have a table with various products or areas. How can you extract the latest data for each group?
My SQL Server Udemy courses are:
70-461, 70-761 Querying Microsoft SQL Server with T-SQL: rebrand.ly/que...
98-364: Database Fundamentals (Microsoft SQL Server): rebrand.ly/dat...
70-462 SQL Server Database Administration (DBA): rebrand.ly/sql...
Microsoft SQL Server Reporting Services (SSRS): rebrand.ly/sql...
SQL Server Integration Services (SSIS): rebrand.ly/sql...
SQL Server Analysis Services (SSAS): rebrand.ly/sql...
Microsoft Power Pivot (Excel) and SSAS (Tabular DAX model): rebrand.ly/mic...
----
In this video, we will be looking at fictious house prices per region. However, the data is not for the same dates in each region.
How can you get the latest row for each group? So you need the latest row for East Midlands, the latest row for West Midlands etc.
There are at least three different ways for doing this:
1. In the WHERE clause, using a correlated query,
2. Using a JOIN, and
3. Using the ROW_NUMBER function.
Here is the code to create and populate the table. If you want to try it yourself, why not check your solutions with mine?
----
CREATE TABLE tblHouseprices (
PriceDate date NOT NULL,
Region varchar(20) NOT NULL,
price int NOT NULL);
GO
INSERT INTO tblHouseprices
VALUES
('2022-06-01', 'Greater Manchester', 346251),
('2022-07-01', 'East Midlands', 312289),
('2022-07-01', 'West Midlands', 365274),
('2022-08-01', 'East Midlands', 328072),
('2022-08-01', 'Greater Manchester', 353617),
('2022-09-01', 'East Midlands', 339697),
('2022-09-01', 'West Midlands', 370206),
('2022-09-01', 'Greater Manchester', 358902),
('2022-10-01', 'West Midlands', 376596),
('2022-10-01', 'Greater Manchester', 357744),
('2022-11-01', 'West Midlands', 371699);

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 16   
@krishnakamalgogoi9893
@krishnakamalgogoi9893 2 месяца назад
with cte3 as (select *, ROW_NUMBER() over(partition by Region order by Pricedate desc) as region_rnk from tblhouseprices) select * from cte3 where region_rnk = 1
@Random_dudebro
@Random_dudebro 7 месяцев назад
I have been looking for a way to do this for about 3 weeks for a work project and you saved me… thank you lol
@SQLServer101
@SQLServer101 7 месяцев назад
Hi Random. I'm glad that I could help. Phillip
@itsgroovylue
@itsgroovylue 7 месяцев назад
You absolutely saved my day my friend!! Thank you!
@Mnfls
@Mnfls 9 месяцев назад
This is 🤯
@dangkhanhlinh476
@dangkhanhlinh476 8 месяцев назад
nice!
@nonoobott8602
@nonoobott8602 Год назад
Absolutely brilliant. This is so useful. Thanks for sharing
@jacekk7819
@jacekk7819 Год назад
I`m not sure why but my CTE with ROW_NUMBER is faster than other two , query plan showing me 22% , 39% , 39% for query cost
@SQLServer101
@SQLServer101 Год назад
Hi Jacek. Very interesting - it's good a) to be able to have multiple ways to do the same thing, and b) to see which is faster! Phillip
@diegocosta168
@diegocosta168 11 месяцев назад
Great! Helped me a lot 👏👏👏
@danramirz
@danramirz 2 года назад
Thanks for this video!
@ElWiwif
@ElWiwif Год назад
You rock!! Thanks!!
@HippoAliasSkelman
@HippoAliasSkelman Год назад
thanks a lot :)
@minnrick7986
@minnrick7986 2 года назад
Thanks! CTE seems the most elegant to me :)
@SQLServer101
@SQLServer101 2 года назад
Thanks - I do like CTEs, as they can simplify nested subqueries - which we'll be having a look at in the next video. Phillip
@taibabs1222
@taibabs1222 2 года назад
You could also put the query into a temp table as suppose to a CTE.
Далее
I Took An iPhone 16 From A POSTER! 😱📱 #shorts
00:18
Как открыть багажник?
00:36
Просмотров 16 тыс.
SQL Temp Tables
10:21
Просмотров 5 тыс.
Solving one of PostgreSQL's biggest weaknesses.
17:12
Просмотров 197 тыс.
Solving SQL Query | Rows to Column in SQL
1:13:11
Просмотров 172 тыс.
SQL Server Management Studio (SSMS) | Full Course
1:22:44
Calculating the first day of the week in SQL Server
8:05
Beginner to T-SQL [Full Course]
2:45:54
Просмотров 314 тыс.