0

我的 SQL Server 数据库中有两个要加入的表:

人员表:

PersonId   Name    DeviceId
001        John    11111
002        Eric    22222
003        Steve   33333

设备表:

DeviceId  Date
11111     2013-02-01
11111     2013-02-02
11111     2013-02-03
22222     2013-02-03
22222     2013-02-01

我想要的结果如下

PersonId   Name   DeviceId  Date         IsRegistered
001        John   11111     2013-02-03       1
002        Eric   22222     2013-02-03       1
003        Steve  33333     null             0

因此,如您所见,我想要表之间的连接,并且只想要唯一值。数据字段应该是最后注册的(最新日期)。如果此人在日期字段中具有值,IsRegistered则应具有值 0

如果有人知道我该如何解决这个问题,我将不胜感激

4

2 回答 2

4

询问:

SELECT 
  p.*, d.maxdate
FROM persons p
left join
(  SELECT id, MAX(date) MaxDate
   FROM device   
   GROUP BY id
) d  ON p.deviceid = d.id
     ORDER BY d.maxdate DESC;



| PERSONID |  NAME | DEVICEID |                         MAXDATE |
-----------------------------------------------------------------
|        1 |  John |    11111 | February, 03 2013 02:00:00+0000 |
|        2 |  Eric |    22222 | February, 03 2013 00:00:00+0000 |
|        3 | Steve |    33333 |                          (null) |

我看到您还需要isRegistered列,这里是:

  • SQLFIDDLE WITH 最后一列添加

询问:

SELECT 
    p.*, d.maxdate, case when d.maxdate is null then 0 else 1 end as isRegistered
    FROM persons p
    left join
    (  SELECT id, MAX(date) MaxDate
       FROM device   
       GROUP BY id
    ) d  ON p.deviceid = d.id
         ORDER BY d.maxdate DESC
    ;

结果:

| PERSONID |  NAME | DEVICEID |                         MAXDATE | ISREGISTERED |
--------------------------------------------------------------------------------
|        1 |  John |    11111 | February, 03 2013 02:00:00+0000 |            1 |
|        2 |  Eric |    22222 | February, 03 2013 00:00:00+0000 |            1 |
|        3 | Steve |    33333 |                          (null) |            0 |
于 2013-01-15T13:10:35.850 回答
1

试试这个:

SELECT 
  p.PersonId,
  p.Name,
  p.DeviceId,
  d.MaxDate AS "Date",
  CASE 
    WHEN d.deviceID IS NULL THEN 0 
    ELSE 1 
  END AS IsRegistred
FROM Person  p
LEFT JOIN
(
  SELECT  DeviceId, MAX(Date) MaxDate
  FROM Device
  GROUP BY DeviceId
) d ON p.DeviceId = d. DeviceId;

SQL 小提琴演示

这会给你:

| PERSONID |  NAME | DEVICEID |                            DATE | ISREGISTRED |
-------------------------------------------------------------------------------
|        1 |  John |    11111 | February, 03 2013 02:00:00+0000 |           1 |
|        2 |  Eric |    22222 | February, 03 2013 02:00:00+0000 |           1 |
|        3 | Steve |    33333 |                          (null) |           0 |

或者:使用排名功能ROW_NUMBER(),您可以这样做:

WITH CTE
AS
(
    SELECT
      p.PersonId,
      p.Name,
      p.DeviceId,
      d.deviceID AS ddeviceID,
      d.Date,
      ROW_NUMBER() OVER(PARTITION BY p.PersonID, p.DeviceId 
                        ORDER BY Date DESC) rownum
    FROM Person p
    LEFT JOIN Device d ON p.DeviceId = d. DeviceId
) 
SELECT
  PersonID,
  Name,
  DeviceId,
  Date,
  CASE 
    WHEN ddeviceID IS NULL THEN 0 
    ELSE 1 
  END AS IsRegistred
FROM CTE
WHERE  rownum = 1;

更新的 SQL Fiddle 演示

这会给你同样的结果。

于 2013-01-15T13:04:18.030 回答