在您通知我们查询,这两个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/ –