在您通知我們查詢,這兩個subquerys獲得了大量的數值在單行列表,例如:
N° First Name Last Name Seats 2014 Seats 2015 1 John Paul All seats of John Paul All seats of John Paul 2 Paul John All seats of Paul John All seats of Paul John
這是你想要的嗎?
再次編輯:
select
[First Name], [Last Name],
Seats_2014 = STUFF((
select ', ' + [2014 Seats]
from dbo.Combined2
where [ReportMonth2] between '2015-06-01' and '2015-06-30'
ORDER BY [2014 Seats]
FOR XML PATH('')), 1, 2, ''),
Seats_2015 = STUFF((
select ', ' + [2015 Seats]
from dbo.Combined2
where [ReportMonth2] between '2015-06-01' and '2015-06-30'
ORDER BY [2015 Seats]
FOR XML PATH('')), 1, 2, '')
from
dbo.Combined2
where
Region = 'NAM' and
[FTE Status] = 'Active' and
[ReportMonth2] between '2014-01-01' and '2014-12-31'
說明:
Seats_2014 = STUFF((
-- Stuff is used to remove the first ', ' from the result
select N', ' + [2014 Seats]
-- N is the string declaration for nvarchar (used to prevent problems with strange characters) and ', ' come before each Seat
from dbo.Combined2
where [ReportMonth2] between '2015-06-01' and '2015-06-30'
ORDER BY [2014 Seats]
FOR XML PATH(N'')), 1, 2, N''),
--for xml path do the concatenation trick, the 1,2 is how many characters will be removed from tab (', '), in this casse one ',' and one ' ' and trade for N''
也看到,這條線是在2014年和2015年一樣,這是正確的? where [ReportMonth2] between '2015-06-01' and '2015-06-30'
你想做什麼?這個查詢根本沒有任何意義。 –
我試圖提取2014年座位和2015年座位的列值僅2015年6月份,但其餘列需要考慮2014年全年。表名稱爲Combined2 –
您沒有提供任何附近足夠的信息遠不止於猜測。這將是一個很好的開始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –