查询以获取映射到同一代码的两个不同项目的总和
query to Get sum of two different items that is mapped onto same code
我有房间的库存 table :
dtm
hotelid
roomcode
intqty
2000-01-04
23
svr
9
2000-01-04
23
ovb
9
这是我的映射 table :
hotelid
roomcode1
roomcode2
externalroom
23
svr
ovb
023
我正在尝试获取上述两个房间在同一日期的库存 table 的 intqty 总和:
这是我目前所拥有的:
select
a.dtm as InvDate,
a.intResortID as ResortId,
a.strRoomType as RoomType,
sUM(a.intQty) as InvCount,
am.externalRoomId
from
dbo.tblAvailable c
join dbo.tblAvailable a on a.dtm = c.dtm
and a.intResortID = c.intResortID
and a.strRoomType = c.strRoomType
join dbo.tblResorts r on r.intResortID = a.intResortID
join bbtest.externalResortMap arm on arm.ResortID = a.intResortID
join bbtest.externalRoomMap am on am.RoomId = a.strRoomType
group by
a.dtm,
a.intResortID,
a.strRoomType,
a.intQty,
am.externalRoomId
order by
invDate;
预期结果是:
Invdate
ResortId
RoomType
InvCount
externalRoomId
2000-01-04
23
svr
18
023
查询输出:
Invdate
ResortId
RoomType
InvCount
externalRoomId
2000-01-04
23
svr
9
023
2000-01-04
23
ovb
9
023
不确定您为什么两次加入 tblAvailable,但如果我没有犯任何错误,这就是您需要做的事情:
select
a.dtm as InvDate,
a.intResortID as ResortId,
am.RoomId_1 as RoomType,
sum(a.intQty) as InvCount,
am.externalRoomId
from
dbo.tblAvailable a
join dbo.tblResorts r on r.intResortID = a.intResortID
join bbtest.externalResortMap arm on arm.ResortID = a.intResortID
join bbtest.externalRoomMap am
on a.strRoomType in (am.RoomId_1 ,am.RoomId_2)
group by
a.dtm,
a.intResortID,
am.RoomId_1,
am.externalRoomId
order by
invDate;
我有房间的库存 table :
dtm | hotelid | roomcode | intqty |
---|---|---|---|
2000-01-04 | 23 | svr | 9 |
2000-01-04 | 23 | ovb | 9 |
这是我的映射 table :
hotelid | roomcode1 | roomcode2 | externalroom |
---|---|---|---|
23 | svr | ovb | 023 |
我正在尝试获取上述两个房间在同一日期的库存 table 的 intqty 总和: 这是我目前所拥有的:
select
a.dtm as InvDate,
a.intResortID as ResortId,
a.strRoomType as RoomType,
sUM(a.intQty) as InvCount,
am.externalRoomId
from
dbo.tblAvailable c
join dbo.tblAvailable a on a.dtm = c.dtm
and a.intResortID = c.intResortID
and a.strRoomType = c.strRoomType
join dbo.tblResorts r on r.intResortID = a.intResortID
join bbtest.externalResortMap arm on arm.ResortID = a.intResortID
join bbtest.externalRoomMap am on am.RoomId = a.strRoomType
group by
a.dtm,
a.intResortID,
a.strRoomType,
a.intQty,
am.externalRoomId
order by
invDate;
预期结果是:
Invdate | ResortId | RoomType | InvCount | externalRoomId |
---|---|---|---|---|
2000-01-04 | 23 | svr | 18 | 023 |
查询输出:
Invdate | ResortId | RoomType | InvCount | externalRoomId |
---|---|---|---|---|
2000-01-04 | 23 | svr | 9 | 023 |
2000-01-04 | 23 | ovb | 9 | 023 |
不确定您为什么两次加入 tblAvailable,但如果我没有犯任何错误,这就是您需要做的事情:
select
a.dtm as InvDate,
a.intResortID as ResortId,
am.RoomId_1 as RoomType,
sum(a.intQty) as InvCount,
am.externalRoomId
from
dbo.tblAvailable a
join dbo.tblResorts r on r.intResortID = a.intResortID
join bbtest.externalResortMap arm on arm.ResortID = a.intResortID
join bbtest.externalRoomMap am
on a.strRoomType in (am.RoomId_1 ,am.RoomId_2)
group by
a.dtm,
a.intResortID,
am.RoomId_1,
am.externalRoomId
order by
invDate;