2015-10-09 52 views
0

我有两列[2014席位]和[2015席位],我只需要2015年6月份的值(不必介意列的名称,值可以为空),但其余列需要在全年(2014年)进行。我尝试了以下查询,并且发现子查询返回了超过1个值的错误。非常感谢您的帮助,谢谢!子查询为单个列过滤返回了超过1个值错误

select 
    [First Name], [Last Name], 
    (select [2014 Seats] 
    from dbo.Combined2 
    where [ReportMonth2] between '2015-06-01' and '2015-06-30') as Seats_2014, 
    (select [2015 Seats] 
    from dbo.Combined2 
    where [ReportMonth2] between '2015-06-01' and '2015-06-30') as Seats_2015 
from 
    dbo.Combined2 
where 
    Region = 'NAM' and 
    [FTE Status] = 'Active' and 
    [ReportMonth2] between '2014-01-01' and '2014-12-31' 
+0

你想做什么?这个查询根本没有任何意义。 –

+0

我试图提取2014年座位和2015年座位的列值仅2015年6月份,但其余列需要考虑2014年全年。表名称为Combined2 –

+0

您没有提供任何附近足够的信息远不止于猜测。这将是一个很好的开始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –

回答

1

我不知道你在做什么,但这里是在黑暗中拍摄的照片。

select [First Name] 
    ,[Last Name] 
from dbo.Combined2 
cross apply 
(
    select [2014 Seats] 
    from dbo.Combined2 
    WHERE [ReportMonth2] between '2015-06-01' and '2015-06-30' 
)as Seats_2014 
cross apply 
(
    select [2015 Seats] 
    from dbo.Combined2 
    WHERE [ReportMonth2] between '2015-06-01' and '2015-06-30' 
)as Seats_2015 
where Region='NAM' 
    and [FTE Status]='Active' 
    and [ReportMonth2] between '2014-01-01' and '2014-12-31' 
+0

嗨肖恩,感谢您的查询,我编辑了这个问题,以便它可以更清楚地理解。请让我知道你明白哪个部分的问题,我试过上面的查询,它只返回一个值多次 –

1

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

+0

Yepp..Exactly!..这些值的总和.. –

+0

我不dont知道如果这将工作,但试试看,并看到此链接的更多信息http://sqlperformance.com/2014/08/t-sql-queries/sql-server-grouped-concatenation –

+0

谢谢蒂亚戈,让我尝试并找回你 –

相关问题