3

私は値を持つこのRoomTableを持っています

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. 各部屋の A Phase と B Phase の合計。
  2. 各部屋の最大日付からの ACount と BCount の値
  3. ACount 値が -1 の場合は、前の日付を使用します。Bカウントと同じ。
  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

どんなアイデアでも大歓迎です。ありがとう。

4

3 に答える 3

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句が本当に必要かどうかわからないので、コメントアウトしました。この SQL Fiddle demoからわかるように、結果テーブルの Room Three の TotalBPhase の値は 14 になるはずです。

于 2012-11-06T05:38:23.583 に答える
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 
于 2012-11-06T07:18:04.837 に答える
-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
于 2012-11-06T05:39:35.377 に答える