需要连接不同的表,但在连接期间需要每列选择不同的行
我有下表:
Devices
-------
id
Device
Snid
locationid
planid
Locations
---------
id
location
Plans
------
id
plan
Assignments
-----------
id
deviceid
startdate
enddate
我尝试过的是这个
SELECT DISTINCT a.id, a.device, a.Snid, b.location, c.plan, d.startdate, d.enddate
FROM Devices a
LEFT JOIN Locations b on a.locationid=b.id
LEFT JOIN Plans c on a.planid=c.id
LEFT JOIN Assignments d on a.id=deviceid
如果在select中,我删除了分配列(开始日期,结束日期),那么我得到了正常结果,但是我需要获取分配表列。但是,一旦添加列,就无法获得不同的设备。
作为结果,我想得到的是具有计划和位置(用于计划和位置名称)以及分配开始日期和结束日期列的组合的设备表。并且要拥有具有最新任务的独特设备,在我的情况下,该设备应使用空白的enddate列。
此外,我需要获得totalassignments栏。我尝试在选择部分添加:
SUM(case when a.id=d.deviceid then 1 else 0) as totalassignments
但是如果我使用这个,我需要添加一个分组依据。在分组依据中,我需要包括所有列,然后再次丢失我的与众不同。
示例如下:
设备表
id device locationid planid
2 computer 1 0
3 mobile 2 1
10 printer 1 1
12 scanner 1 1
32 tablet 1 1
42 tv 2 2
68 video 3 2
98 camera 0 0
位置表
ID location
1 Storage
2 Office
3 Contractor
计划表
ID Plan
1 Short-term
2 Long-term
分配表
id deviceid startdate enddate
1 10 10/17/2019 2/23/2019
2 32 10/20/2019
3 12 10/18/2019 11/1/2019
4 68
5 98 10/15/2019 10/5/2019
6 42 10/23/2019
7 10 10/1/2019 NULL
8 2 8/18/2019 10/21/2019
9 3 10/23/2020 NULL
10 12 10/2/2019 NULL
所需结果
device deviceid location plan totalassignments startdate enddate
computer 2 Storage NULL 1 8/18/2019 10/21/2019
mobile 3 Office Short-term 1 10/23/2020 NULL
printer 10 Storage Short-term 2 10/1/2019 NULL
scanner 12 Storage Short-term 2 10/2/2019 NULL
tablet 32 Storage Short-term 1 10/20/2019
tv 42 Office Long-term 1 10/23/2019
video 68 ContractorLong-term 1 NULL NULL
camera 98 NULL NULL 1 10/15/2019
如您所见,结果必须具有设备(已替换的locationid和planid以及使用内部联接的相关位置和计划名称),每个设备均具有分配的数量,并在其旁边具有开始日期和结束日期最新分配(在结束日期为NULL时配置)
答案 0 :(得分:1)
这是因为列数据中的时间因素不相同。
CAST(d.startdate AS DATE)
将使时间元素无效,仅保留日期
尝试一下
SELECT DISTINCT a.id, a.device, a.Snid, b.location, c.plan,
CAST(d.startdate AS DATE), CAST(d.enddate AS DATE)
FROM Devices a LEFT JOIN Locations b on a.locationid=b.id
LEFT JOIN Plans c on a.planid=c.id LEFT JOIN Assignments d on a.id=deviceid
如果您想包括聚合函数,
SELECT DISTINCT a.id, a.device, a.Snid, b.location, c.plan,
CAST(d.startdate AS DATE), CAST(d.enddate AS DATE),
SUM(case when a.id=d.deviceid then 1 else 0) as totalassignments
FROM Devices a LEFT JOIN Locations b on a.locationid=b.id
LEFT JOIN Plans c on a.planid=c.id LEFT JOIN Assignments d on a.id=deviceid
GROUP BY a.id, a.device, a.Snid, b.location, c.plan,
CAST(d.startdate AS DATE), CAST(d.enddate AS DATE)