SQL查询MAX函数

时间:2014-06-19 04:20:19

标签: sql sql-server

我想根据最后一位用户获取特定产品的数据&各自上次使用的时间。例如

Host   Product   LastUserName   LastUsedTime
1      X1        ABC            6/13/2014
1      X1        ABC            6/14/2014
1      X1        ABC            6/15/2014
1      X1        XYZ            6/14/2014
1      X1        XYZ            6/15/2014
1      X1        XYZ            6/16/2014

我已经尝试过MAX函数并找到数据

Host   Product   LastUserName   LastUsedTime
1      X1        ABC            6/15/2014
1      X1        XYZ            6/16/2014

但无法获得所需的输出

Host   Product   LastUserName   LastUsedTime
1      X1        XYZ            6/16/2014

SQL:

select a.Netbios_Name0, b.DisplayName0, c.LastUserName0,
       MAX(c.LastUsedTime0) as [Last Used Time] 
from table1 a,table2 b,table3 c 
where a.ItemKey = b.ResourceID 
    and a.ItemKey = c.ResourceID 
group by a.Netbios_Name0, b.DisplayName0, c.LastUserName0 
order by b.DisplayName0 

感谢您的帮助!!

7 个答案:

答案 0 :(得分:1)

对于MSSQL:

select a.host, a.Product, a.LastUserName, a.LastUsedTime
from tablex a join 
(
select Host, Product, max(LastUsedTime) as LastUsedTime
    from tablex
    group by Host, Product 
) b on a.host = b.host and a.Product = b.Product and a.LastUsedTime = b.LastUsedTime 

答案 1 :(得分:0)

试试这个:

SELECT TOP 1 * FROM table_name ORDER BY LastUsername DESC,  LastUsedTime DESC 

答案 2 :(得分:0)

如果您只想要一行,请不要使用GROUP BY。只需对结果进行排序并使用TOP 1获取第一行(LIMIT 1用于MySQL - 这就是为什么你应该总是告诉你的RDBMS问题。)

select TOP 1 a.Netbios_Name0, b.DisplayName0, c.LastUserName0, c.LastUsedTime0
from table1 a,table2 b,table3 c 
where a.ItemKey = b.ResourceID 
    and a.ItemKey = c.ResourceID 
ORDER BY c.LastUsedTime0 DESC

答案 3 :(得分:0)

试试这个:

SELECT      TOP 1 a.Netbios_Name0, b.DisplayName0, c.LastUserName0,
            c.LastUsedTime0 as [Last Used Time] 
FROM        table1 a INNER JOIN
            table2 b ON a.ItemKey = b.ResourceID INNER JOIN
            table3 c ON a.ItemKey = c.ResourceID
WHERE       c.LastUsedTime0 = (SELECT TOP 1 LastUsedTime0 FROM table3 WHERE ResourceID = c.ResourceID)

答案 4 :(得分:0)

要获得每个产品的最后日期行,您首先需要获取最后一个日期,然后JOIN返回其他值

WITH L AS (
  SELECT b.DisplayName0, MAX(c.LastUsedTime0) LastUsedTime0
  FROM   table2 b
         INNER JOIN table3 c ON b.ResourceID = c.ResourceID
  GROUP BY b.DisplayName0
)
SELECT a.Netbios_Name0, b.DisplayName0, c.LastUserName0
     , c.LastUsedTime0 as [Last Used Time] 
FROM   table1 a
       INNER JOIN table2 b ON a.ItemKey = b.ResourceID
       INNER JOIN table3 c ON a.ItemKey = c.ResourceID
       INNER JOIN L ON b.DisplayName0 = L.DisplayName0
                   AND c.LastUsedTime0 = L.LastUsedTime0
ORDER BY b.DisplayName0 

如果您还需要将查询更改为

所需的主机名
WITH L AS (
  SELECT a.Netbios_Name0, b.DisplayName0, MAX(c.LastUsedTime0) LastUsedTime0
  FROM   table1 a
         INNER JOIN table2 b ON a.ItemKey = b.ResourceID
         INNER JOIN table3 c ON a.ItemKey = c.ResourceID
  GROUP BY a.Netbios_Name0, b.DisplayName0
)
SELECT a.Netbios_Name0, b.DisplayName0, c.LastUserName0
     , c.LastUsedTime0 as [Last Used Time] 
FROM   table1 a
       INNER JOIN table2 b ON a.ItemKey = b.ResourceID
       INNER JOIN table3 c ON a.ItemKey = c.ResourceID
       INNER JOIN L ON a.Netbios_Name0 = L.Netbios_Name0
                   AND b.DisplayName0 = L.DisplayName0
                   AND c.LastUsedTime0 = L.LastUsedTime0
ORDER BY b.DisplayName0 

作为最后一条建议,请停止在JOIN条款中写下WHERE定义,它的可读性较差,并与不同的想法混合使用

答案 5 :(得分:0)

 SELECT Product_Usage.*
   FROM Product_Usage
        INNER JOIN (SELECT Product, max(LastUsedTime) LastUSedTime
                      FROM Product_Usage
                     GROUP BY Product) LastUsed
                ON Product_Usage.Product = LastUsed.Product
               AND Product_Usage.LastUsedTime = LastUsed.LastUsedTime

答案 6 :(得分:0)

;用cte作为 ( 选择max(lastusedtime)over(PARTITION by product)为mdate          ,host,product,lastusername,lastusedtime          来自test1 ) 从cte
中选择不同的主机,产品,lastusername,lastusedtime lastusedtime = mdate enter image description here