SQL查询以获取仅显示表中最新日期的数据

时间:2018-04-05 07:33:17

标签: sql sql-server

我有3张表格如下:

表用户

+---+-----+--------+
| id| name| contact|
+---+-----+--------+
|101| John| 123121 |
|102| Jake| 123122 |
|103|  Mia| 123123 |
|104| Mike| 123124 |
|105|Drake| 123125 |
|106|Jonas| 123126 |
+---+-----+--------+

表格交易

+------+--------+-------+
|billno|billdate|user_id|
+------+--------+-------+
|  A001|01/01/18|    101|
|  A002|01/01/18|    102|
|  A003|01/01/18|    103|
|  A004|01/02/18|    101|
|  A005|01/02/18|    105|
|  A006|01/02/18|    102|
|  A007|01/03/18|    105|
|  A008|01/03/18|    106|
|  A009|01/04/18|    101|
|  A010|01/04/18|    103|
|  A011|01/04/18|    106|
+------+--------+-------+

表产品

+------+-----------+
|billno|productcode|
+------+-----------+
|  A001|       P001|
|  A001|       P002|
|  A001|       P002|
|  A002|       P001|
|  A002|       P003|
|  A003|       P001|
|  A003|       P003|
|  A003|       P004|
|  A004|       P001|
|  A004|       P005|
|  A005|       P001|
|  A005|       P003|
|  A005|       P004|
|  A006|       P001|
|  A007|       P001|
|  A007|       P005|
|  A008|       P002|
|  A008|       P003|
|  A008|       P004|
|  A008|       P005|
|  A009|       P001|
+------+-----------+

此表是一个示例,真实表上有更多数据。

如果我的查询是这样的:

SELECT user.name, user.contact, transaction.billno, transaction.billdate 
FROM user inner join transaction on user.id = transaction.user_id inner join 
product on transaction.billno = product.billno 
WHERE product.productcode = 'P001' 

它将显示所有账单号和用户的产品代码为P001。

我的问题是,如何只显示产品代码为P001的用户的最新日期?

结果应该是这样的:

1 | John| 123121| A009| 01/04/18
2 |Drake| 123125| A007| 01/03/18
3 | Jake| 123125| A006| 01/02/18

只需拥有该产品代码为P001的用户ID的最新日期。

希望你们理解我的问题。谢谢。 :)

3 个答案:

答案 0 :(得分:1)

尝试此查询:

select * from (
    select [t].[user_id], [p].billno, [p].productcode, [t].billdate,
           --here we assign "ranks", starting with latest date
           row_number() over (partition by [t].[user_id] order by [t].billdate desc) [rn]
    from (
        --here we filter out by product code, so we will get only P001
        select billno, productcode from Product where productcode = 'P001'
    ) [p]
    join [Transaction] [t] on [p].billno = [t].billno
) [a] where [rn] = 1

答案 1 :(得分:1)

您当前的尝试似乎有效,只需添加 top(1)with ties 子句即可显示每个用户的最新帐单

select top(1) with ties u.id, u.name, u.contact, t.billno, t.billdate
from User u inner join Transaction t on t.user_id = u.id
            inner join Product p on p.billno = t.billno
where p.productcode = 'P001'
order by row_number() over (partition by u.id order by t.billdate desc)

答案 2 :(得分:0)

SELECT user.name, 
       user.contact, 
       transaction.billno, 
       MAX(transaction.billdate)
FROM user
INNER JOIN transaction ON user.id = transaction.user_id 
INNER JOIN product ON transaction.billno = product.billno 
WHERE product.productcode = 'P001' 
GROUP BY user.name,
         user.contact
         transaction.billno;

您可以应用DateTime数据类型MIN([col_name])& MAX([col_name])并且DBMS默认按顺序对它们进行排序 - ,

不要忘记使用 GROUP BY对您在select子句后放置的其他列进行分组

另一种方法

SELECT TOP 1 user.name, 
             user.contact, 
             transaction.billno, 
             MAX(transaction.billdate)
FROM user
INNER JOIN transaction ON user.id = transaction.user_id 
INNER JOIN product ON transaction.billno = product.billno 
WHERE product.productcode = 'P001'
ORDER BY transaction.billdate DESC;