0

我有以下使用 SQL Server 2005 的数据集:

TABLE NAME: LOG

TYPE       TIMESTAMP            USERID
Job        26/03/2013 00:24     DED
Job        21/03/2013 02:31     EGA
Sales      NULL                 NULL
Sales      NULL                 NULL

我想根据 MA​​X(timestamp) 检索列 TYPE、TIMESTAMP 和 USERID.. 即结果应该是

Job        26/03/2013 00:24     DED

我试过以下

SELECT max(timestamp), UserID
FROM log
GROUP BY UserID

但是,这会返回所有行。

当我尝试

SELECT max(timestamp), max(UserID)
FROM log
GROUP BY UserID

返回

Job        26/03/2013 00:24     EGA

我了解返回的 max(userID) 是按字母顺序排序的最大列值。

最后当我尝试

SELECT * 
FROM log 
WHERE timestamp = (SELECTmax(timestamp) FROM log)

我没有得到任何结果(空)..

如果有人可以提供帮助,将不胜感激!


编辑更新:

如果它有任何帮助-我应该提到我有多个表,并且需要SELECT并且需要WHERE TYPE` 可以等于('Job' 或 'Sales')..USERIDMAX(timestamp)

我发现这非常有效:

SELECT Type,
       MAX(CASE WHEN log.Type IN ('Job', 'Sales') THEN log.Timestamp ELSE NULL END) As Timestamp,
FROM log

现在的问题是当我尝试时:

SELECT Type,
       MAX(CASE WHEN log.Type IN ('Job', 'Sales') THEN log.Timestamp ELSE NULL END) As Timestamp,
       MAX(CASE WHEN log.Type IN ('Job', 'Sales') THEN log.UserID ELSE NULL END) As UserID,
FROM log

它给了我基于字母顺序的 max(userID),而不是基于 max(timeframe)..

当 TYPE = JOB 或 SALES 时,我需要 MAX(timeframe) 和相应的 UserID

(放置在带有许多其他列的大选择查询的末尾)。


示例查询更新:

这是我要执行的查询.. 非常抱歉我缺乏深入的知识!

DECLARE @FROMDATE AS DATETIME, @TODATE AS DATETIME;
SET @FROMDATE = '1 Mar 2013'; SET @TODATE = '31 Mar 2013';

SELECT CASE WHEN country.country_code IN (61,64,673,60,65,679,66,62,92,84,63,91) THEN 'APAC'
            WHEN country.country_code IN (81,82,852,853,886) THEN 'EAST ASIA'
            WHEN country.country_code IN (44,353,32,45,358,33,49,30,39,352,356,47,351,34,46,31) THEN 'EUROPE'
            WHEN country.country_code IN (1,101) THEN 'USA/CANADA' ELSE 'OTHER'
       END AS REGION,

       info.Status as STATUS, info.Code as CODE,
       CONVERT(VARCHAR, info.Start_Date,103) as 'START DATE', 

       log.Type AS TYPE,
       MAX(CASE WHEN log.Type IN ('Job','Sales') THEN log.Timestamp ELSE NULL END) As TIMESTAMP,   -- this works fine! :)
       MAX(CASE WHEN log.Type IN ('Job','Sales') THEN log.UserID ELSE NULL END) As USERID  --having trouble with this! :(

FROM log JOIN info ON (log.id = info.id), country

WHERE info.date BETWEEN @FROMDATE AND @TODATE
      AND info.Status NOT LIKE '%NA%'

GROUP BY country.code, info.status, info.code, info.start_date, log.type

我希望这对大家有所帮助,并再次感谢您的惊人支持。我猜这很简单,但我错过了一些东西..

4

2 回答 2

1

我根据您的问题创建了以下示例测试表:

CREATE TABLE Log (TYPE VARCHAR(5) NOT NULL, TIMESTAMP DATETIME NULL, USERID VARCHAR(3) NULL);

INSERT INTO Log
VALUES ('Job', '03/26/2013 00:24', 'DED'), ('Job', '03/21/2013 02:31', 'EGA'), ('Sales', NULL, NULL), ('Sales', NULL, NULL);

我尝试了您的最后一个示例查询,它工作正常。由于此查询对您不起作用,因此我编写了以下内容,它也应该可以处理并处理您有多个具有相同 TIMESTAMP 值的行的情况:

WITH BaseData
AS
    (SELECT *
        , ReverseOrder = ROW_NUMBER() OVER
            (ORDER BY TIMESTAMP DESC)
    FROM Log)
SELECT *
FROM BaseData
WHERE ReverseOrder = 1;
于 2013-03-26T18:27:40.067 回答
1

查看我对另一个问题的回答,以根据日期列获取表格中的第一行或最后一行:https ://stackoverflow.com/a/10111071/452792

你可以试试 :

DECLARE @FROMDATE AS DATETIME, @TODATE AS DATETIME;
SET @FROMDATE = '1 Mar 2013'; SET @TODATE = '31 Mar 2013';

;WITH LOG_CTE AS 
(
  SELECT Stuff(Max(Convert(Varchar, Timestamp, 20) + IsNull(Convert(Varchar, Id), '')), 1, 19, '') As Id
       , Max(Type) As Type
       , Max(timestamp) As Timestamp
       , Stuff(Max(Convert(Varchar, Timestamp, 20) + IsNull(Convert(Varchar, UserID), '')), 1, 19, '') As UserID
    FROM log 
   GROUP BY log.Type 
  HAVING log.Type IN ('JOB','SALES')
)
SELECT CASE WHEN country.country_code IN (61,64,673,60,65,679,66,62,92,84,63,91) THEN 'APAC'
            WHEN country.country_code IN (81,82,852,853,886) THEN 'EAST ASIA'
            WHEN country.country_code IN (44,353,32,45,358,33,49,30,39,352,356,47,351,34,46,31) THEN 'EUROPE'
            WHEN c.country_code IN (1,101) THEN 'USA/CANADA' ELSE 'OTHER'
       END AS REGION,

       info.Status as STATUS, info.Code as CODE,
       CONVERT(VARCHAR, info.Start_Date,103) as 'START DATE', 

       log.Type AS TYPE,
       log.Timestamp,
       log.UserID

FROM LOG_CTE log JOIN info ON (log.id = info.id)

WHERE info.date BETWEEN @FROMDATE AND @TODATE
      AND info.Status NOT LIKE '%NA%'
于 2013-03-26T19:47:14.187 回答