Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Changing an output based on previous listed data

Author  Topic 

sqlchiq
Posting Yak Master

133 Posts

Posted - 2008-10-22 : 00:07:25
location CheckinDay checkindate roomtype roomrate executiontime id_num
Palms Saturday 8/30/2008 Florida 199 8/15/2008 1
Palms Saturday 8/30/2008 Florida SOLD 8/8/2008 2
Palms Saturday 8/30/2008 Atrium 239 8/8/2008 3
Palms Saturday 8/30/2008 Atrium 239 8/15/2008 4


I have the above set, I was wondering if there was a way to output a result set, where if a certain room was sold out on a certain date, then make it sold out for all future dates also.

For example, in the above, the "Florida" room on 8/30/2008 was sold out on 8/8, however, it reappears on 8/15.

Is there a way to change the 8/15 (and all future dates) rate to sold out?

For all rooms where the location, checkinday, checkindate, and roomtype match obviously (dont want to change a rate for rooms that arent the same, or dont have the same checkin)

Desired result set


location CheckinDay checkindate roomtype roomrate executiontime id_num
Gaylord Palms Saturday 8/30/2008 Florida SOLD 8/15/2008 1
Gaylord Palms Saturday 8/30/2008 Florida SOLD 8/8/2008 2
Gaylord Palms Saturday 8/30/2008 Atrium 239 8/8/2008 3
Gaylord Palms Saturday 8/30/2008 Atrium 239 8/15/2008 4




Thanks for any help in advance

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-10-22 : 00:26:49
try this

DECLARE @Rooms TABLE
(
location VARCHAR(100),
CheckinDay VARCHAR(100),
checkindate DATETIME,
roomtype VARCHAR(100),
roomrate VARCHAR(100),
executiontime DATETIME,
id_num INT
)
INSERT INTO @Rooms
SELECT 'Palms', 'Saturday', '8/30/2008', 'Florida', '199', '8/15/2008', 1 UNION ALL
SELECT 'Palms', 'Saturday', '8/30/2008', 'Florida', 'SOLD', '8/8/2008', 2 UNION ALL
SELECT 'Palms', 'Saturday', '8/30/2008', 'Atrium', '239', '8/8/2008', 3 UNION ALL
SELECT 'Palms', 'Saturday', '8/30/2008', 'Atrium', '239', '8/15/2008', 4


UPDATE RA
SET roomrate = 'SOLD'
FROM @Rooms RA
INNER JOIN (SELECT * FROM @Rooms WHERE roomrate = 'SOLD') RS ON RS.location = RA.location
AND RA.CheckinDay = RS.CheckinDay
AND RA.checkindate = RS.checkindate
AND RA.roomtype = RS.roomtype
AND RA.executiontime > RS.executiontime

SELECT * FROM @Rooms



"There is only one difference between a dream and an aim. A dream requires soundless sleep to see, whereas an aim requires sleepless efforts to achieve..!!"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-22 : 00:29:56
[code]Update t
SET t.roomrate ='SOLD'
FROM yourtable t
INNER JOIN (SELECT roomtype,MAX(executiontime) as maxdate
FROM table
WHERE roomrate='SOLD'
GROUP BY roomtype)t1
on t1.roomtype=t.roomtype
and t.executiontime>t1.maxdate[/code]
Go to Top of Page

cvraghu
Posting Yak Master

187 Posts

Posted - 2008-10-22 : 02:06:17
Why do you store multiple rows for the same room type and check in date?
Go to Top of Page

sqlchiq
Posting Yak Master

133 Posts

Posted - 2008-10-22 : 18:20:01
quote:
Originally posted by visakh16

Update t
SET t.roomrate ='SOLD'
FROM yourtable t
INNER JOIN (SELECT roomtype,MAX(executiontime) as maxdate
FROM table
WHERE roomrate='SOLD'
GROUP BY roomtype)t1
on t1.roomtype=t.roomtype
and t.executiontime>t1.maxdate




is it possible to do this without actually changing the table?

like in a simple select statement that will display the output
Go to Top of Page

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-10-22 : 18:48:02
[code]
select location, checkinday, checkindate, roomtype, case when (SELECT top 1 1 FROM @Rooms t1
WHERE t1.roomrate='SOLD' and t1.roomtype=t.roomtype and t1.checkinday = t.checkinday and t1.checkindate = t.checkindate
and t1.roomtype = t.roomtype) = 1 then 'SOLD' else roomrate end, executiontime
from @Rooms t[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-23 : 00:01:01
[code]SELECT t.location,t.CheckinDay,t.checkindate,
t.roomtype,
CASE WHEN sld.SoldDate IS NOT NULL THEN 'SOLD'
ELSE CAST(t.roomrate AS varchar(10))
END AS roomrate,
t.executiontime,
t.id_num
FROm yourtable t
OUTER APPLY(SELECT TOP 1 ExecutionTime AS SoldDate
FROM Yourtable
WHERE roomtype=t.roomtype
AND roomrate='SOLD'
AND Executiontime <t.ExecutionTime
ORDER BY ExecutionTime DESC)sld[/code]
Go to Top of Page
   

- Advertisement -