En iyi yanıtlayıcılar
Tablo içinde birden fazla dönen değerin sonuncusu

Soru
-
Merhabalar, View üzerinde çalışıyorum. Bir tablo içinde bir kolonda birden fazla dönen kayıt var, ve ben burada en son hangisi eklenmiş ise onu kullanmak istiyorum. View ile yapmam pek mümkün gelmedi, ama sizlerinde düşüncelerini alabilir miyim ?
- Düzenleyen Scytodes 1 Ekim 2022 Cumartesi 22:04
Yanıtlar
-
Tek bir id yolluyorsanız order by desc işinizi görüyor olmalı. Birden çok id yi tek listede görmek istiyorsanız ROW_NUMBER() kullanabilirsiniz;
create table TestUser ( Id int identity(1,1)not null primary key, UserName nvarchar(100) ) insert into TestUser (UserName) values ('User1'), ('User2'), ('User3') create table UserLog ( Id int identity(1,1)not null primary key, UserId int not null, LogDate date, constraint fk_UserLog_UserId foreign key (UserId) references TestUser(Id) ) insert into UserLog (UserId,LogDate) values (1,'20220101'), (1,'20220102'), (1,'20220103'), (2,'20220101'), (2,'20220102'), (2,'20220103'), (3,'20220101'), (3,'20220102'), (3,'20220103') with tmp as ( select *, ROW_NUMBER() over ( partition by UserId order by LogDate desc) as rowNo from UserLog ) select* from tmp where rowNo= 1
Altan Özdemir
- Yanıt Olarak İşaretleyen Scytodes 30 Eylül 2022 Cuma 08:57
Tüm Yanıtlar
-
Tek bir id yolluyorsanız order by desc işinizi görüyor olmalı. Birden çok id yi tek listede görmek istiyorsanız ROW_NUMBER() kullanabilirsiniz;
create table TestUser ( Id int identity(1,1)not null primary key, UserName nvarchar(100) ) insert into TestUser (UserName) values ('User1'), ('User2'), ('User3') create table UserLog ( Id int identity(1,1)not null primary key, UserId int not null, LogDate date, constraint fk_UserLog_UserId foreign key (UserId) references TestUser(Id) ) insert into UserLog (UserId,LogDate) values (1,'20220101'), (1,'20220102'), (1,'20220103'), (2,'20220101'), (2,'20220102'), (2,'20220103'), (3,'20220101'), (3,'20220102'), (3,'20220103') with tmp as ( select *, ROW_NUMBER() over ( partition by UserId order by LogDate desc) as rowNo from UserLog ) select* from tmp where rowNo= 1
Altan Özdemir
- Yanıt Olarak İşaretleyen Scytodes 30 Eylül 2022 Cuma 08:57
-
-
SQL serverda "en son" kavrami sadece indexler uzerindendir. Fiziksel olarak en son hangisinin eklendigini ogrenebilmek icin rowversion, identity gibi bir kolonunuzun olmasi gerekir.
"En son" icin hangi kolonu kullanacaginizi belirledikten sonra tek yapmaniz gereken top ve order by kullanmak. Ornegin, soyad,ad'a gore en son ise:
Select top(1) * from myTable order by soyad desc, ad desc;
Blog
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop.
Not: Temelin geri zekali arkadasi Idris bu mesaja da atlayip ne kadar aptal oldugunu bir kez daha belgeleyebilir. Kendisinin tek marifeti beni takip edip, neden her soruya cevap verdigimi, yardim etmeye calistigimi sorgulamaktir. Bu beyinsiz zavalliya, aptal oldugunu hatirlatmayi unutmayiniz.- Düzenleyen CetinBasoz_Dont Trust Microsoft 30 Eylül 2022 Cuma 12:14
-
-
Top 1 neden işinizi görmüyordu anlayamadım. Tekrarlayan olunca top(1) kullanılamıyor mu?
Blog
How to create a Minimal, Reproducible Example
The way to Go.
World's most advanced open source (object-) relational Database.
Flutter (for mobile, for web & desktop.
Not: Temelin geri zekali arkadasi Idris bu mesaja da atlayip ne kadar aptal oldugunu bir kez daha belgeleyebilir. Kendisinin tek marifeti beni takip edip, neden her soruya cevap verdigimi, yardim etmeye calistigimi sorgulamaktir. Bu beyinsiz zavalliya, aptal oldugunu hatirlatmayi unutmayiniz.