2012-11-06 105 views
3

總價值和價值2000查詢我有這個RoomTable與價值SQL Server將從最大日

SID Room Date  APhase BPhase ACount BCount 
1  One 10/28/2012 4  5   3  6 
2  One 10/29/2012 2  3  -1  -1 
3  One 10/30/2012 4  5   7  -1 
4  Two 10/28/2012 8  3   2  3 
5  Two 10/30/2012 3  5   4  6 
6  Three 10/29/2012 5  8   2  -1 
7  Three 10/30/2012 5  6  -1  4 
8  Four 10/29/2012 6  2  -1  -1 
9  Four 10/30/2012 5  8  -1  -1 

我想是返回如下:

  1. APhase和β-相的總和每個房間。
  2. 從每個房間的最大日期計算ACount和BCount的值
  3. 如果ACount值爲-1,則使用上一個日期。與BCount相同。
  4. 如果ACount值是-1並且前一個日期是-1等等。然後使用0.與BCount相同。

我可以得到1號的查詢與此查詢

SELECT Room, sum(APhase) as TotalAPhase, sum(BPhase) as TotalBPhase 
FROM RoomTable 
WHERE Date between '10/28/2012' and '10/30/2012' 
group by Room 
order by Room 

但我對如何將數2-4查詢困惑。

這是輸出我想

Room TotalAPhase TotalBPhase ACount BCount 
One 10   13   7  6 
Two 11   8   4  6 
Three 10   13   2  4 
Four 11   10   0  0 

任何想法將非常感激。謝謝。

回答

5

希望這個作品對你的情況:

SELECT 
Room 
,SUM(APhase) AS TotalAPhase 
,SUM(BPhase) AS TotalBPhase 
,ISNULL(( SELECT TOP 1 RT1.ACount 
      FROM RoomTable RT1 
      WHERE RT1.Room = RT.Room 
       AND RT1.ACount != -1 
      ORDER BY RT1.Date DESC 
), 0) AS ACount 
,ISNULL(( SELECT TOP 1 RT2.BCount 
      FROM RoomTable RT2 
      WHERE RT2.Room = RT.Room 
       AND RT2.BCount != -1 
      ORDER BY RT2.Date DESC 
), 0) AS BCount 

FROM RoomTable RT 
--WHERE Date between '10/28/2012' and '10/30/2012' 
GROUP BY Room 
ORDER BY Room 

我不知道,如果你真的很需要那where子句所以我評論一下。並且結果表上第三個房間的TotalBPhase值應爲14,從this SQL Fiddle demo可以看出。

+0

我不能讓SQL Server 2000上這項工作,它說因爲它不是在聚合函數包含列「RT.Room」在選擇列表中無效.. 。 – quinekxi

+0

@quinekxi:嘗試在GROUP BY中使用'RT.'作爲前綴'Room'(一種瘋狂的猜測)。 –

+0

@AndriyM:這真是一個很好的猜測。現在它的工作就是我想要的。謝謝,謝謝yildizm85 – quinekxi

-1

您可以嘗試查詢像這樣>>>

SELECT Room, sum(APhase) as TotalAPhase, sum(BPhase) as TotalBPhase 
FROM RoomTable 
WHERE Date =(select max(Date) from RoomTable group by Room order by Room) 
and ACount=(update RoomTable set ACount=0 where ACount<0) and and ACount=(update RoomTable set BCount=0 where BCount<0) 
group by Room 
order by Room 
2

您可以通過@ yildizm85使用的答案,或者,如果你想更高性能的代碼,你可以打破所有任務上更小的步驟,並使用表變量實現相同。請參見下面的查詢......

declare @roomResult table 
(Room nvarchar(50), maxadate datetime, maxbdate datetime, TotalAPhase int , TotalBPhase int , acount int, bcount int) 

insert into @roomResult 
SELECT Room, null,null,sum(APhase) as TotalAPhase, sum(BPhase) as TotalBPhase ,0,0 
FROM RoomTable 
group by Room 
order by Room 

update @roomresult 
set maxadate = mxdate 
from 
(
select room roomname, max(date) mxdate from 
(
select room, date , case when acount = -1 then null else acount end acount , case when bcount = -1 then null else bcount end bcount 
from roomtable) as a where a.acount is not null 
group by room 
) b inner join @roomresult c on b.roomname = c.room 


update @roomresult 
set maxbdate = mxdate 
from 
(
select room roomname, max(date) mxdate from 
(
select room, date , case when bcount = -1 then null else bcount end bcount 
from roomtable) as a where a.bcount is not null 
group by room 
) b inner join @roomresult c on b.roomname = c.room 

update @roomresult 
set acount = r.acount 
from @roomresult rr inner join roomtable r 
on rr.room = r.room and rr.maxadate = r.date 

update @roomresult 
set bcount = r.bcount 
from @roomresult rr inner join roomtable r 
on rr.room = r.room and rr.maxbdate = r.date 

select Room,TotalAPhase,TotalbPhase,ACount,BCount From @roomResult 
+0

bapare patilji tumhi數據庫madhale master disata。 yewadha motha答案!!! + 1 – Freelancer

+0

但是它工作:-) –

+0

aasanar na。 mhanun tar mhanalo tumhi master disata database madhale。 – Freelancer