简体   繁体   中英

SQL Server 2008 R2: Show only recently added records

I have two tables:

  1. Cust : Contains customer details like customer ID and customer Name.

  2. Cust_Address : This table contains customer ID and customer address.

Table : Cust

create table cust
(
  cust_id int,
  cust_name varchar(10)
);

Records Insertion :

insert into cust values(1,'A');
insert into cust values(2,'B');
insert into cust values(3,'C');
insert into cust values(4,'D');

Table : Cust_Address

create table cust_address
(
  cust_id int,
  cust_add varchar(50)
);

Records Insertion :

insert into cust_address values(1,'US');
insert into cust_address values(2,'UK');
insert into cust_address values(3,'UAE');
insert into cust_address values(4,'SA');
insert into cust_address values(1,'AUS');
insert into cust_address values(2,'IND');
insert into cust_address values(3,'SL');
insert into cust_address values(1,'CHINA');

Now I want to show the result which contains the latest customer address which have been inserted in the table Cust_Address .

Expected Result :

Cust_ID    Cust_Name   Cust_Add
-------------------------------
1          A           CHINA
2          B           IND
3          C           SL
4          D           SA

Here is the SQLFiddle for tables and its records.

You are not able to retrieve the rows in any particular order. You need some more info to get an order. The best way is primary index in Cust_address

CustAddrID int identity(1, 1) not null primary key

You can also have a CreatedOn column that will have default value equal to getDate()

After that you can figure out what is the last inserted value for CustAddr for each Cust record.

In case you are not able to add new column there then maybe change tracking functionality. But your issue seems to be too trivial for that.

There are also Temporal Tables in SQL Server 2016. But again it's probably too much.

Here is an example how you can get the address using primary key CustAddrID SQL Fiddle

select  cust_name, cust_add
from cust C 
join 
(select 
  cust_add, cust_id,
  row_number() over (partition by cust_id  order by cust_add_id desc) rn
  from cust_address ) CLA
 on CLA.cust_id = C.cust_id and
    CLA.rn = 1

Identity column increases every time when we insert new value to the table. The correct value for your case will be the record with the highest cust_add_id and specified cust_id.

In the above query we generates numbers in desc order starting from 1 using row_number() function for each cust_id (partition by cust_id). Finally we take only the records with generated number rn equal to 1 CLA.rn = 1 and we join it to cust table.

You can replace row_number() by max(cust_add_id) and group by cust_id. However in that case you need to join cust_add table twice.

You will not be able to get the rows out of the link table in the order they were inserted.

You need to have a column for this.

Imagine how big the meta-data would be if you needed to keep a record for each record for creation! Would you also want to keep meta-data on your meta-data so you know when the meta-data was updated? The space use can quickly escalate.

SQL Server keeps some stats but something this specific will need to come from a user-defined field.

So you either use a identity column in the CustAddr table [CustAddr int identity(1, 1) not null primary key] or add a column for createdDateAndTime DateTime Default GetDate().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM