2015-10-09 85 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

謝謝蒂亞戈,讓我嘗試並找回你 –

相關問題