我用这些数据创建了数据库:
市场表:
ID| Name | Location
1 | NYSE | USA
2 | NASDAQ | USA
3 | CME | USA
4 | Euronext| Netherlands
周末餐桌:
ID| DayOff
1 | 2013-11-28
2 | 2013-12-25
3 | 2014-01-01
4 | 2014-01-21
5 | 2014-02-18
6 | 2014-03-29
7 | 2014-05-27
8 | 2014-07-04
9 | 2014-09-02
10| NULL
m2m_market_weekends 表:
market_id | weekend_id
1 | 1
1 | ...
1 | 9
2 | 1
2 | ...
2 | 9
2 | 1
3 | ...
3 | 9
5 | 10
编写查询,它将具有相等 ID 的数据分组并将所有日期附加到一个字符串中:
SELECT [Market].Name as [MarketName],
IsNull(STUFF((SELECT '; ' + CAST([DayOff] AS nvarchar(15)) FROM [Weekend] FOR XML PATH ('')),1, 1, ''), 'NULL') as [DayOff]
FROM [m2m_market_weekends]
INNER JOIN [Market] ON [m2m_market_weekends].[market_id] = [Market].[ID]
INNER JOIN [Weekend] ON [m2m_market_weekends].[weekend_id] = [Weekend].[ID]
GROUP BY [Market].Name
并得到:
MarketName | DayOff
NYSE | 2013-11-28; ...
NASDAQ | 2013-11-28; ...
CME | 2013-11-28; ...
Euronext | 2013-11-28; ... <-- This market have NULL value at Weekend table
如何解决这个问题,这个查询在 Euronext DayOff = 'NULL' 返回?