select C.customerId,(C.lastName+', '+C.firstName) as CustomerName, C.companyName,
D.companyName+' ('+D.lastName+','+D.firstName+')'
as "Parent CompanyName(Last, First)",S.siteId, S.nickName as siteName,
dbo.GetSiteTelemetryBoxList(s.siteId) as "DeviceId's",
dbo.GetSiteTelemetryBoxSKUList(S.siteId,0) as SKU
from Site S
INNER JOIN Customer C ON S.customerId = C.customerId
INNER JOIN Customer D ON D.customerId = C.parentCustomerId
where S.createDate between DATEADD(DAY, -65, GETUTCDATE()) and GETUTCDATE()
order by C.customerId, S.siteId
上面的查询返回如下所示的值:
CID CustomerName companyName Parent CompanyName(Last, First) SiteName DeviceId SKU
888296 DeYoung, Scott DeYoung Farms Mercier Valley Irrigation (Mercier,Ralph) H E east 200241 NETB12WR
890980 Rust, Marcus NULL Chester Inc. (Young,Scott) Byroad east 346370 NETB12WR
890980 Rust, Marcus NULL Chester Inc. (Young,Scott) Byroad west 345431 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) hwy 64 south 333721 UNKNOWN
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 North 250162 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 West 250164 NETB12WR
891094 Pirani, Mark A Pirani Farm AMX Irrigation (Burroughs,Michael) HWY 64 East 250157 NETB12WR
891430 Gammil, Bob Gammil FArms AMX Irrigation (Burroughs,Michael) angel 333677 UNKNOWN
891430 Gammil, Bob Gammil FArms AMX Irrigation (Burroughs,Michael) cemetery 333564 UNKNOWN
我现在面临的问题是,如果 customerId/Name 在结果集中重复。SiteName、deviceId、SKU 应该连接起来以将数据表示为一个值。例如,Mark Pirani 行看起来像
CID CustomerName ... SiteName DeviceId's ...
891904 Pirani, Mark ... hwy 64 south, HWY 64 North, HWY 64 West, HWY 64 East 333721,250162,250164,250157 ...