简体   繁体   English

根据顺序选择多个记录

[英]select multiple records based on order by

i have a table with a bunch of customer IDs. 我有一个带有一堆客户ID的表。 in a customer table is also these IDs but each id can be on multiple records for the same customer. 客户表中也包含这些ID,但每个ID可以位于同一客户的多个记录中。 i want to select the most recently used record which i can get by doing order by <my_field> desc 我想选择最近使用的记录,我可以通过order by <my_field> desc进行order by <my_field> desc来获取

say i have 100 customer IDs in this table and in the customers table there is 120 records with these IDs (some are duplicates). 说我在此表中有100个客户ID,并且在customers表中有120条带有这些ID的记录(有些是重复的)。 how can i apply my order by condition to only get the most recent matching records? 如何order by条件应用order by以仅获取最新的匹配记录?

dbms is sql server 2000. dbms是sql server 2000。

table is basically like this: loc_nbr and cust_nbr are primary keys 该表基本上是这样的:loc_nbr和cust_nbr是主键

a customer shops at location 1. they get assigned loc_nbr = 1 and cust_nbr = 1 then a customer_id of 1. 某客户在位置1进行购物。他们被分配了loc_nbr = 1和cust_nbr = 1,然后分配了customer_id 1。

they shop again but this time at location 2. so they get assigned loc_nbr = 2 and cust_Nbr = 1. then the same customer_id of 1 based on their other attributes like name and address. 他们再次购物,但这次是在位置2。因此他们被分配了loc_nbr = 2和cust_Nbr = 1,然后根据其其他属性(例如名称和地址)分配了相同的customer_id 1。

because they shopped at location 2 AFTER location 1, it will have a more recent rec_alt_ts value, which is the record i would want to retrieve. 因为他们在位置1之后的位置2购物,所以它将有一个更新的rec_alt_ts值,这是我想要检索的记录。

Use an aggregate function in the query to group by customer IDs: 在查询中使用聚合函数按客户ID分组:

  SELECT cust_Nbr, MAX(rec_alt_ts) AS most_recent_transaction, other_fields
    FROM tableName
GROUP BY cust_Nbr, other_fields
ORDER BY cust_Nbr DESC;

This assumes that rec_alt_ts increases every time, thus the max entry for that cust_Nbr would be the most recent entry. 假定rec_alt_ts每次增加,因此该cust_Nbr的最大条目将是最新条目。

Since you didn't give real table and field names, this is just psuedo code for a solution. 由于您没有提供真实的表名和字段名,因此这只是解决方案的伪代码。

select * 
from customer_table t2
inner join location_table t1 
  on t1.some_key = t2.some_key
where t1.LocationKey = (select top 1 (LocationKey) as LatestLocationKey from  location_table  where cust_id = t1.cust_id order by some_field)

You want to use the ROW_NUMBER() function with a Common Table Expression (CTE). 您想将ROW_NUMBER()函数与通用表表达式(CTE)一起使用。

Here's a basic example. 这是一个基本示例。 You should be able to use a similar query with your data. 您应该能够对数据使用类似的查询。

;WITH TheLatest AS
(
    SELECT *, ROW_NUMBER() OVER (PARTITION BY group-by-fields ORDER BY sorting-fields) AS ItemCount 
    FROM TheTable 
)
SELECT * 
FROM TheLatest 
WHERE ItemCount = 1

UPDATE: I just noticed that this was tagged with sql-server-2000. 更新:我只是注意到,这是用sql-server-2000标记的。 This will only work on SQL Server 2005 and later. 这仅适用于SQL Server 2005及更高版本。

By using time and date we can take out the recent detail for the customer. 通过使用时间和日期,我们可以为客户提取最近的详细信息。 use the column from where you take out the date and the time for the customer. 使用从中获取客户日期和时间的列。

eg: 例如:

SQL> select ename , to_date(hiredate,'dd-mm-yyyy hh24:mi:ss') from emp order by to_date(hiredate,'dd-mm-yyyy hh24:mi:ss'); SQL>从emp顺序中选择ename,to_date(hiredate,'dd-mm-yyyy hh24:mi:ss'); to_date(hiredate,'dd-mm-yyyy hh24:mi:ss');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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