获取设备在一个表中的最新日期和从第二个表拉动相关字段

时间:2018-03-30 22:52:57

标签: sql tsql sql-server-2016

我有“table1”......

╔══════════╤═════════════════════════╤══════════╤══════════╗
║ EventID  │ UpdateTime              │ User     │ DeviceID ║
╠══════════╪═════════════════════════╪══════════╪══════════╣
║ 1        │ 2018-03-27 17:36:34.080 │ Sally    │ 129      ║
╟──────────┼─────────────────────────┼──────────┼──────────╢
║ 2        │ 2018-03-27 17:47:44.057 │ Jack     │ 563      ║
╟──────────┼─────────────────────────┼──────────┼──────────╢
║ 3        │ 2018-03-29 02:14:01.203 │ Bob      │ 789      ║
╟──────────┼─────────────────────────┼──────────┼──────────╢
║ 4        │ 2018-03-27 17:23:11.350 │ Cathy    │ 352      ║
╟──────────┼─────────────────────────┼──────────┼──────────╢
║ 5        │ 2018-03-27 17:35:31.470 │ John     │ 352      ║
╟──────────┼─────────────────────────┼──────────┼──────────╢
║ 6        │ 2018-03-27 17:36:34.080 │ Margaret │ 2376     ║
╚══════════╧═════════════════════════╧══════════╧══════════╝

...和“table2”......

╔══════════╤═════════════╤════════╗
║ ID       │ DeviceName  │ Active ║
╠══════════╪═════════════╪════════╣
║ 129      │ DeviceA     │ False  ║
╟──────────┼─────────────┼────────╢
║ 563      │ DeviceB     │ True   ║
╟──────────┼─────────────┼────────╢
║ 789      │ DeviceC     │ True   ║
╟──────────┼─────────────┼────────╢
║ 352      │ DeviceD     │ True   ║
╟──────────┼─────────────┼────────╢
║ 2376     │ DeviceE     │ False  ║
╚══════════╧═════════════╧════════╝

我想最终得到这个:

╔═════════════╤═════════════════════════╤══════╗
║ DeviceName  │ LatestUpdateTime        │ User ║
╠═════════════╪═════════════════════════╪══════╣
║ DeviceB     │ 2018-03-27 17:47:44.057 │ Jack ║
╟─────────────┼─────────────────────────┼──────╢
║ DeviceC     │ 2018-03-29 02:14:01.203 │ Bob  ║
╟─────────────┼─────────────────────────┼──────╢
║ DeviceD     │ 2018-03-27 17:35:31.470 │ John ║
╚═════════════╧═════════════════════════╧══════╝

我已经:

SELECT t2.DeviceName, t1.UpdateTime, t1.User
FROM table1 t1
INNER JOIN (
    SELECT MAX(UpdateTime) AS 'LatestUpdateTime', DeviceID
    FROM table2
    GROUP BY DeviceID
    ) t2 ON t1.ID = t2.DeviceID
WHERE t2.Active = 'True'
ORDER BY t2.DeviceName

我想要“DeviceName”和“LatestUpdateTime”,但我也想不出如何获得“用户”。

非常感谢任何帮助。

很难找到这个并且在这里决定头衔。如果你有更好的想法,我也对此感兴趣。

2 个答案:

答案 0 :(得分:3)

[DEMO]

我会使用common table expression

;WITH cte AS (
    SELECT t2.DeviceName, 
        t1.UpdateTime,
        ROW_NUMBER() OVER (PARTITION BY t1.DeviceID ORDER BY t1.UpdateTime DESC) RowNum, 
        t1.[User]
    FROM table1 t1
    INNER JOIN table2 t2 ON t1.DeviceID = t2.ID
    WHERE t2.Active = 'True')

SELECT DeviceName, UpdateTime LatestUpdateTime, [User] 
FROM cte
WHERE RowNum = 1
ORDER BY DeviceName

enter image description here

我正在订购t1.UpdateTime DESC并为其分配行号。

每次找到重复的t1.DeviceID时,都会分配一个增量编号,这就是为什么我选择查找RowNum = 1(意思是,给我最新的更新时间)。

您可能遇到的一个问题是使用Reserved Word User。请注意我是如何将其包裹在方括号[User]中的。

如果您有平局,我建议您使用t1.EventID DESC

因此,您要将RowNum上的顺序更改为:

ROW_NUMBER() OVER (PARTITION BY t1.DeviceID ORDER BY t1.UpdateTime DESC, t1.EventID DESC)

答案 1 :(得分:0)

pivotTable.DataFields[1].Format = "#0.00%";
pivotTable.DataFields[1].Function = DataFieldFunctions.Count;
pivotTable.DataFields[1].Name = "Percent of Total";

foreach (XmlElement documentElementChild in pivotTable.PivotTableXml.DocumentElement.ChildNodes)
{
    if (documentElementChild.Name.Equals("dataFields"))
    {
        foreach (XmlElement dataFieldChild in documentElementChild.ChildNodes)
        {
            foreach (XmlAttribute attribute in dataFieldChild.Attributes)
            {
                if (attribute.Value.Equals("Percent of Total"))
                {
                    // found our dataField element; add the attribute
                    dataFieldChild.SetAttribute("showDataAs", "percentOfTotal");
                    break;
                }
            }
        }
    }
}

dbfiddle