简体   繁体   English

SQL Server 2000查询最大日期的总值和值

[英]SQL Server 2000 Query of total value and value from max date

I have this RoomTable with value 我有这个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

What I want is to return the following: 我想要的是返回以下内容:

  1. Total sum of APhase and BPhase of each Room. 每个房间的APhase和BPhase的总和。
  2. Value of ACount and BCount from max date of each Room 每个房间最大日期的ACount和BCount的价值
  3. If ACount value is -1 then use the previous date. 如果ACount值为-1,则使用上一个日期。 Same as BCount. 与BCount相同。
  4. If ACount value is -1 and the previous date is -1 and so on. 如果ACount值为-1,前一个日期为-1,依此类推。 Then use 0. Same as BCount. 然后使用0.与BCount相同。

I can get the query of number 1 with this query 我可以使用此查询获得数字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

But I'm confused on how to include the number 2-4 query. 但我对如何包含2-4号查询感到困惑。

This is the output I want 这是我想要的输出

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

Any ideas will be much appreciated. 任何想法将不胜感激。 Thanks. 谢谢。

Hope this works for your case: 希望这适用于您的情况:

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

I am not sure if you really need that where clause so I commented it out. 我不确定你是否真的需要where子句,所以我评论了它。 And the value of TotalBPhase for Room Three on your result table should be 14, as can be seen from this SQL Fiddle demo . 在结果表中,RoomB的TotalBPhase值应该是14,从这个SQL Fiddle演示中可以看出。

You can use the answer by @yildizm85 or if you want more performant code, you can break down all tasks on smaller steps and use table variables to achieve the same. 您可以使用@ yildizm85的答案,或者如果您需要更高性能的代码,您可以分解较小步骤上的所有任务并使用表变量来实现相同的目标。 See below query... 见下面的查询......

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 

You can try query something like this>>> 您可以尝试查询类似这样的内容>>>

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM